Skip to content
Snippets Groups Projects
Commit 3c368084 authored by dyszczo's avatar dyszczo
Browse files

tests

parent df83c621
No related branches found
No related tags found
1 merge request!1Feature/init
<?php
class WP_Post {
public $ID;
public $post_type;
}
\ No newline at end of file
<?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
<?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
<?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() );
}
}
<?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() );
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment