109 lines
3.7 KiB
PHP
109 lines
3.7 KiB
PHP
<?php
|
|
declare(strict_types=1);
|
|
|
|
namespace WPMultiCity\Tests;
|
|
|
|
use WPMultiCity\ShortcodeDmr;
|
|
|
|
final class ShortcodeDmrTest extends TestCase
|
|
{
|
|
public function test_register_attaches_init_action(): void
|
|
{
|
|
ShortcodeDmr::register();
|
|
|
|
self::assertNotFalse(
|
|
has_action('init', [ShortcodeDmr::class, 'do_register']),
|
|
'ShortcodeDmr::register() must hook init'
|
|
);
|
|
}
|
|
|
|
public function test_do_register_registers_shortcode(): void
|
|
{
|
|
\Brain\Monkey\Functions\expect('add_shortcode')
|
|
->once()
|
|
->with('shortcode_dmr', [ShortcodeDmr::class, 'render']);
|
|
|
|
ShortcodeDmr::do_register();
|
|
|
|
self::assertTrue(true);
|
|
}
|
|
|
|
public function test_render_returns_text_when_row_matches_current_town(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/tyumen/';
|
|
\Brain\Monkey\Functions\when('get_terms')->justReturn([10 => 'spb', 11 => 'tyumen']);
|
|
|
|
$term = new \WP_Term();
|
|
$term->term_id = 11;
|
|
$term->slug = 'tyumen';
|
|
\Brain\Monkey\Functions\when('get_term_by')->alias(function ($field, $slug, $tax) use ($term) {
|
|
return ($field === 'slug' && $slug === 'tyumen' && $tax === 'town') ? $term : false;
|
|
});
|
|
|
|
\Brain\Monkey\Functions\expect('get_field')
|
|
->once()
|
|
->with('list_shortcode', 42)
|
|
->andReturn([
|
|
['town' => 10, 'text' => 'СПб-текст'],
|
|
['town' => 11, 'text' => 'Тюмень-текст'],
|
|
]);
|
|
|
|
$result = ShortcodeDmr::render(['id' => 42]);
|
|
|
|
self::assertSame('Тюмень-текст', $result);
|
|
}
|
|
|
|
public function test_render_returns_empty_when_id_is_zero(): void
|
|
{
|
|
\Brain\Monkey\Functions\expect('get_field')->never();
|
|
self::assertSame('', ShortcodeDmr::render(['id' => 0]));
|
|
}
|
|
|
|
public function test_render_returns_empty_when_current_town_is_null(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/no-matching-segment/';
|
|
\Brain\Monkey\Functions\when('get_terms')->justReturn([10 => 'spb']);
|
|
\Brain\Monkey\Functions\expect('get_field')->never();
|
|
|
|
self::assertSame('', ShortcodeDmr::render(['id' => 42]));
|
|
}
|
|
|
|
public function test_render_returns_empty_when_term_lookup_fails(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/tyumen/';
|
|
\Brain\Monkey\Functions\when('get_terms')->justReturn([11 => 'tyumen']);
|
|
\Brain\Monkey\Functions\when('get_term_by')->justReturn(false);
|
|
\Brain\Monkey\Functions\expect('get_field')->never();
|
|
|
|
self::assertSame('', ShortcodeDmr::render(['id' => 42]));
|
|
}
|
|
|
|
public function test_render_returns_empty_when_rows_are_empty(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/tyumen/';
|
|
\Brain\Monkey\Functions\when('get_terms')->justReturn([11 => 'tyumen']);
|
|
$term = new \WP_Term();
|
|
$term->term_id = 11;
|
|
$term->slug = 'tyumen';
|
|
\Brain\Monkey\Functions\when('get_term_by')->justReturn($term);
|
|
\Brain\Monkey\Functions\expect('get_field')->once()->andReturn([]);
|
|
|
|
self::assertSame('', ShortcodeDmr::render(['id' => 42]));
|
|
}
|
|
|
|
public function test_render_returns_empty_when_no_row_matches(): void
|
|
{
|
|
$_SERVER['REQUEST_URI'] = '/tyumen/';
|
|
\Brain\Monkey\Functions\when('get_terms')->justReturn([10 => 'spb', 11 => 'tyumen']);
|
|
$term = new \WP_Term();
|
|
$term->term_id = 11;
|
|
$term->slug = 'tyumen';
|
|
\Brain\Monkey\Functions\when('get_term_by')->justReturn($term);
|
|
\Brain\Monkey\Functions\expect('get_field')
|
|
->once()
|
|
->andReturn([['town' => 10, 'text' => 'Только СПб']]);
|
|
|
|
self::assertSame('', ShortcodeDmr::render(['id' => 42]));
|
|
}
|
|
}
|