11bfaa08a0
Re-add ->never() to both noop tests so Brain Monkey/Mockery actively asserts
acf_add_local_field_group is never called when ACF is absent. Order matters:
expect() must precede when('function_exists')->alias() so FunctionStub sees the
real function_exists and skips the eval() path (function already declared in
acf-stubs.php). Also tighten stub signature to match real ACF ($field_group,
no type hint, no default).
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
82 lines
2.9 KiB
PHP
82 lines
2.9 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace WPMultiCity\Tests;
|
|
|
|
use WPMultiCity\FieldGroup;
|
|
|
|
final class FieldGroupTest extends TestCase
|
|
{
|
|
public function test_register_attaches_acf_init_action(): void
|
|
{
|
|
FieldGroup::register();
|
|
|
|
self::assertNotFalse(
|
|
has_action('acf/init', [FieldGroup::class, 'do_register']),
|
|
'FieldGroup::register() must hook acf/init'
|
|
);
|
|
}
|
|
|
|
public function test_do_register_is_noop_when_acf_missing(): void
|
|
{
|
|
// Set up the ->never() expectation BEFORE aliasing function_exists, so that
|
|
// FunctionStub::__construct() sees the real function_exists and skips the
|
|
// eval() path (the function is already declared in acf-stubs.php).
|
|
\Brain\Monkey\Functions\expect('acf_add_local_field_group')->never();
|
|
\Brain\Monkey\Functions\when('function_exists')->alias(function ($name) {
|
|
return $name !== 'acf_add_local_field_group';
|
|
});
|
|
|
|
FieldGroup::do_register();
|
|
|
|
// The real check is the ->never() Mockery expectation above (verified at
|
|
// teardown). assertTrue is here only to keep PHPUnit from flagging the
|
|
// test as risky under failOnRisky="true" — Mockery assertions don't
|
|
// count toward PHPUnit's assertion tally.
|
|
self::assertTrue(true);
|
|
}
|
|
|
|
public function test_do_register_calls_acf_add_local_field_group_with_expected_structure(): void
|
|
{
|
|
\Brain\Monkey\Functions\when('function_exists')->alias(function ($name) {
|
|
return $name === 'acf_add_local_field_group';
|
|
});
|
|
|
|
$captured = null;
|
|
\Brain\Monkey\Functions\expect('acf_add_local_field_group')
|
|
->once()
|
|
->with(\Mockery::on(function ($group) use (&$captured) {
|
|
$captured = $group;
|
|
return true;
|
|
}));
|
|
|
|
FieldGroup::do_register();
|
|
|
|
self::assertSame('group_wpmc_shortcode_rows', $captured['key']);
|
|
self::assertSame('WPMultiCity / Shortcode rows', $captured['title']);
|
|
|
|
self::assertCount(1, $captured['fields']);
|
|
$repeater = $captured['fields'][0];
|
|
|
|
self::assertSame('field_wpmc_list_shortcode', $repeater['key']);
|
|
self::assertSame('list_shortcode', $repeater['name']);
|
|
self::assertSame('repeater', $repeater['type']);
|
|
self::assertCount(2, $repeater['sub_fields']);
|
|
|
|
[$town, $text] = $repeater['sub_fields'];
|
|
self::assertSame('town', $town['name']);
|
|
self::assertSame('taxonomy', $town['type']);
|
|
self::assertSame('town', $town['taxonomy']);
|
|
self::assertSame('id', $town['return_format']);
|
|
self::assertSame(0, $town['allow_null']);
|
|
|
|
self::assertSame('text', $text['name']);
|
|
self::assertSame('textarea', $text['type']);
|
|
|
|
self::assertSame(
|
|
[[['param' => 'post_type', 'operator' => '==', 'value' => 'shortcode-town']]],
|
|
$captured['location']
|
|
);
|
|
}
|
|
}
|