Skip to content

Commit

Permalink
Start developing in situ PHPUnit tests #15
Browse files Browse the repository at this point in the history
  • Loading branch information
bobbingwide committed Aug 17, 2023
1 parent 7cfa189 commit 4c18bd6
Show file tree
Hide file tree
Showing 5 changed files with 245 additions and 0 deletions.
1 change: 1 addition & 0 deletions .phpunit.result.cache
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"version":1,"defects":{"Tests_text_field::test_text_field_render":5,"Tests_text_field::test_text_field_render_post_no_field":3,"Tests_text_field::test_text_field_render_no_post_no_field":3},"times":{"Tests_text_field::test_text_field_render":0.125,"Tests_text_field::test_text_field_render_no_post_no_field":0.108,"Tests_text_field::test_text_field_render_post_no_field":0.207}}
44 changes: 44 additions & 0 deletions phpunit.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?xml version="1.0" encoding="UTF-8"?>
<testsuites>
<testsuite name="" tests="2" assertions="9" errors="0" warnings="0" failures="1" skipped="0" time="0.314422">
<testsuite name="acf-field-block" tests="2" assertions="9" errors="0" warnings="0" failures="1" skipped="0" time="0.314422">
<testsuite name="Tests_text_field" file="C:\apache\htdocs\wordpress\wp-content\plugins\acf-field-block\tests\test-text-field.php" tests="2" assertions="9" errors="0" warnings="0" failures="1" skipped="0" time="0.314422">
<testcase name="test_text_field_render_no_post_no_field" class="Tests_text_field" classname="Tests_text_field" file="C:\apache\htdocs\wordpress\wp-content\plugins\acf-field-block\tests\test-text-field.php" line="41" assertions="6" time="0.107882"/>
<testcase name="test_text_field_render_post_no_field" class="Tests_text_field" classname="Tests_text_field" file="C:\apache\htdocs\wordpress\wp-content\plugins\acf-field-block\tests\test-text-field.php" line="72" assertions="3" time="0.206539">
<failure type="PHPUnit\Framework\ExpectationFailedException">Tests_text_field::test_text_field_render_post_no_field
Failed asserting that file "tests/data/en_US/test_text_field_render_post_no_field.html" exists.

C:\apache\htdocs\wordpress\wp-content\plugins\oik-batch\tests\class-bw-unittestcase.php:274
C:\apache\htdocs\wordpress\wp-content\plugins\oik-batch\tests\class-bw-unittestcase.php:327
C:\apache\htdocs\wordpress\wp-content\plugins\acf-field-block\tests\test-text-field.php:81</failure>
<system-out>Array
(
[key] =&gt;
[label] =&gt; Label text
[name] =&gt; name_text
[aria-label] =&gt;
[type] =&gt; text
[instructions] =&gt;
[required] =&gt; 1
[conditional_logic] =&gt; 0
[wrapper] =&gt; Array
(
[width] =&gt;
[class] =&gt;
[id] =&gt;
)

[default_value] =&gt;
[maxlength] =&gt;
[placeholder] =&gt;
[prepend] =&gt;
[append] =&gt;
)
&lt;!-- wp:acf-field/acf-field {"name":"acf-field/acf-field","data":{"acf-field-name":"name_text"},"mode":"preview"} /--&gt;&#13;
&lt;p&gt;Field not set for post ID 19189&lt;/p&gt;&#13;
</system-out>
</testcase>
</testsuite>
</testsuite>
</testsuite>
</testsuites>
14 changes: 14 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<phpunit
bootstrap="../../plugins/oik-batch/oik-wp.php"
backupGlobals="false"
colors="false"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
>
<testsuites>
<testsuite name="acf-field-block">
<directory prefix="test-" suffix=".php">tests/</directory>
</testsuite>
</testsuites>
</phpunit>
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<p>Field name_text not set for post ID 0</p>
185 changes: 185 additions & 0 deletions tests/test-text-field.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,185 @@
<?php

