diff --git a/tests/unit/Stub/WP_Post.php b/tests/unit/Stub/WP_Post.php new file mode 100644 index 0000000000000000000000000000000000000000..bba4f366d9ee04ea1002cbf4541411407c5e1315 --- /dev/null +++ b/tests/unit/Stub/WP_Post.php @@ -0,0 +1,6 @@ +<?php + +class WP_Post { + public $ID; + public $post_type; +} \ No newline at end of file diff --git a/tests/unit/TestForm.php b/tests/unit/TestForm.php deleted file mode 100644 index 53e4f80af25aaf718d46bc710042a5e759493531..0000000000000000000000000000000000000000 --- a/tests/unit/TestForm.php +++ /dev/null @@ -1,58 +0,0 @@ -<?php - -namespace Tests; - -use WPDesk\Forms\AbstractForm; - -class TestForm extends \PHPUnit\Framework\TestCase -{ - - const FORM1_ID = 'test_form'; - const FORM1_FORM_DATA = [ 'test' => true ]; - const FORM1_UPDATED_FORM_DATA = [ 'test666' => true ]; - - private $form; - - protected function setUp(){ - // Create a new instance from the Abstract Class - $this->form = $this->getMockBuilder( AbstractForm::class ) - ->enableOriginalConstructor() - ->setMethods(['get_form_id']) - ->getMockForAbstractClass(); - $this->form->method( 'get_form_id' )->willReturn( self::FORM1_ID ); - $this->form->method( 'create_form_data' )->willReturn( self::FORM1_FORM_DATA ); - } - - protected function getForm(){ - return clone $this->form; - } - - /** - * Test getting form id. - */ - public function testFormId() - { - $form = $this->getForm(); - $this->assertEquals(self::FORM1_ID, $form->get_form_id()); - } - - /** - * Test getting form data. - */ - public function testFormData() - { - $form = $this->getForm(); - $this->assertSame( self::FORM1_FORM_DATA, $form->get_form_data()); - } - - /** - * Test updated form data. - */ - public function testUpdatedFormData() - { - $form = $this->getForm(); - - $form->update_form_data( self::FORM1_UPDATED_FORM_DATA ); - $this->assertSame( array_merge( self::FORM1_FORM_DATA, self::FORM1_UPDATED_FORM_DATA ), $form->get_form_data()); - } -} \ No newline at end of file diff --git a/tests/unit/TestFormCollection.php b/tests/unit/TestFormCollection.php deleted file mode 100644 index 55ddd37438974c3a97e32f192a6a518b3a9d89ec..0000000000000000000000000000000000000000 --- a/tests/unit/TestFormCollection.php +++ /dev/null @@ -1,141 +0,0 @@ -<?php - -namespace Tests; - -use WPDesk\Forms\AbstractForm; -use WPDesk\Forms\FormsCollection; - -class TestFormCollection extends \PHPUnit\Framework\TestCase { - - const FORM1_ID = 'test_form'; - const FORM2_ID = 'test_form2'; - - const FORM1_FORM_DATA = [ 'test' => true ]; - const FORM2_FORM_DATA = [ 'test2' => 'potato' ]; - - const FORM1_PREFIXED_FORM_DATA = [ 'test_form_test' => true ]; - const FORM1_UPDATED_FORM_DATA = [ 'test666' => true ]; - - - private $formConditionalTrue; - private $formConditionalFalse; - - protected function setUp() { - $this->formConditionalTrue = $this->getMockBuilder( AbstractForm::class ) - ->enableOriginalConstructor() - ->setMethods( [ 'get_form_id', 'is_active' ] ) - ->getMockForAbstractClass(); - $this->formConditionalTrue->method( 'get_form_id' )->willReturn( self::FORM1_ID ); - $this->formConditionalTrue->method( 'is_active' )->willReturn( true ); - $this->formConditionalTrue->method( 'create_form_data' )->willReturn( self::FORM1_FORM_DATA ); - - $this->formConditionalFalse = $this->getMockBuilder( AbstractForm::class ) - ->enableOriginalConstructor() - ->setMethods( [ 'get_form_id', 'is_active' ] ) - ->getMockForAbstractClass(); - $this->formConditionalFalse->method( 'get_form_id' )->willReturn( self::FORM2_ID ); - $this->formConditionalFalse->method( 'is_active' )->willReturn( false ); - $this->formConditionalFalse->method( 'create_form_data' )->willReturn( self::FORM2_FORM_DATA ); - } - - protected function getFormConditionalTrue() { - return clone $this->formConditionalTrue; - } - - protected function getFormConditionalFalse() { - return clone $this->formConditionalFalse; - } - - /** - * Test adding and checking single form. - */ - public function testIfFormExists() { - $collection = new FormsCollection(); - $collection->add_form( $this->getFormConditionalTrue() ); - - $this->assertTrue( $collection->is_form_exists( self::FORM1_ID ) ); - } - - /** - * Test adding and checking multiple forms. - */ - public function testIfFormsExists() { - $collection = new FormsCollection(); - $collection->add_forms( [ - $this->getFormConditionalTrue(), - $this->getFormConditionalFalse(), - ] ); - - $this->assertTrue( $collection->is_form_exists( self::FORM1_ID ) ); - $this->assertTrue( $collection->is_form_exists( self::FORM2_ID ) ); - } - - /** - * Test getting single form. AbstractForm object is expected - */ - public function testGettingExistingForm() { - $collection = new FormsCollection(); - $collection->add_form( $this->getFormConditionalTrue() ); - - $this->assertInstanceOf( - AbstractForm::class, - $collection->get_form( self::FORM1_ID ) - ); - } - - /** - * Test getting not existing single form. - */ - public function testGettingNotExistingForm() { - $collection = new FormsCollection(); - $collection->add_form( $this->getFormConditionalTrue() ); - - $this->expectException( \OutOfRangeException::class ); - $collection->get_form( '123456' ); - } - - /** - * Test returned data. - */ - public function testReturnedFormsData() { - $collection = new FormsCollection(); - $collection->add_forms( [ - $this->getFormConditionalTrue(), - $this->getFormConditionalFalse(), - ] ); - - $this->assertSame( self::FORM1_FORM_DATA, $collection->get_forms_data() ); - } - - /** - * Test returned prefixed data. - */ - public function testReturnedPrefixedFormsData() { - $collection = new FormsCollection(); - $collection->add_forms( [ - $this->getFormConditionalTrue(), - $this->getFormConditionalFalse(), - ] ); - - $this->assertSame( self::FORM1_PREFIXED_FORM_DATA, $collection->get_forms_data( true ) ); - - - } - - /** - * Test returned updated data. - */ - public function testReturnedUpdatedFormsData() { - $collection = new FormsCollection(); - $collection->add_forms( [ - $this->getFormConditionalTrue(), - $this->getFormConditionalFalse(), - ] ); - - $form = $collection->get_form( self::FORM1_ID ); - $form->update_form_data( self::FORM1_UPDATED_FORM_DATA ); - $this->assertSame( array_merge( self::FORM1_FORM_DATA, self::FORM1_UPDATED_FORM_DATA ), $collection->get_forms_data() ); - - } - -} \ No newline at end of file diff --git a/tests/unit/TestGetStrategy.php b/tests/unit/TestGetStrategy.php new file mode 100644 index 0000000000000000000000000000000000000000..da614486f28603d4dfd3f124c6472959c7a86df3 --- /dev/null +++ b/tests/unit/TestGetStrategy.php @@ -0,0 +1,83 @@ +<?php + +namespace unit; + +use PHPUnit\Framework\TestCase; +use WPDesk\ShowDecision\GetStrategy; + +class TestGetStrategy extends TestCase { + const true_1 = [ 'a' => '1' ]; + const true_2 = [ 'b' => '2' ]; + const true_3 = [ 'c' => '3' ]; + + const false_1 = [ 'x' => '1' ]; + const false_2 = [ 'y' => '2' ]; + + /** + * Prepares $_GET with true clauses + */ + private function prepare_get() { + $_GET = [ + self::true_1, + self::true_2, + self::true_3 + ]; + } + + protected function setUp() { + parent::setUp(); + $this->prepare_get(); + } + + public function testAndClausesFailureTest() { + $strategy = new GetStrategy( [ + [ + self::true_1, + self::false_1 + ] + ] ); + $this->assertFalse( $strategy->shouldDisplay() ); + } + + public function testAndClausesSuccess() { + $strategy = new GetStrategy( [ + [ + self::true_1, + self::true_2, + self::true_3 + ] + ] ); + $this->assertTrue( $strategy->shouldDisplay() ); + } + + public function testOrClausesSuccess() { + $strategy = new GetStrategy( [ + [ + self::true_1, + self::false_1 + ], + [ + self::true_1, + self::true_2 + ] + ] ); + $this->assertTrue( $strategy->shouldDisplay() ); + } + + public function testOrClausesFailure() { + $strategy = new GetStrategy( [ + [ + self::true_1, + self::false_1 + ], + [ + self::true_1, + self::true_2, + self::true_3, + self::false_1 + ] + ] ); + $this->assertFalse( $strategy->shouldDisplay() ); + } + +} diff --git a/tests/unit/TestPostTypeStrategy.php b/tests/unit/TestPostTypeStrategy.php new file mode 100644 index 0000000000000000000000000000000000000000..8b0790f67172d41c6bcdda07b6b67559178c7c05 --- /dev/null +++ b/tests/unit/TestPostTypeStrategy.php @@ -0,0 +1,54 @@ +<?php + +namespace unit; + +use PHPUnit\Framework\TestCase; +use WPDesk\ShowDecision\PostTypeStrategy; + + +class TestPostTypeStrategy extends TestCase { + const valid_post_type = 'test_post_type'; + const invalid_post_type = 'invalid'; + + protected function setUp() { + require_once __DIR__ . DIRECTORY_SEPARATOR . 'Stub' . DIRECTORY_SEPARATOR . 'WP_Post.php'; + parent::setUp(); + \WP_Mock::setUp(); + } + + protected function tearDown() { + parent::tearDown(); + \WP_Mock::tearDown(); + } + + public function testInvalidPostTypeFailure() { + $strategy = new PostTypeStrategy( self::invalid_post_type ); + $this->assertFalse( $strategy->shouldDisplay() ); + } + + public function testPostTypeUsingGetSuccess() { + $_GET = [ 'post_type' => self::valid_post_type ]; + $strategy = new PostTypeStrategy( self::valid_post_type ); + $this->assertTrue( $strategy->shouldDisplay() ); + } + + public function testPostTypeUsingPostSuccess() { + $post_id = 123; + $_GET = [ + 'post' => $post_id + ]; + \WP_Mock::userFunction('get_post', [ + 'times' => 1, + 'return' => function() use ($post_id) { + $post = new \WP_Post(); + $post->ID = $post_id; + $post->post_type = self::valid_post_type; + return $post; + } + ]); + + $strategy = new PostTypeStrategy( self::valid_post_type ); + $this->assertTrue( $strategy->shouldDisplay() ); + } + +}