Skip to content

Commit

Permalink
Start adding block phpunit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
jrtashjian committed Aug 29, 2023
1 parent 3d12786 commit c9318fc
Show file tree
Hide file tree
Showing 12 changed files with 442 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,14 @@
/**
* Tests the BaseControlBlock class.
*/
class BaseControlBlockTest extends BlockTestCase {
class BaseControlBlockTest extends FormBlockTestCase {
/**
* The block instance to test against.
*
* @var \OmniForm\BlockLibrary\Blocks\BaseControlBlock
*/
protected $block_instance;

/**
* Register the block to test against.
*/
Expand Down
44 changes: 44 additions & 0 deletions phpunit/includes/BlockLibrary/Blocks/ButtonTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Tests the Button class.
*
* @package OmniForm
*/

namespace OmniForm\Tests\BlockLibrary\Blocks;

use OmniForm\BlockLibrary\Blocks\Button;

/**
* Tests the Button class.
*/
class ButtonTest extends FormBlockTestCase {
/**
* Register the block to test against.
*/
public function set_up() {
$this->register_block_type( new ButtonBlock() );
}

/**
* Make sure the block does not render markup if the buttonLabel attribute is empty.
*/
public function test_does_not_render_without_button_label() {
$this->assertEmpty( $this->render_block_with_attributes() );

$this->assertNotEmpty(
$this->render_block_with_attributes(
array(
'buttonLabel' => 'button label',
)
)
);
}
}

// phpcs:disable
class ButtonBlock extends Button {
public function block_type_metadata() {
return 'omniform/' . $this->block_type_name();
}
}
52 changes: 52 additions & 0 deletions phpunit/includes/BlockLibrary/Blocks/CaptchaTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php
/**
* Tests the Captcha class.
*
* @package OmniForm
*/

namespace OmniForm\Tests\BlockLibrary\Blocks;

use OmniForm\BlockLibrary\Blocks\Captcha;

/**
* Tests the Captcha class.
*/
class CaptchaTest extends FormBlockTestCase {
/**
* Register the block to test against.
*/
public function set_up() {
$this->register_block_type( new CaptchaBlock() );
}

/**
* Make sure the block does not render markup if the service attribute is empty.
*/
public function test_does_not_render_without_service() {
$this->assertEmpty( $this->render_block_with_attributes() );

$this->assertNotEmpty(
$this->render_block_with_attributes(
array(
'service' => 'hcaptcha',
)
)
);

$this->assertEmpty(
$this->render_block_with_attributes(
array(
'service' => 'none-existant-service',
)
)
);
}
}

// phpcs:disable
class CaptchaBlock extends Captcha {
public function block_type_metadata() {
return 'omniform/' . $this->block_type_name();
}
}
44 changes: 44 additions & 0 deletions phpunit/includes/BlockLibrary/Blocks/FieldTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Tests the Field class.
*
* @package OmniForm
*/

namespace OmniForm\Tests\BlockLibrary\Blocks;

use OmniForm\BlockLibrary\Blocks\Field;

/**
* Tests the Field class.
*/
class FieldTest extends FormBlockTestCase {
/**
* Register the block to test against.
*/
public function set_up() {
$this->register_block_type( new FieldBlock() );
}

/**
* Make sure the block does not render markup if the fieldLabel attribute is empty.
*/
public function test_does_not_render_without_field_label() {
$this->assertEmpty( $this->render_block_with_attributes() );

$this->assertNotEmpty(
$this->render_block_with_attributes(
array(
'fieldLabel' => 'field label',
)
)
);
}
}

// phpcs:disable
class FieldBlock extends Field {
public function block_type_metadata() {
return 'omniform/' . $this->block_type_name();
}
}
44 changes: 44 additions & 0 deletions phpunit/includes/BlockLibrary/Blocks/FieldsetTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Tests the Fieldset class.
*
* @package OmniForm
*/

namespace OmniForm\Tests\BlockLibrary\Blocks;

use OmniForm\BlockLibrary\Blocks\Fieldset;

/**
* Tests the Fieldset class.
*/
class FieldsetTest extends FormBlockTestCase {
/**
* Register the block to test against.
*/
public function set_up() {
$this->register_block_type( new FieldsetBlock() );
}

/**
* Make sure the block does not render markup if the fieldLabel attribute is empty.
*/
public function test_does_not_render_without_field_label() {
$this->assertEmpty( $this->render_block_with_attributes() );

$this->assertNotEmpty(
$this->render_block_with_attributes(
array(
'fieldLabel' => 'field label',
)
)
);
}
}

// phpcs:disable
class FieldsetBlock extends Fieldset {
public function block_type_metadata() {
return 'omniform/' . $this->block_type_name();
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
<?php
/**
* Tests the BlockTestCase class.
* Tests the FormBlockTestCase class.
*
* @package OmniForm
*/

namespace OmniForm\Tests\BlockLibrary\Blocks;

/**
* Tests the BlockTestCase class.
* Tests the FormBlockTestCase class.
*/
class BlockTestCase extends \WP_UnitTestCase {
class FormBlockTestCase extends \WP_UnitTestCase {
/**
* The block instance to test against.
*
* @var \OmniForm\BlockLibrary\Blocks\BaseControlBlock
* @var \OmniForm\BlockLibrary\Blocks\FormBlockInterface
*/
protected $block_instance;

Expand All @@ -28,9 +28,9 @@ class BlockTestCase extends \WP_UnitTestCase {
/**
* Register the block to test against.
*
* @param \OmniForm\BlockLibrary\Blocks\BaseControlBlock $block_object The block object to test against.
* @param \OmniForm\BlockLibrary\Blocks\FormBlockInterface $block_object The block object to test against.
*/
protected function register_block_type( \OmniForm\BlockLibrary\Blocks\BaseControlBlock $block_object ) {
protected function register_block_type( \OmniForm\BlockLibrary\Blocks\FormBlockInterface $block_object ) {
$this->block_type_name = $block_object->block_type_metadata();

register_block_type(
Expand Down
39 changes: 39 additions & 0 deletions phpunit/includes/BlockLibrary/Blocks/InputTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Tests the Input class.
*
* @package OmniForm
*/

namespace OmniForm\Tests\BlockLibrary\Blocks;

use OmniForm\BlockLibrary\Blocks\Input;

/**
* Tests the Input class.
*/
class InputTest extends FormBlockTestCase {
/**
* Register the block to test against.
*/
public function set_up() {
$this->register_block_type( new InputBlock() );
}

/**
* Make sure the block does not render markup if the fieldLabel attribute is empty.
*/
public function test_does_not_render_without_field_label() {
$this->assertEmpty( $this->render_block_with_attributes() );

$this->apply_block_context( 'omniform/fieldLabel', 'field label' );
$this->assertNotEmpty( $this->render_block_with_attributes() );
}
}

// phpcs:disable
class InputBlock extends Input {
public function block_type_metadata() {
return 'omniform/' . $this->block_type_name();
}
}
39 changes: 39 additions & 0 deletions phpunit/includes/BlockLibrary/Blocks/LabelTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/**
* Tests the Label class.
*
* @package OmniForm
*/

namespace OmniForm\Tests\BlockLibrary\Blocks;

use OmniForm\BlockLibrary\Blocks\Label;

/**
* Tests the Label class.
*/
class LabelTest extends FormBlockTestCase {
/**
* Register the block to test against.
*/
public function set_up() {
$this->register_block_type( new LabelBlock() );
}

/**
* Make sure the block does not render markup if the fieldLabel attribute is empty.
*/
public function test_does_not_render_without_field_label() {
$this->assertEmpty( $this->render_block_with_attributes() );

$this->apply_block_context( 'omniform/fieldLabel', 'field label' );
$this->assertNotEmpty( $this->render_block_with_attributes() );
}
}

// phpcs:disable
class LabelBlock extends Label {
public function block_type_metadata() {
return 'omniform/' . $this->block_type_name();
}
}
44 changes: 44 additions & 0 deletions phpunit/includes/BlockLibrary/Blocks/SelectGroupTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Tests the SelectGroup class.
*
* @package OmniForm
*/

namespace OmniForm\Tests\BlockLibrary\Blocks;

use OmniForm\BlockLibrary\Blocks\SelectGroup;

/**
* Tests the SelectGroup class.
*/
class SelectGroupTest extends FormBlockTestCase {
/**
* Register the block to test against.
*/
public function set_up() {
$this->register_block_type( new SelectGroupBlock() );
}

/**
* Make sure the block does not render markup if the fieldLabel attribute is empty.
*/
public function test_does_not_render_without_field_label() {
$this->assertEmpty( $this->render_block_with_attributes() );

$this->assertNotEmpty(
$this->render_block_with_attributes(
array(
'fieldLabel' => 'field label',
)
)
);
}
}

// phpcs:disable
class SelectGroupBlock extends SelectGroup {
public function block_type_metadata() {
return 'omniform/' . $this->block_type_name();
}
}
Loading

0 comments on commit c9318fc

Please sign in to comment.