/**
* @package oik-bob-bing-wide
* @copyright (C) Bobbing Wide 2023
*
* Test the functions in shortcodes/oik-dash.php
*/
class Tests_text_field extends BW_UnitTestCase {

function setUp() : void {
parent::setUp();
}

/**
*
* Calls the logic to render an ACF field block
* and compares the generated HTML with expected.
*
* We need to
* - register the field group with the required field type
* - create a post with the post meta set
* - render the acf-field block
*
* ```
* <!-- wp:acf-field/acf-field {
* "name":"acf-field/acf-field",
* "data":{"acf-field-name":"text","_acf-field-name":"field_645f589a88304"},
* "mode":"preview"}
* /-->
* ```
*/

/**
* Tests the text field type when there's no global post
* and the post_meta's not set.
*
* @return void
*
*/
function test_text_field_render_no_post_no_field() {
$field = $this->maybe_create_field_group( 'text');
$content = $this->create_acf_field_block( $field );
$html = do_blocks( $content );
//echo $output;
//$this->generate_expected_file( $html );
$this->assertArrayEqualsFile( $html );
//$this->assertEquals( $icons[0], "menu" );
//$this->assertEquals( $icons[0], "admin-appearance");

}

/**
* Creates the field group if necessary.
*
* @param $field_type
*
* @return array
*/
function maybe_create_field_group( $field_type ) {
$field = $this->create_test_field( $field_type);
$this->assertIsArray( $field );
$field_group = $this->create_test_field_group( $field_type );
return $field;

}

/**
* Tests the field type text when the fields not set for the post.
* @return void
*/
function test_text_field_render_post_no_field() {
//$field = $this->maybe_create_field_group( 'text');
$field = $this->create_test_field( 'text');
print_r( $field );
$content = $this->create_acf_field_block( $field );
echo $content;
$this->set_dummy_post();
$html = do_blocks( $content );
//echo $output;
$this->generate_expected_file( $html );
$this->assertArrayEqualsFile( $html );
//$this->assertEquals( $icons[0], "menu" );
//$this->assertEquals( $icons[0], "admin-appearance");
}

function set_dummy_post() {
global $post;
$post = $this->dummy_post( 1 );
}

function dummy_post( $n, $parent=0 ) {
$args = array( 'post_type' => 'post', 'post_title' => "post title $n", 'post_excerpt' => 'Excerpt. No post ID', 'post_parent' => $parent );
$id = self::factory()->post->create( $args );
$post = get_post( $id );
$GLOBALS['post'] = null;
$GLOBALS['id'] = 0;
return $post;
}


function test_text_field_render() {
$field = $this->create_test_field( 'text');
$this->assertIsArray( $field );
$field_group = $this->create_test_field_group( 'text');
$content = $this->create_acf_field_block( $field );
$html = do_blocks( $content );
//echo $output;
//$this->generate_expected_file( $html );
$this->assertArrayEqualsFile( $html );
//$this->assertEquals( $icons[0], "menu" );
//$this->assertEquals( $icons[0], "admin-appearance");

}

function create_test_field_group( $field_type ) {
$acf_field = $this->create_test_field( $field_type );
//register_field_group( array(
$registered = acf_add_local_field_group( array(
/* 'key' =>'group_645e28e943198', */
'title' =>'test_field_group',
'fields' =>array( $acf_field ),
'location' =>array(
array(
array(
'param' =>'post_type',
'operator'=>'==',
'value' =>'oik-testimonials/acf-cycler',
),
),
),
'menu_order' =>0,
'position' =>'normal',
'style' =>'default',
'label_placement' =>'top',
'instruction_placement'=>'label',
'hide_on_screen' =>'',
'active' =>true,
'description' =>'',
'show_in_rest' =>0,
) );
$this->assertTrue( $registered);

}

function create_test_field( $type ) {
$acf_field =
array(
'key' => '',
'label' => 'Label ' . $type,
'name' => 'name_' . $type,
'aria-label' =>'',
'type' => $type,
'instructions' =>'',
'required' =>1,
'conditional_logic'=>0,
'wrapper' =>array(
'width'=>'',
'class'=>'',
'id' =>'',
),
'default_value' =>'',
'maxlength' =>'',
'placeholder' =>'',
'prepend' =>'',
'append' =>'',
);

return $acf_field;
}

function create_acf_field_block( $field ) {
$block = '<!-- wp:acf-field/acf-field {';
$block .= '"name":"acf-field/acf-field",';
$block .= '"data":{"acf-field-name":"';
$block .= $field['name'];
$block .= '"},"mode":"preview"}';
$block .= ' /-->';
return $block;

}


}

0 comments on commit 4c18bd6

Please sign in to comment.