| 1 |
<?php |
| 2 |
|
| 3 |
namespace QuadLayers\WP_Orm\Tests\Single; |
| 4 |
|
| 5 |
use PHPUnit\Framework\TestCase; |
| 6 |
use Brain\Monkey\Functions; |
| 7 |
use Brain\Monkey; |
| 8 |
use QuadLayers\WP_Orm\Builder\SingleRepositoryBuilder; |
| 9 |
use QuadLayers\WP_Orm\Repository\SingleRepository; |
| 10 |
|
| 11 |
class SingleRepositoryBuilderTest extends TestCase |
| 12 |
{ |
| 13 |
private array $testValue; |
| 14 |
private array $testDefaults; |
| 15 |
private string $table; |
| 16 |
private SingleRepository $repository; |
| 17 |
|
| 18 |
protected function setUp(): void |
| 19 |
{ |
| 20 |
|
| 21 |
$this->table = 'settings'; |
| 22 |
|
| 23 |
$this->testDefaults = [ |
| 24 |
'key1' => 'default_value_1', |
| 25 |
'key2' => 'default_value_2' |
| 26 |
]; |
| 27 |
|
| 28 |
$this->testValue = [ |
| 29 |
'key1' => 'value1', |
| 30 |
'key2' => 'value2', |
| 31 |
]; |
| 32 |
|
| 33 |
$builder = (new SingleRepositoryBuilder()) |
| 34 |
->setTable($this->table) |
| 35 |
->setEntity('\QuadLayers\WP_Orm\Tests\Single\SingleEntityTest'); |
| 36 |
|
| 37 |
$this->repository = $builder->getRepository(); |
| 38 |
|
| 39 |
Functions\when('update_option')->alias( |
| 40 |
function ($option, $value) { |
| 41 |
if (serialize($this->testValue) !== serialize($value)) { |
| 42 |
fwrite(STDOUT, "testValue => " . json_encode($this->testValue, true)); |
| 43 |
fwrite(STDOUT, "value => " . json_encode($value, true)); |
| 44 |
return false; |
| 45 |
} |
| 46 |
if ($this->table !== $option) { |
| 47 |
fwrite(STDOUT, "option => " . json_encode($option, true)); |
| 48 |
fwrite(STDOUT, "table => " . json_encode($this->table, true)); |
| 49 |
return false; |
| 50 |
} |
| 51 |
return true; |
| 52 |
} |
| 53 |
); |
| 54 |
|
| 55 |
// When get_option is called, return existingData |
| 56 |
Functions\when('get_option')->justReturn($this->testValue); |
| 57 |
} |
| 58 |
|
| 59 |
protected function tearDown(): void |
| 60 |
{ |
| 61 |
Monkey\tearDown(); |
| 62 |
parent::tearDown(); |
| 63 |
} |
| 64 |
|
| 65 |
public function testSave() |
| 66 |
{ |
| 67 |
|
| 68 |
$result = $this->repository->create($this->testValue); |
| 69 |
|
| 70 |
$this->assertTrue($result); |
| 71 |
} |
| 72 |
|
| 73 |
public function testGet() |
| 74 |
{ |
| 75 |
// Invoke get method |
| 76 |
$result = $this->repository->find(); |
| 77 |
|
| 78 |
// Ensure the result matches our expectations |
| 79 |
// key1 should be the same as existing data |
| 80 |
$this->assertEquals($this->testValue['key1'], $result->getKey1()); |
| 81 |
// key2 should be the default value from the schema |
| 82 |
$this->assertEquals($this->testValue['key2'], $result->getKey2()); |
| 83 |
} |
| 84 |
|
| 85 |
public function testDefaults() |
| 86 |
{ |
| 87 |
|
| 88 |
$result = $this->repository->create($this->testValue); |
| 89 |
|
| 90 |
// Ensure the result matches our expectations |
| 91 |
// key1 should be the same as existing data |
| 92 |
$this->assertEquals($this->testDefaults['key1'], 'default_value_1'); |
| 93 |
// key2 should be the default value from the schema |
| 94 |
$this->assertEquals($this->testDefaults['key2'], 'default_value_2'); |
| 95 |
} |
| 96 |
} |
| 97 |
|