From 7bdf758f6292661b9158db274aa92b7902cde323 Mon Sep 17 00:00:00 2001 From: Bart Jaskulski <bjaskulski@protonmail.com> Date: Mon, 16 Oct 2023 21:59:27 +0200 Subject: [PATCH] refactor: more tests coverage, remove phpunit deprecations Signed-off-by: Bart Jaskulski <bjaskulski@protonmail.com> --- ...child-template.php => nested-template.php} | 0 tests/Fixtures/root-template.php | 2 +- tests/Fixtures/simple.php | 2 + tests/Fixtures/some_template.php | 1 + .../theme-template/child-template.php | 0 tests/Fixtures/with-parameter.php | 1 + tests/Fixtures/with-variable.php | 1 + tests/Renderer/TestSimplePhpRenderer.php | 67 ++++++++--- tests/Renderer/templates/some_template.php | 2 - tests/Resolver/TestChainResolver.php | 15 +-- tests/Resolver/TestDirResolver.php | 50 ++++---- tests/Resolver/TestWpThemeResolver.php | 111 ++++++++---------- tests/Resolver/templates/some_template.php | 2 +- tests/TestCase.php | 31 +++++ tests/TestPluginViewBuilder.php | 22 +--- 15 files changed, 164 insertions(+), 143 deletions(-) rename tests/Fixtures/{child-template.php => nested-template.php} (100%) create mode 100644 tests/Fixtures/simple.php create mode 100644 tests/Fixtures/some_template.php create mode 100644 tests/Fixtures/theme-template/child-template.php create mode 100644 tests/Fixtures/with-parameter.php create mode 100644 tests/Fixtures/with-variable.php delete mode 100644 tests/Renderer/templates/some_template.php create mode 100644 tests/TestCase.php diff --git a/tests/Fixtures/child-template.php b/tests/Fixtures/nested-template.php similarity index 100% rename from tests/Fixtures/child-template.php rename to tests/Fixtures/nested-template.php diff --git a/tests/Fixtures/root-template.php b/tests/Fixtures/root-template.php index b4e0f03..a88c0b2 100644 --- a/tests/Fixtures/root-template.php +++ b/tests/Fixtures/root-template.php @@ -1 +1 @@ -This is a content of nested template: <?php $this->output_render('child-template'); ?> +This is a content of nested template: <?php $this->output_render('nested-template'); ?> diff --git a/tests/Fixtures/simple.php b/tests/Fixtures/simple.php new file mode 100644 index 0000000..50503d9 --- /dev/null +++ b/tests/Fixtures/simple.php @@ -0,0 +1,2 @@ +<?php + echo 'outputText'; diff --git a/tests/Fixtures/some_template.php b/tests/Fixtures/some_template.php new file mode 100644 index 0000000..d5dc4cf --- /dev/null +++ b/tests/Fixtures/some_template.php @@ -0,0 +1 @@ +this is dummy text diff --git a/tests/Fixtures/theme-template/child-template.php b/tests/Fixtures/theme-template/child-template.php new file mode 100644 index 0000000..e69de29 diff --git a/tests/Fixtures/with-parameter.php b/tests/Fixtures/with-parameter.php new file mode 100644 index 0000000..6d12c57 --- /dev/null +++ b/tests/Fixtures/with-parameter.php @@ -0,0 +1 @@ +passed parameter: <?php echo $params['custom_parameter']; diff --git a/tests/Fixtures/with-variable.php b/tests/Fixtures/with-variable.php new file mode 100644 index 0000000..f953d67 --- /dev/null +++ b/tests/Fixtures/with-variable.php @@ -0,0 +1 @@ +passed variable: <?php echo $custom_variable; diff --git a/tests/Renderer/TestSimplePhpRenderer.php b/tests/Renderer/TestSimplePhpRenderer.php index b647bea..1cedb6f 100644 --- a/tests/Renderer/TestSimplePhpRenderer.php +++ b/tests/Renderer/TestSimplePhpRenderer.php @@ -1,37 +1,68 @@ <?php -namespace WPDesk\View\Tests; +namespace WPDesk\View\Tests\Resolver; +use WPDesk\View\Renderer\SimplePhpRenderer; use WPDesk\View\Resolver\ChainResolver; use WPDesk\View\Resolver\DirResolver; use WPDesk\View\Resolver\Exception\CanNotResolve; use WPDesk\View\Resolver\NullResolver; +use WPDesk\View\Tests\TestCase; -class TestSimplePhpRenderer extends \PHPUnit\Framework\TestCase +class TestSimplePhpRenderer extends TestCase { - const TEXT_IN_TEMPLATE = 'outputText'; - - const TEMPLATE_NAME = 'some_template'; - - const TEMPLATE_DIR = '/templates'; - public function testThrowsExceptionWhenCannotFindTemplate() { $this->expectException(CanNotResolve::class); - $renderer = new \WPDesk\View\Renderer\SimplePhpRenderer(new DirResolver('')); + $renderer = new SimplePhpRenderer(new DirResolver('whatever')); $renderer->render('anything'); } - public function testRenderWithDirResolver() - { - $renderer = new \WPDesk\View\Renderer\SimplePhpRenderer(new DirResolver(__DIR__ . self::TEMPLATE_DIR)); - $this->assertEquals(self::TEXT_IN_TEMPLATE, $renderer->render(self::TEMPLATE_NAME)); + /** + * @dataProvider templatesWithContent + */ + public function testRenderingTemplates(string $template, string $content, array $parameters): void { + $renderer = new SimplePhpRenderer( new DirResolver(self::getTemplatesPath())); + $this->assertStringContainsString($content, $renderer->render($template, $parameters)); } - public function testCanRenderNestedTemplates(): void - { - $renderer = new \WPDesk\View\Renderer\SimplePhpRenderer(new DirResolver(__DIR__ . '/../Fixtures')); - $this->assertStringContainsString('This is a content of nested template: Hello World!', $renderer->render('root-template')); + /** + * @dataProvider templatesWithContent + */ + public function testOutputtingTemplates(string $template, string $content, array $parameters): void { + $this->expectOutputString($content); + $renderer = new SimplePhpRenderer(new DirResolver(self::getTemplatesPath())); + $renderer->output_render($template, $parameters); + } + + public function templatesWithContent(): iterable { + yield 'simple template' => [ + 'template' => 'simple', + 'content' => 'outputText', + 'parameters' => [] + ]; + + yield 'nested template' => [ + 'template' => 'root-template', + 'content' => "This is a content of nested template: Hello World!\n", + 'parameters' => [] + ]; + + yield 'access parameters with $param variable' => [ + 'template' => 'with-parameter', + 'content' => 'passed parameter: custom_parameter', + 'parameters' => [ + 'custom_parameter' => 'custom_parameter' + ] + ]; + + yield 'access parameters directly through variable' => [ + 'template' => 'with-variable', + 'content' => 'passed variable: custom_variable', + 'parameters' => [ + 'custom_variable' => 'custom_variable' + ] + ]; } -} \ No newline at end of file +} diff --git a/tests/Renderer/templates/some_template.php b/tests/Renderer/templates/some_template.php deleted file mode 100644 index fcd781a..0000000 --- a/tests/Renderer/templates/some_template.php +++ /dev/null @@ -1,2 +0,0 @@ -<?php - echo 'outputText'; \ No newline at end of file diff --git a/tests/Resolver/TestChainResolver.php b/tests/Resolver/TestChainResolver.php index b68c1f9..0a8012e 100644 --- a/tests/Resolver/TestChainResolver.php +++ b/tests/Resolver/TestChainResolver.php @@ -1,27 +1,18 @@ <?php -namespace WPDesk\View\Tests; +namespace WPDesk\View\Tests\Resolver; use WPDesk\View\Resolver\ChainResolver; use WPDesk\View\Resolver\Exception\CanNotResolve; use WPDesk\View\Resolver\NullResolver; +use WPDesk\View\Tests\TestCase; -class TestChainResolver extends \PHPUnit\Framework\TestCase +class TestChainResolver extends TestCase { const RESPONSE_OF_RESOLVER = 'response'; const RESOLVE_METHOD_NAME = 'resolve'; - public function setUp(): void - { - \WP_Mock::setUp(); - } - - public function tearDown(): void - { - \WP_Mock::tearDown(); - } - public function testUseSecondResolverWhenFirstFailed() { $validResolver = \Mockery::mock(NullResolver::class); diff --git a/tests/Resolver/TestDirResolver.php b/tests/Resolver/TestDirResolver.php index 42cfcfa..96e254a 100644 --- a/tests/Resolver/TestDirResolver.php +++ b/tests/Resolver/TestDirResolver.php @@ -1,39 +1,35 @@ <?php -namespace WPDesk\View\Tests; +namespace WPDesk\View\Tests\Resolver; +use WPDesk\View\Resolver\DirResolver; use WPDesk\View\Resolver\Exception\CanNotResolve; +use WPDesk\View\Tests\TestCase; -class TestDirResolver extends \PHPUnit\Framework\TestCase -{ - const TEMPLATE_NAME = 'some_template.php'; - const TEMPLATE_FILE = 'some_template.php'; - const TEMPLATE_SUBDIR = 'templates'; +class TestDirResolver extends TestCase { - public function setUp(): void - { - \WP_Mock::setUp(); - } + public function testCanFindInDirPath(): void { + $resolver = new DirResolver(self::getTemplatesPath()); - public function tearDown(): void - { - \WP_Mock::tearDown(); + $this->assertEquals( + self::getTemplatesPath() . '/some_template.php', + $resolver->resolve('some_template.php'), + 'Template should be found in dir' + ); } - public function testCanFindInDirPath() - { - $dir = __DIR__ . '/' . self::TEMPLATE_SUBDIR; - $resolver = new \WPDesk\View\Resolver\DirResolver($dir); + public function testThrowExceptionWhenCannotFind(): void { + $this->expectException(CanNotResolve::class); - $this->assertStringEndsWith(self::TEMPLATE_FILE, $resolver->resolve(self::TEMPLATE_NAME), - 'Template should be found in dir'); - } + $resolver = new DirResolver('whatever'); + $resolver->resolve('whatever2'); + } - public function testThrowExceptionWhenCannotFind() - { - $this->expectException(CanNotResolve::class); + public function testResolvingFromCurrentPath(): void { + $this->expectException(CanNotResolve::class); + $this->expectExceptionMessage('Denying to search in system\'s root path.'); - $resolver = new \WPDesk\View\Resolver\DirResolver('whatever'); - $resolver->resolve('whatever2'); - } -} \ No newline at end of file + $resolver = new DirResolver(null); + $resolver->resolve('whatever2'); + } +} diff --git a/tests/Resolver/TestWpThemeResolver.php b/tests/Resolver/TestWpThemeResolver.php index c3fb0a2..b5b06a5 100644 --- a/tests/Resolver/TestWpThemeResolver.php +++ b/tests/Resolver/TestWpThemeResolver.php @@ -1,70 +1,53 @@ <?php -namespace WPDesk\View\Tests; +namespace WPDesk\View\Tests\Resolver; use WPDesk\View\Resolver\Exception\CanNotResolve; +use WPDesk\View\Resolver\WPThemeResolver; +use WPDesk\View\Tests\TestCase; -class TestThemeResolver extends \PHPUnit\Framework\TestCase +class TestWpThemeResolver extends TestCase { - const TEMPLATE_NAME = 'some_template.php'; - const TEMPLATE_FILE = 'some_template.php'; - const TEMPLATE_SUBDIR = 'templates'; - - public function setUp(): void - { - \WP_Mock::setUp(); - - \WP_Mock::userFunction('locate_template', [ - 'return' => function ($template_names, $load = false, $require_once = true) { - $located = ''; - foreach ((array)$template_names as $template_name) { - if ( ! $template_name) { - continue; - } - if (file_exists(STYLESHEETPATH . '/' . $template_name)) { - $located = STYLESHEETPATH . '/' . $template_name; - break; - } - } - - return $located; - } - ]); - - \WP_Mock::userFunction('trailingslashit', [ - 'return' => function ($string) { - return rtrim($string, '/\\') . '/'; - } - ]); - - \WP_Mock::userFunction('untrailingslashit', [ - 'return' => function ($string) { - return rtrim($string, '/\\'); - } - ]); - } - - public function tearDown(): void - { - \WP_Mock::tearDown(); - } - - public function testCanFindInStyleSheetPath() - { - define('STYLESHEETPATH', __DIR__); - - $template_base_path = self::TEMPLATE_SUBDIR; - $resolver = new \WPDesk\View\Resolver\WPThemeResolver($template_base_path); - - $this->assertStringEndsWith(self::TEMPLATE_FILE, $resolver->resolve(self::TEMPLATE_NAME), - 'Template should be found in stylesheetpath'); - } - - public function testThrowExceptionWhenCannotFind() - { - $this->expectException(CanNotResolve::class); - - $resolver = new \WPDesk\View\Resolver\WPThemeResolver('whatever'); - $resolver->resolve('whatever2'); - } -} \ No newline at end of file + public function setUp(): void + { + parent::setUp(); + + \WP_Mock::userFunction('locate_template', [ + 'return' => function ($template_names, $load = false, $require_once = true) { + $located = ''; + foreach ((array)$template_names as $template_name) { + if ( ! $template_name) { + continue; + } + if (file_exists(STYLESHEETPATH . '/' . $template_name)) { + $located = STYLESHEETPATH . '/' . $template_name; + break; + } + } + + return $located; + } + ]); + } + + public function testCanFindInStyleSheetPath() + { + define('STYLESHEETPATH', self::getTemplatesPath()); + + $resolver = new WPThemeResolver('theme-template'); + + $this->assertEquals( + self::getTemplatesPath() . '/theme-template/child-template.php', + $resolver->resolve('child-template.php'), + 'Template should be found in stylesheetpath' + ); + } + + public function testThrowExceptionWhenCannotFind() + { + $this->expectException(CanNotResolve::class); + + $resolver = new WPThemeResolver('whatever'); + $resolver->resolve('whatever2'); + } +} diff --git a/tests/Resolver/templates/some_template.php b/tests/Resolver/templates/some_template.php index dd6143c..d5dc4cf 100644 --- a/tests/Resolver/templates/some_template.php +++ b/tests/Resolver/templates/some_template.php @@ -1 +1 @@ -this is dummy text \ No newline at end of file +this is dummy text diff --git a/tests/TestCase.php b/tests/TestCase.php new file mode 100644 index 0000000..8508c07 --- /dev/null +++ b/tests/TestCase.php @@ -0,0 +1,31 @@ +<?php + +namespace WPDesk\View\Tests; + +abstract class TestCase extends \PHPUnit\Framework\TestCase { + + public function setUp(): void { + \WP_Mock::setUp(); + + \WP_Mock::userFunction('trailingslashit', [ + 'return' => function ($string) { + return rtrim($string, '/\\') . '/'; + } + ]); + + \WP_Mock::userFunction('untrailingslashit', [ + 'return' => function ($string) { + return rtrim($string, '/\\'); + } + ]); + } + + public function tearDown(): void { + \WP_Mock::tearDown(); + } + + + public static function getTemplatesPath(): string { + return __DIR__ . '/Fixtures'; + } +} diff --git a/tests/TestPluginViewBuilder.php b/tests/TestPluginViewBuilder.php index 9b9ea8c..b58a479 100644 --- a/tests/TestPluginViewBuilder.php +++ b/tests/TestPluginViewBuilder.php @@ -4,21 +4,7 @@ namespace WPDesk\View\Tests; use WPDesk\View\PluginViewBuilder; -class TestPluginViewBuilder extends \PHPUnit\Framework\TestCase { - public function setUp(): void { - \WP_Mock::setUp(); - - \WP_Mock::userFunction('trailingslashit', [ - 'return' => function ($string) { - return rtrim($string, '/\\') . '/'; - } - ]); - } - - public function tearDown(): void - { - \WP_Mock::tearDown(); - } +class TestPluginViewBuilder extends TestCase { public function testCanRenderUsingDir() { $builder = new PluginViewBuilder( __DIR__ . '/Fixtures', 'template' ); @@ -27,11 +13,11 @@ class TestPluginViewBuilder extends \PHPUnit\Framework\TestCase { $val = 'val to render'; $args = [ 'singleArg' => $val ]; $content = $renderer->render( 'file', $args ); - $this->assertRegExp( '/template content/', $content, 'Content from Fixtures/template/file.php should be renderer' ); - $this->assertRegExp( "/{$val}/", $content, 'Content from Fixtures/template/file.php should contain $val' ); + $this->assertMatchesRegularExpression( '/template content/', $content, 'Content from Fixtures/template/file.php should be renderer' ); + $this->assertMatchesRegularExpression( "/{$val}/", $content, 'Content from Fixtures/template/file.php should contain $val' ); $contentUsingOtherMethod = $builder->loadTemplate( 'file', '.', $args ); $this->assertEquals( $content, $contentUsingOtherMethod, 'Content should be the same no matter the method of render' ); } -} \ No newline at end of file +} -- GitLab