RepositoryTest.php
96 lines
| 1 | <?php |
| 2 | |
| 3 | use Jet_Form_Builder\Exceptions\Repository_Exception; |
| 4 | use JFB_Components\Repository\Interfaces\Repository_Pattern_Interface; |
| 5 | use JFB_Components\Repository\Repository_Item_Instance_Trait; |
| 6 | use JFB_Components\Repository\Repository_Pattern_Trait; |
| 7 | |
| 8 | class RepositoryTest extends \Codeception\TestCase\WPTestCase |
| 9 | { |
| 10 | /** |
| 11 | * @var \WpunitTester |
| 12 | */ |
| 13 | protected $tester; |
| 14 | /** |
| 15 | * @var Repository_Pattern_Interface |
| 16 | */ |
| 17 | protected $repository; |
| 18 | |
| 19 | public function setUp(): void |
| 20 | { |
| 21 | // Before... |
| 22 | parent::setUp(); |
| 23 | |
| 24 | // Your set up methods here. |
| 25 | $this->repository = new class() implements Repository_Pattern_Interface { |
| 26 | use Repository_Pattern_Trait; |
| 27 | |
| 28 | public function rep_instances(): array { |
| 29 | return array( |
| 30 | new class() implements Repository_Item_Instance_Trait { |
| 31 | public function rep_item_id() { |
| 32 | return 'first'; |
| 33 | } |
| 34 | }, |
| 35 | new class() implements Repository_Item_Instance_Trait { |
| 36 | public function rep_item_id() { |
| 37 | return 'second'; |
| 38 | } |
| 39 | }, |
| 40 | new class() implements Repository_Item_Instance_Trait { |
| 41 | public function rep_item_id() { |
| 42 | return 'third'; |
| 43 | } |
| 44 | }, |
| 45 | ); |
| 46 | } |
| 47 | }; |
| 48 | } |
| 49 | |
| 50 | public function tearDown(): void |
| 51 | { |
| 52 | // Your tear down methods here. |
| 53 | |
| 54 | // Then... |
| 55 | parent::tearDown(); |
| 56 | } |
| 57 | |
| 58 | public function testHasItems() { |
| 59 | $this->reset(); |
| 60 | |
| 61 | $this->assertCount( 3, $this->repository->rep_get_values() ); |
| 62 | } |
| 63 | |
| 64 | // tests |
| 65 | public function testGetById() { |
| 66 | $this->reset(); |
| 67 | |
| 68 | $this->assertTrue( $this->repository->rep_isset_item( 'first' ) ); |
| 69 | $this->assertTrue( $this->repository->rep_isset_item( 'second' ) ); |
| 70 | $this->assertTrue( $this->repository->rep_isset_item( 'third' ) ); |
| 71 | } |
| 72 | |
| 73 | public function testInstallNewItem() { |
| 74 | $this->reset(); |
| 75 | |
| 76 | try { |
| 77 | $this->repository->rep_install_item( |
| 78 | new class() implements Repository_Item_Instance_Trait { |
| 79 | public function rep_item_id() { |
| 80 | return 'new'; |
| 81 | } |
| 82 | } |
| 83 | ); |
| 84 | } catch ( Repository_Exception $exception ) { |
| 85 | $this->addWarning( $exception->getMessage() ); |
| 86 | } |
| 87 | |
| 88 | $this->assertTrue( $this->repository->rep_isset_item( 'new' ) ); |
| 89 | } |
| 90 | |
| 91 | protected function reset() { |
| 92 | $this->repository->rep_clear(); |
| 93 | $this->repository->rep_install(); |
| 94 | } |
| 95 | } |
| 96 |