From a8b9118cf09f3bb7ccdf7902180f1888a2341318 Mon Sep 17 00:00:00 2001 From: Nicolas JUEN Date: Tue, 8 Feb 2022 10:46:56 +0100 Subject: [PATCH 01/18] Add base tests --- .gitignore | 4 + composer.json | 7 +- inc/Services/Acf.php | 3 +- phpcs.xml | 1 + phpunit.xml | 14 ++ psalm.xml | 1 + tests/FrameworkTest.php | 36 +++++ tests/Helpers/Formatting/EscapeTest.php | 40 +++++ tests/Helpers/Formatting/TextTest.php | 189 ++++++++++++++++++++++++ tests/Services/AcfTest.php | 117 +++++++++++++++ tests/Services/SvgTest.php | 83 +++++++++++ tests/Tools/Body_Class_Test.php | 54 +++++++ tests/Tools/Template_Parts_Test.php | 57 +++++++ tests/bootstrap.php | 17 +++ tests/data/myfile.php | 0 15 files changed, 619 insertions(+), 4 deletions(-) create mode 100644 phpunit.xml create mode 100644 tests/FrameworkTest.php create mode 100644 tests/Helpers/Formatting/EscapeTest.php create mode 100644 tests/Helpers/Formatting/TextTest.php create mode 100644 tests/Services/AcfTest.php create mode 100644 tests/Services/SvgTest.php create mode 100644 tests/Tools/Body_Class_Test.php create mode 100644 tests/Tools/Template_Parts_Test.php create mode 100644 tests/bootstrap.php create mode 100644 tests/data/myfile.php diff --git a/.gitignore b/.gitignore index d34578a3..a841e817 100644 --- a/.gitignore +++ b/.gitignore @@ -75,3 +75,7 @@ package-lock.json ### config /vendor/ composer.lock + +### Tests +.phpunit.result.cache +tests/coverage/ \ No newline at end of file diff --git a/composer.json b/composer.json index aaeac585..45fb53d6 100644 --- a/composer.json +++ b/composer.json @@ -9,7 +9,8 @@ "phpro/grumphp-shim": "^0.19.1", "squizlabs/php_codesniffer": "^3.5", "vimeo/psalm": "^3.13", - "wp-coding-standards/wpcs": "^2.3" + "wp-coding-standards/wpcs": "^2.3", + "10up/wp_mock": "^0.4.2" }, "scripts": { "cs": [ @@ -20,7 +21,9 @@ ], "psalm": [ "./vendor/bin/psalm" - ] + ], + "tests": "./vendor/bin/phpunit", + "tests-cov": "phpdbg -qrr ./vendor/bin/phpunit --coverage-html ./tests/coverage/" }, "autoload": { "psr-4": { diff --git a/inc/Services/Acf.php b/inc/Services/Acf.php index 6c96b2ac..841c02ef 100644 --- a/inc/Services/Acf.php +++ b/inc/Services/Acf.php @@ -52,7 +52,7 @@ public function warning(): void { return; } - wp_die( sprintf( __( 'This theme can\'t work without ACF plugin. Please login to admin, and activate it !', 'framework-textdomain' ), esc_url( wp_login_url() ) ) ); // phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment, WordPress.Security.EscapeOutput.OutputNotEscaped + \wp_die( sprintf( __( 'This theme can\'t work without ACF plugin. Please login to admin, and activate it !', 'framework-textdomain' ), esc_url( wp_login_url() ) ) ); // phpcs:ignore WordPress.WP.I18n.MissingTranslatorsComment, WordPress.Security.EscapeOutput.OutputNotEscaped } /** @@ -138,7 +138,6 @@ public function init_acf(): void { if ( ! is_file( get_theme_file_path( $this->path . $file . '.php' ) ) ) { continue; } - require_once get_theme_file_path( $this->path . $file . '.php' ); } } diff --git a/phpcs.xml b/phpcs.xml index d3a05a26..10ed28a3 100644 --- a/phpcs.xml +++ b/phpcs.xml @@ -14,6 +14,7 @@ dist/ src/ scripts/ + tests/ diff --git a/phpunit.xml b/phpunit.xml new file mode 100644 index 00000000..2c91f844 --- /dev/null +++ b/phpunit.xml @@ -0,0 +1,14 @@ + + + + + functions.php + inc + + + + + tests + + + diff --git a/psalm.xml b/psalm.xml index a1814797..44c2e5b6 100644 --- a/psalm.xml +++ b/psalm.xml @@ -13,6 +13,7 @@ + diff --git a/tests/FrameworkTest.php b/tests/FrameworkTest.php new file mode 100644 index 00000000..4d2bab4d --- /dev/null +++ b/tests/FrameworkTest.php @@ -0,0 +1,36 @@ +assertFalse( \BEA\Theme\Framework\Framework::get_container()->get_service( 'not-a-service' ) ); + } + + public function testSameContainer() { + $container = \BEA\Theme\Framework\Framework::get_container(); + + $this->assertSame( $container, \BEA\Theme\Framework\Framework::get_container() ); + } + + public function testServiceSet() { + \BEA\Theme\Framework\Framework::register_service( Acf::class ); + + $this->assertNotEmpty( \BEA\Theme\Framework\Framework::get_container()->get_service( Acf::class ) ); + } +} diff --git a/tests/Helpers/Formatting/EscapeTest.php b/tests/Helpers/Formatting/EscapeTest.php new file mode 100644 index 00000000..82617434 --- /dev/null +++ b/tests/Helpers/Formatting/EscapeTest.php @@ -0,0 +1,40 @@ +assertSame( 'ok', $value ); + + $value = escape_attribute_value( 'ok', 'test_escape' ); + $this->assertSame( 'esc_ok', $value ); + } + public function testContent() { + $value = escape_content_value( 'ok', '' ); + $this->assertSame( 'ok', $value ); + + $value = escape_content_value( 'ok', 'test_escape' ); + $this->assertSame( 'esc_ok', $value ); + } +} + diff --git a/tests/Helpers/Formatting/TextTest.php b/tests/Helpers/Formatting/TextTest.php new file mode 100644 index 00000000..7713b413 --- /dev/null +++ b/tests/Helpers/Formatting/TextTest.php @@ -0,0 +1,189 @@ +assertEmpty( $test ); + + ob_start(); + the_text( '' ); + $buffer = ob_get_clean(); + $this->assertSame( $test, $buffer ); + + } + + public function testBeforeAfter() { + WP_Mock::userFunction( 'wp_parse_args', [ + 'times' => 6, + 'return_in_order' => [ + [ + 'before' => 'b', + 'after' => '', + 'escape' => '', + ], + [ + 'before' => '', + 'after' => 'a', + 'escape' => '', + ], + [ + 'before' => 'b', + 'after' => 'a', + 'escape' => '', + ], + [ + 'before' => 'b', + 'after' => '', + 'escape' => '', + ], + [ + 'before' => '', + 'after' => 'a', + 'escape' => '', + ], + [ + 'before' => 'b', + 'after' => 'a', + 'escape' => '', + ], + ], + ] ); + + $val = get_the_text( 'val', [ 'before' => 'b' ] ); + $val2 = get_the_text( 'val', [ 'after' => 'a' ] ); + $val3 = get_the_text( 'val', [ 'before' => 'b', 'after' => 'a' ] ); + + ob_start(); + the_text( 'val', [ 'before' => 'b' ] ); + $buffer = ob_get_clean(); + + ob_start(); + the_text( 'val', [ 'after' => 'a' ] ); + $buffer2 = ob_get_clean(); + + ob_start(); + the_text( 'val', [ 'before' => 'b', 'after' => 'a' ] ); + $buffer3 = ob_get_clean(); + + // Test returns + $this->assertSame( 'bval', $val ); + $this->assertSame( 'vala', $val2 ); + $this->assertSame( 'bvala', $val3 ); + + // Test Buffer and generated the same + $this->assertSame( $val, $buffer ); + $this->assertSame( $val2, $buffer2 ); + $this->assertSame( $val3, $buffer3 ); + } + + public function testEscape() { + WP_Mock::userFunction( 'wp_parse_args', [ + 'times' => 4, + 'return_in_order' => [ + [ + 'before' => '', + 'after' => '', + 'escape' => 'test_escape', + ], + [ + 'before' => 'b', + 'after' => '', + 'escape' => 'test_escape', + ], + [ + 'before' => '', + 'after' => 'a', + 'escape' => 'test_escape', + ], + [ + 'before' => 'b', + 'after' => 'a', + 'escape' => 'test_escape', + ], + ], + ] ); + + $this->assertSame( 'esc_val', get_the_text( 'val', [ + 'before' => '', + 'after' => '', + 'escape' => 'test_escape', + ] ) ); + $this->assertSame( 'besc_val', get_the_text( 'val', [ + 'before' => 'b', + 'after' => '', + 'escape' => 'test_escape', + ] ) ); + $this->assertSame( 'esc_vala', get_the_text( 'val', [ + 'before' => '', + 'after' => 'a', + 'escape' => 'test_escape', + ] ) ); + $this->assertSame( 'besc_vala', get_the_text( 'val', [ + 'before' => 'b', + 'after' => 'a', + 'escape' => 'test_escape', + ] ) ); + } + + public function testFilterSettings() { + WP_Mock::userFunction( 'wp_parse_args', [ + 'return' => + [ + 'before' => '', + 'after' => '', + 'escape' => 'test_escape', + ], + ] ); + + WP_Mock::onFilter( 'bea_theme_framework_text_settings' )->with( [ + 'before' => '', + 'after' => '', + 'escape' => 'test_escape', + ], 'val' )->reply( [ + 'before' => 'b', + 'after' => 'a', + 'escape' => 'esc_html', + ] ); + + $this->assertSame( 'bvala', get_the_text( 'val' ) ); + } + + public function testFilterValue() { + WP_Mock::userFunction( 'wp_parse_args', [ + 'return' => + [ + 'before' => '', + 'after' => '', + 'escape' => 'test_escape', + ], + ] ); + + WP_Mock::onFilter( 'bea_theme_framework_text_value' )->with( 'esc_val', [ + 'before' => '', + 'after' => '', + 'escape' => 'test_escape', + ] )->reply( 'filtered' ); + + $this->assertSame( 'filtered', get_the_text( 'val' ) ); + } +} + diff --git a/tests/Services/AcfTest.php b/tests/Services/AcfTest.php new file mode 100644 index 00000000..a5f602ae --- /dev/null +++ b/tests/Services/AcfTest.php @@ -0,0 +1,117 @@ +assertEquals( 'acf', $acf->get_service_name() ); + } + + public function testWarning() { + $acf = new Acf(); + + WP_Mock::userFunction( 'wp_die', [ + 'times' => 1, + ] ); + + WP_Mock::userFunction( 'esc_url', [ + 'times' => 1, + ] ); + + WP_Mock::userFunction( 'wp_login_url', [ + 'times' => 1, + ] ); + $acf->warning(); + + // Test we do not launch the functions on existing get_field function + WP_Mock::userFunction( 'get_field' ); + $acf->warning(); + } + + public function testGetSetFiles() { + $acf = new Acf(); + + $acf->register_files( [ 'myfile' ] ); + $this->assertSame( [ 'myfile' => 'myfile' ], $acf->get_files() ); + + $acf->register_files( [ 'myfile', '' ] ); + $this->assertSame( [ 'myfile' => 'myfile' ], $acf->get_files() ); + + $acf->register_files( [ 'myfile' ] ); + $this->assertSame( [ 'myfile' => 'myfile' ], $acf->get_files() ); + } + + public function testRegisterOptionPage() { + $acf = new Acf(); + + // Function not existing + $this->assertFalse( $acf->acf_add_options_page( [] ) ); + + WP_Mock::userFunction( 'acf_add_options_page', [ 'return' => 'ok' ] ); + $this->assertSame( 'ok', $acf->acf_add_options_page( [ 'menu_slug' => 'ok' ] ) ); + + // Exception is ok + $this->expectException( InvalidArgumentException::class ); + $acf->acf_add_options_page( [] ); + + } + + public function testRegisterOptionSubPage() { + $acf = new Acf(); + + // Function not existing + $this->assertFalse( $acf->acf_add_options_sub_page( [] ) ); + + WP_Mock::userFunction( 'acf_add_options_sub_page', [ 'return' => 'ok' ] ); + $this->assertSame( 'ok', $acf->acf_add_options_sub_page( [ 'menu_slug' => 'ok' ] ) ); + + // Exception is ok + $this->expectException( InvalidArgumentException::class ); + $acf->acf_add_options_sub_page( [] ); + } + + public function testPath() { + $acf = new Acf(); + + // Function not existing + $acf->set_path( 'test/path' ); + } + + public function testInitAcf() { + $acf = new Acf(); + + // Function not existing + $this->assertNull( $acf->init_acf() ); + + WP_Mock::userFunction( 'get_theme_file_path', [ + 'return_in_order' => [ + __DIR__ . '/../data/', + __DIR__ . '/../data/myfile.php', + __DIR__ . '/../data/myfile.php', + __DIR__ . '/../data/myfile2.php', + __DIR__ . '/../data/myfile3.php', + ], + 'times' => 5, + ] ); + + $acf->register_files( [ 'myfile', 'myfile2', 'myfile3' ] ); + $acf->init_acf(); + } + +} diff --git a/tests/Services/SvgTest.php b/tests/Services/SvgTest.php new file mode 100644 index 00000000..f3ce79b4 --- /dev/null +++ b/tests/Services/SvgTest.php @@ -0,0 +1,83 @@ +assertEquals( 'svg', $svg->get_service_name() ); + } + + public function testTags() { + $svg = new Svg(); + $this->assertIsArray( $svg->allow_svg_tag( [] ) ); + $this->assertArrayHasKey( 'path', $svg->allow_svg_tag( [] ) ); + $this->assertArrayHasKey( 'svg', $svg->allow_svg_tag( [] ) ); + $this->assertArrayHasKey( 'use', $svg->allow_svg_tag( [] ) ); + } + + public function testRegister() { + $svg = new Svg(); + $container = $this->createStub( Service_Container::class ); + + WP_Mock::expectFilterAdded( 'wp_kses_allowed_html', [ $svg, 'allow_svg_tag' ] ); + + $svg->register( $container ); + } + + public function testEmptyIcon() { + $svg = new Svg(); + $this->assertEmpty( $svg->get_the_icon('') ); + } + + public function testGetIcon() { + $svg = new Svg(); + WP_Mock::passthruFunction( 'sanitize_html_class' ); + WP_Mock::userFunction( 'get_theme_file_uri', [ + 'return' => 'test.example.fr', + ] ); + + $this->assertSame( + '', + $svg->get_the_icon( 'test' ) + ); + } + public function testTheIcon() { + $svg = new Svg(); + WP_Mock::passthruFunction( 'sanitize_html_class' ); + WP_Mock::userFunction( 'get_theme_file_uri', [ + 'return' => 'test.example.fr', + ] ); + + \ob_start(); + $this->AssertNull( $svg->the_icon( 'test' ) ); + $this->AssertNull( $svg->the_icon( 'test', [ 'class1', 'class2' ] ) ); + \ob_end_clean(); + } + + public function testGetIconWithClass() { + $svg = new Svg(); + WP_Mock::passthruFunction( 'sanitize_html_class' ); + + WP_Mock::userFunction( 'get_theme_file_uri', [ + 'return' => 'test.example.fr', + ] ); + + $this->assertSame( + '', + $svg->get_the_icon( 'test', [ 'class2', 'class3' ] ) + ); + } +} diff --git a/tests/Tools/Body_Class_Test.php b/tests/Tools/Body_Class_Test.php new file mode 100644 index 00000000..c25d9754 --- /dev/null +++ b/tests/Tools/Body_Class_Test.php @@ -0,0 +1,54 @@ +add( 'test' ); + $this->assertEquals( [ 'test' ], $body_class->body_class( [] ) ); + + } + + public function testRemove() { + $body_class = new \BEA\Theme\Framework\Tools\Body_Class(); + + $body_class->add( 'test' ); + $body_class->remove( 'test' ); + $this->assertEquals( [], $body_class->body_class( [] ) ); + } + + public function testAddRemoveWithInitialData() { + $body_class = new \BEA\Theme\Framework\Tools\Body_Class(); + + $body_class->add( 'test' ); + $body_class->remove( 'test' ); + $this->assertEquals( [ 'leaveme' ], $body_class->body_class( [ 'leaveme' ] ) ); + } + + public function testName() { + $body_class = new \BEA\Theme\Framework\Tools\Body_Class(); + self::assertEquals( 'body-class', $body_class->get_service_name() ); + } + + public function testBoot() { + $body_class = new \BEA\Theme\Framework\Tools\Body_Class(); + $container = $this->createStub( Service_Container::class ); + + \WP_Mock::expectFilterAdded( 'body_class', [ $body_class, 'body_class' ] ); + + $body_class->boot( $container ); + } +} diff --git a/tests/Tools/Template_Parts_Test.php b/tests/Tools/Template_Parts_Test.php new file mode 100644 index 00000000..4c445671 --- /dev/null +++ b/tests/Tools/Template_Parts_Test.php @@ -0,0 +1,57 @@ +assertTrue( $template->add_var( 'slug', 'key', 'value' ) ); + } + + public function testGet() { + $template = new \BEA\Theme\Framework\Tools\Template_Parts(); + + $this->assertNull( $template->get_var( 'slug', 'key' ) ); + + $template->add_var( 'slug', 'key', 'value' ); + + $this->assertSame( 'value', $template->get_var( 'slug', 'key' ) ); + } + + public function testGetOtherslug() { + $template = new \BEA\Theme\Framework\Tools\Template_Parts(); + + $template->add_var( 'slug', 'key', 'value' ); + + $this->assertEmpty( $template->get_var( 'slug2', 'key' ) ); + } + + public function testGetVars() { + $template = new \BEA\Theme\Framework\Tools\Template_Parts(); + + $template->add_var( 'slug', 'key', 'value' ); + $template->add_var( 'slug', 'key2', 'value3' ); + $template->add_var( 'slug3', 'key', 'value3' ); + + $this->assertSame( [ 'key' => 'value', 'key2' => 'value3' ], $template->get_vars( 'slug' ) ); + $this->assertNull( $template->get_vars( 'slug2' ) ); + $this->assertSame( 'value3', $template->get_var( 'slug3', 'key' ) ); + } + + public function testName() { + $template = new \BEA\Theme\Framework\Tools\Template_Parts(); + self::assertEquals( 'template-parts', $template->get_service_name() ); + } + +} diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 00000000..d62fd886 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,17 @@ + Date: Tue, 8 Feb 2022 10:55:34 +0100 Subject: [PATCH 02/18] Add phpunit tests to the github action --- .github/workflows/php-quality.yml | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/.github/workflows/php-quality.yml b/.github/workflows/php-quality.yml index 3887269a..65d8bac2 100644 --- a/.github/workflows/php-quality.yml +++ b/.github/workflows/php-quality.yml @@ -32,4 +32,13 @@ jobs: - name: Check psalm run: | mkdir -p dist - composer psalm \ No newline at end of file + composer psalm + + - name: Check unit test + run: composer tests-cov -- --coverage-clover=./tests/coverage/xml/index.xml + + - name: Run codacy-coverage-reporter + uses: codacy/codacy-coverage-reporter-action@v1 + with: + project-token: ${{ secrets.CODACY_PROJECT_TOKEN }} + coverage-reports: tests/coverage/xml/ \ No newline at end of file From fad9a8f6a34f4ff228039a5a126b201c4362234e Mon Sep 17 00:00:00 2001 From: Nicolas JUEN Date: Tue, 8 Feb 2022 10:57:01 +0100 Subject: [PATCH 03/18] Fix report name --- .github/workflows/php-quality.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/php-quality.yml b/.github/workflows/php-quality.yml index 65d8bac2..ddbe6b53 100644 --- a/.github/workflows/php-quality.yml +++ b/.github/workflows/php-quality.yml @@ -41,4 +41,4 @@ jobs: uses: codacy/codacy-coverage-reporter-action@v1 with: project-token: ${{ secrets.CODACY_PROJECT_TOKEN }} - coverage-reports: tests/coverage/xml/ \ No newline at end of file + coverage-reports: tests/coverage/xml/index.xml \ No newline at end of file From 3e1e1a7269d9002c0a7f7369c74ea347c871910b Mon Sep 17 00:00:00 2001 From: Nicolas JUEN Date: Tue, 8 Feb 2022 15:33:23 +0100 Subject: [PATCH 04/18] Remove the tests folder from the exported zip --- .gitattributes | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.gitattributes b/.gitattributes index ecb4618d..3323417e 100644 --- a/.gitattributes +++ b/.gitattributes @@ -8,4 +8,5 @@ composer.json export-ignore composer.lock export-ignore grumphp.yml export-ignore phpcs.xml export-ignore -psalm.xml export-ignore \ No newline at end of file +psalm.xml export-ignore +tests export-ignore \ No newline at end of file From d24958ab85321ebd34fe3ee201922a3804aa79b8 Mon Sep 17 00:00:00 2001 From: Nicolas JUEN Date: Wed, 9 Feb 2022 22:03:39 +0100 Subject: [PATCH 05/18] Ad polyfill file for wp_parse_args --- tests/bootstrap.php | 5 +---- tests/polyfills.php | 19 +++++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) create mode 100644 tests/polyfills.php diff --git a/tests/bootstrap.php b/tests/bootstrap.php index d62fd886..d1b42513 100644 --- a/tests/bootstrap.php +++ b/tests/bootstrap.php @@ -10,8 +10,5 @@ * Now we include any plugin files that we need to be able to run the tests. This * should be files that define the functions and classes you're going to test. */ +require_once __DIR__ . '/polyfills.php'; require_once __DIR__ . '/../functions.php'; - -function test_escape( $value ) { - return 'esc_' . $value; -} \ No newline at end of file diff --git a/tests/polyfills.php b/tests/polyfills.php new file mode 100644 index 00000000..3bf191d1 --- /dev/null +++ b/tests/polyfills.php @@ -0,0 +1,19 @@ + Date: Wed, 9 Feb 2022 22:04:21 +0100 Subject: [PATCH 06/18] Fix image tests --- tests/Helpers/Formatting/ImageTest.php | 50 ++++++++++++++++++++++++++ 1 file changed, 50 insertions(+) create mode 100644 tests/Helpers/Formatting/ImageTest.php diff --git a/tests/Helpers/Formatting/ImageTest.php b/tests/Helpers/Formatting/ImageTest.php new file mode 100644 index 00000000..b4a97597 --- /dev/null +++ b/tests/Helpers/Formatting/ImageTest.php @@ -0,0 +1,50 @@ +assertSame( '', get_the_image( 0, [] ) ); + } + + public function testGetImageEmpty() { + WP_Mock::userFunction( 'wp_get_attachment_image', [ + 'return' => '', + ] + ); + + $this->assertSame( '', get_the_image( 10, [ 'size' => '', 'data-location' => '' ] ) ); + } + + public function testImageBeforeAfter() { + WP_Mock::userFunction( 'wp_get_attachment_image', [ + 'return' => '', + ] + ); + + $this->assertSame( 'b', get_the_image( 10, [], [ 'before' => 'b' ] ) ); + $this->assertSame( 'a', get_the_image( 10, [], [ 'after' => 'a' ] ) ); + $this->assertSame( 'ba', get_the_image( 10, [], [ 'after' => 'a', 'before' => 'b' ] ) ); + } +} + From b9dc2bbf62270288b98de12c989261532375c452 Mon Sep 17 00:00:00 2001 From: Nicolas JUEN Date: Wed, 9 Feb 2022 22:04:42 +0100 Subject: [PATCH 07/18] Fix tests for text --- tests/Helpers/Formatting/TextTest.php | 137 +++++++++----------------- 1 file changed, 44 insertions(+), 93 deletions(-) diff --git a/tests/Helpers/Formatting/TextTest.php b/tests/Helpers/Formatting/TextTest.php index 7713b413..1d8d4263 100644 --- a/tests/Helpers/Formatting/TextTest.php +++ b/tests/Helpers/Formatting/TextTest.php @@ -28,46 +28,9 @@ public function testEmpty() { the_text( '' ); $buffer = ob_get_clean(); $this->assertSame( $test, $buffer ); - } public function testBeforeAfter() { - WP_Mock::userFunction( 'wp_parse_args', [ - 'times' => 6, - 'return_in_order' => [ - [ - 'before' => 'b', - 'after' => '', - 'escape' => '', - ], - [ - 'before' => '', - 'after' => 'a', - 'escape' => '', - ], - [ - 'before' => 'b', - 'after' => 'a', - 'escape' => '', - ], - [ - 'before' => 'b', - 'after' => '', - 'escape' => '', - ], - [ - 'before' => '', - 'after' => 'a', - 'escape' => '', - ], - [ - 'before' => 'b', - 'after' => 'a', - 'escape' => '', - ], - ], - ] ); - $val = get_the_text( 'val', [ 'before' => 'b' ] ); $val2 = get_the_text( 'val', [ 'after' => 'a' ] ); $val3 = get_the_text( 'val', [ 'before' => 'b', 'after' => 'a' ] ); @@ -96,31 +59,7 @@ public function testBeforeAfter() { } public function testEscape() { - WP_Mock::userFunction( 'wp_parse_args', [ - 'times' => 4, - 'return_in_order' => [ - [ - 'before' => '', - 'after' => '', - 'escape' => 'test_escape', - ], - [ - 'before' => 'b', - 'after' => '', - 'escape' => 'test_escape', - ], - [ - 'before' => '', - 'after' => 'a', - 'escape' => 'test_escape', - ], - [ - 'before' => 'b', - 'after' => 'a', - 'escape' => 'test_escape', - ], - ], - ] ); + $this->assertSame( 'esc_val', get_the_text( 'val', [ 'before' => '', @@ -145,45 +84,57 @@ public function testEscape() { } public function testFilterSettings() { - WP_Mock::userFunction( 'wp_parse_args', [ - 'return' => - [ - 'before' => '', - 'after' => '', - 'escape' => 'test_escape', - ], - ] ); - WP_Mock::onFilter( 'bea_theme_framework_text_settings' )->with( [ - 'before' => '', - 'after' => '', - 'escape' => 'test_escape', - ], 'val' )->reply( [ - 'before' => 'b', - 'after' => 'a', - 'escape' => 'esc_html', - ] ); - - $this->assertSame( 'bvala', get_the_text( 'val' ) ); + WP_Mock::onFilter( 'bea_theme_framework_text_settings' )->with( + [ + 'before' => '', + 'after' => '', + 'escape' => 'test_escape', + ], + 'val' + )->reply( + [ + 'before' => 'b', + 'after' => 'a', + 'escape' => 'esc_html', + ] + ); + + $this->assertSame( + 'bvala', + get_the_text( + 'val', + [ + 'before' => 'b', + 'after' => 'a', + 'escape' => 'esc_html', + ] + ) + ); } public function testFilterValue() { - WP_Mock::userFunction( 'wp_parse_args', [ - 'return' => + + WP_Mock::onFilter( 'bea_theme_framework_text_value' )->with( + 'esc_val', + [ + 'before' => '', + 'after' => '', + 'escape' => 'test_escape', + ] + )->reply( 'filtered' ); + + $this->assertSame( + 'filtered', + get_the_text( + 'val', [ 'before' => '', 'after' => '', 'escape' => 'test_escape', - ], - ] ); - - WP_Mock::onFilter( 'bea_theme_framework_text_value' )->with( 'esc_val', [ - 'before' => '', - 'after' => '', - 'escape' => 'test_escape', - ] )->reply( 'filtered' ); - - $this->assertSame( 'filtered', get_the_text( 'val' ) ); + ] + ) + ); } } From b93ecf5a41795466232046974fc7dc4862865db3 Mon Sep 17 00:00:00 2001 From: Nicolas JUEN Date: Wed, 9 Feb 2022 22:04:57 +0100 Subject: [PATCH 08/18] Add tests for Link formatin --- tests/Helpers/Formatting/LinkTest.php | 202 ++++++++++++++++++++++++++ 1 file changed, 202 insertions(+) create mode 100644 tests/Helpers/Formatting/LinkTest.php diff --git a/tests/Helpers/Formatting/LinkTest.php b/tests/Helpers/Formatting/LinkTest.php new file mode 100644 index 00000000..534f9521 --- /dev/null +++ b/tests/Helpers/Formatting/LinkTest.php @@ -0,0 +1,202 @@ +assertSame( '', get_the_link( [] ) ); + } + + public function testGetTheLinkTargetBlank() { + + // Auto noopener + $this->assertSame( + 'New windowTITLE LINK', + get_the_link( + [ + 'href' => 'https://localhost.dev', + 'target' => '_blank', + 'title' => 'TITLE LINK', + ] + ) + ); + + // self + $this->assertSame( + '', + get_the_link( + [ + 'href' => 'https://localhost.dev', + 'target' => '_self', + ] + ) + ); + + // self title + $this->assertSame( + 'TITLE LINK', + get_the_link( + [ + 'href' => 'https://localhost.dev', + 'target' => '_self', + 'title' => 'TITLE LINK', + ] + ) + ); + + // Custom attribute + $this->assertSame( + '', + get_the_link( + [ + 'href' => 'https://localhost.dev', + 'data-seo' => 'ok', + ] + ) + ); + $this->assertSame( + '', + get_the_link( + [ + 'href' => 'https://localhost.dev', + 'empty-data' => null, + ] + ) + ); + + // Before/after + $this->assertSame( + 'b', + get_the_link( + [ + 'href' => 'https://localhost.dev', + ], + [ + 'before' => 'b', + ] + ) + ); + $this->assertSame( + 'a', + get_the_link( + [ + 'href' => 'https://localhost.dev', + ], + [ + 'after' => 'a', + ] + ) + ); + $this->assertSame( + 'ba', + get_the_link( + [ + 'href' => 'https://localhost.dev', + ], + [ + 'after' => 'a', + 'before' => 'b', + ] + ) + ); + + $this->assertSame( + 'Content', + get_the_link( + [ + 'href' => 'https://localhost.dev', + ], + [ + 'content' => 'Content', + ] + ) + ); + + $this->assertSame( + 'Content', + get_the_link( + [ + 'href' => 'https://localhost.dev', + 'title' => 'TITLE', + ], + [ + 'content' => 'Content', + ] + ) + ); + } + + public function testGetAcfLinkEmptyURLOrTitle() { + $this->assertSame( '', get_acf_link( [] ) ); + $this->assertSame( '', get_acf_link( [ 'field' => [ 'url' => 'ok' ] ] ) ); + $this->assertSame( '', get_acf_link( [ 'field' => [ 'title' => 'ok' ] ] ) ); + } + + public function testGetAcfLinkWithAttributes() { + $this->assertSame( + 'New windowTitle', + get_acf_link( + [ + 'field' => [ + 'title' => 'Title', + 'url' => 'https://localhost.dev', + 'target' => '_blank', + ], + ] + ) + ); + + $this->assertSame( + 'Title', + get_acf_link( + [ + 'field' => [ + 'title' => 'Title', + 'url' => 'https://localhost.dev', + 'target' => '', + ], + ] + ) + ); + + $this->assertSame( + 'CONTENT', + get_acf_link( + [ + 'field' => [ + 'title' => 'Title', + 'url' => 'https://localhost.dev', + 'target' => '', + ], + ], + [ + 'content' => 'CONTENT', + ] + ) + ); + } +} + From bd06e86e60ace8e2bb26db1b8cc601b4d4bd2c2d Mon Sep 17 00:00:00 2001 From: Nicolas JUEN Date: Wed, 9 Feb 2022 22:05:24 +0100 Subject: [PATCH 09/18] Add tests for tools and services --- tests/Services/AssetsTest.php | 35 ++++++++++++++ tests/Tools/AssetsTest.php | 88 +++++++++++++++++++++++++++++++++++ 2 files changed, 123 insertions(+) create mode 100644 tests/Services/AssetsTest.php create mode 100644 tests/Tools/AssetsTest.php diff --git a/tests/Services/AssetsTest.php b/tests/Services/AssetsTest.php new file mode 100644 index 00000000..17a285ae --- /dev/null +++ b/tests/Services/AssetsTest.php @@ -0,0 +1,35 @@ +assertEquals( 'assets', $assets->get_service_name() ); + } + + public function testStyleSheetURIWithScriptDebug() { + $assets = new Assets(); + + WP_Mock::passthruFunction( 'get_theme_file_path', [ 'times' => 1 ] ); + + $this->assertSame( 'ok.css', $assets->stylesheet_uri( 'ok.css' ) ); + } + +} diff --git a/tests/Tools/AssetsTest.php b/tests/Tools/AssetsTest.php new file mode 100644 index 00000000..cde1f4e8 --- /dev/null +++ b/tests/Tools/AssetsTest.php @@ -0,0 +1,88 @@ + 1 ] ); + WP_Mock::passthruFunction( + 'wp_register_script', + [ + 'times' => 1, + 'args' => [ + 'handle', + 'src', + [], + false, + false, + ], + ] + ); + + $assets = new Assets(); + $assets->register_script( 'handle', 'src' ); + } + + public function testEnqueueScript() { + WP_Mock::passthruFunction( + 'wp_enqueue_script', + [ + 'times' => 1, + 'args' => [ + 'handle', + ], + ] + ); + + $assets = new Assets(); + $assets->enqueue_script( 'handle' ); + } + + public function testRegisterStyle() { + WP_Mock::passthruFunction( 'get_theme_file_uri', [ 'times' => 1 ] ); + WP_Mock::passthruFunction( + 'wp_register_style', + [ + 'times' => 1, + 'args' => [ + 'handle', + 'src', + [], + false, + 'all', + ], + ] + ); + + $assets = new Assets(); + $assets->register_style( 'handle', 'src' ); + } + + public function testEnqueueStyle() { + WP_Mock::passthruFunction( + 'wp_enqueue_style', + [ + 'times' => 1, + 'args' => [ + 'handle', + ], + ] + ); + + $assets = new Assets(); + $assets->enqueue_style( 'handle' ); + } +} From e26c89773168b209bcfdd31df4ea2c121e0cf12f Mon Sep 17 00:00:00 2001 From: Nicolas JUEN Date: Thu, 10 Feb 2022 15:54:40 +0100 Subject: [PATCH 10/18] Add files for testing --- tests/data/assets/app.css | 0 tests/data/assets/app.js | 0 tests/data/assets/app.min.css | 0 tests/data/assets/app.min.js | 0 tests/data/assets/assets-empty.json | 0 tests/data/assets/assets.json | 8 ++++++++ 6 files changed, 8 insertions(+) create mode 100644 tests/data/assets/app.css create mode 100644 tests/data/assets/app.js create mode 100644 tests/data/assets/app.min.css create mode 100644 tests/data/assets/app.min.js create mode 100644 tests/data/assets/assets-empty.json create mode 100644 tests/data/assets/assets.json diff --git a/tests/data/assets/app.css b/tests/data/assets/app.css new file mode 100644 index 00000000..e69de29b diff --git a/tests/data/assets/app.js b/tests/data/assets/app.js new file mode 100644 index 00000000..e69de29b diff --git a/tests/data/assets/app.min.css b/tests/data/assets/app.min.css new file mode 100644 index 00000000..e69de29b diff --git a/tests/data/assets/app.min.js b/tests/data/assets/app.min.js new file mode 100644 index 00000000..e69de29b diff --git a/tests/data/assets/assets-empty.json b/tests/data/assets/assets-empty.json new file mode 100644 index 00000000..e69de29b diff --git a/tests/data/assets/assets.json b/tests/data/assets/assets.json new file mode 100644 index 00000000..a8584cb0 --- /dev/null +++ b/tests/data/assets/assets.json @@ -0,0 +1,8 @@ +{ + "app.css" : "app.min.css", + "app.js" : "app.min.js", + "editor.js" : "editor.min.js", + "editor.css" : "editor.min.css", + "login.css" : "login.min.css", + "custom.css" : "custom.min.css" +} \ No newline at end of file From 1bcc6f7c616f719ae0315621d2bf203856275922 Mon Sep 17 00:00:00 2001 From: Nicolas JUEN Date: Thu, 10 Feb 2022 17:34:42 +0100 Subject: [PATCH 11/18] Add Service Assets Tests --- tests/Services/AssetsTest.php | 180 +++++++++++++++++++++++++++++++++- 1 file changed, 178 insertions(+), 2 deletions(-) diff --git a/tests/Services/AssetsTest.php b/tests/Services/AssetsTest.php index 17a285ae..a05ec408 100644 --- a/tests/Services/AssetsTest.php +++ b/tests/Services/AssetsTest.php @@ -2,15 +2,24 @@ namespace Services; +use BEA\Theme\Framework\Framework; use BEA\Theme\Framework\Service_Container; use BEA\Theme\Framework\Services\Acf; use BEA\Theme\Framework\Services\Assets; use BEA\Theme\Framework\Services\Svg; use InvalidArgumentException; +use stdclass; use WP_Mock; use WP_Mock\Tools\TestCase; +use function define; class AssetsTest extends TestCase { + /** + * Since we have CONSTANTS, run all tests in separate processes + */ + protected $preserveGlobalState = false; + protected $runTestInSeparateProcess = true; + public function setUp(): void { WP_Mock::setUp(); } @@ -24,12 +33,179 @@ public function testName() { $this->assertEquals( 'assets', $assets->get_service_name() ); } - public function testStyleSheetURIWithScriptDebug() { + public function testStyleSheetURIWithScriptDebugAndNoFiles() { $assets = new Assets(); - WP_Mock::passthruFunction( 'get_theme_file_path', [ 'times' => 1 ] ); + define( 'SCRIPT_DEBUG', true ); + + WP_Mock::userFunction( 'get_theme_file_path', [ + 'times' => 1, + 'return' => 'ok.css', + 'args' => [ '/dist/app.css' ], + ] ); + + $this->assertSame( 'ok.css', $assets->stylesheet_uri( 'ok.css' ) ); + } + + public function testStyleSheetURIWithScriptDebugAndFiles() { + $assets = new Assets(); + + define( 'SCRIPT_DEBUG', true ); + + $assets_file = __DIR__ . '/../data/assets/app.css'; + + WP_Mock::userFunction( + 'get_theme_file_path', + [ + 'times' => 1, + 'return' => $assets_file, + 'args' => [ + '/dist/app.css', + ], + ] + ); + WP_Mock::userFunction( + 'get_theme_file_uri', + [ + 'times' => 1, + 'args' => [ + '/dist/app.css', + ], + 'return' => 'https://localhost.dev/data/assets/app.css', + ] + ); + + $this->assertSame( 'https://localhost.dev/data/assets/app.css', $assets->stylesheet_uri( 'ok.css' ) ); + } + + public function testStyleSheetURIWithOutScriptDebugAndNoFiles() { + $assets = new Assets(); + + define( 'SCRIPT_DEBUG', false ); + + WP_Mock::passthruFunction( 'get_theme_file_path', [ 'times' => 2 ] ); $this->assertSame( 'ok.css', $assets->stylesheet_uri( 'ok.css' ) ); } + public function testGetMinFileEmpty() { + $assets = new Assets(); + + $this->assertSame( '', $assets->get_min_file( '' ) ); + } + + public function testGetMinFileAssetsExistsAndEmpty() { + $assets = new Assets(); + $assets_file = __DIR__ . '/../data/assets/assets-empty.json'; + + WP_Mock::userFunction( 'get_theme_file_path', [ 'args' => '/dist/assets.json', 'return' => $assets_file ] ); + + $this->assertSame( '', $assets->get_min_file( 'css' ) ); + } + + public function testGetMinFileAssetsExistsNotEmptyNonExistingType() { + $assets = new Assets(); + $assets_file = __DIR__ . '/../data/assets/assets.json'; + + WP_Mock::userFunction( 'get_theme_file_path', [ 'args' => '/dist/assets.json', 'return' => $assets_file ] ); + + $this->assertSame( '', $assets->get_min_file( 'non-existing' ) ); + } + + public function testGetMinFileAssetsExistsNotEmptyExistingType() { + $assets = new Assets(); + $assets_file = __DIR__ . '/../data/assets/assets.json'; + + WP_Mock::userFunction( 'get_theme_file_path', [ 'args' => '/dist/assets.json', 'return' => $assets_file ] ); + + // Existing + $this->assertSame( 'app.min.css', $assets->get_min_file( 'css' ) ); + $this->assertSame( 'app.min.js', $assets->get_min_file( 'js' ) ); + $this->assertSame( 'editor.min.css', $assets->get_min_file( 'editor.css' ) ); + $this->assertSame( 'editor.min.js', $assets->get_min_file( 'editor.js' ) ); + $this->assertSame( 'login.min.css', $assets->get_min_file( 'login' ) ); + + // Custom + $this->assertSame( 'custom.min.css', $assets->get_min_file( 'custom.css' ) ); + + // Non existing + $this->assertSame( '', $assets->get_min_file( 'custom.min.css' ) ); + } + + public function testGetLoginStyleSheet() { + $assets = new Assets(); + $assets_file = __DIR__ . '/../data/assets/assets.json'; + + WP_Mock::userFunction( 'get_theme_file_path', [ 'args' => '/dist/assets.json', 'return' => $assets_file ] ); + + // Existing + $this->assertSame( 'dist/login.min.css', $assets->login_stylesheet_uri() ); + } + + public function testGetLoginStyleSheetDebug() { + $assets = new Assets(); + $assets_file = __DIR__ . '/../data/assets/assets.json'; + define( 'SCRIPT_DEBUG', true ); + + WP_Mock::userFunction( 'get_theme_file_path', [ 'args' => '/dist/assets.json', 'return' => $assets_file ] ); + + // Existing + $this->assertSame( 'dist/login.css', $assets->login_stylesheet_uri() ); + } + + public function testRegisterFilesAdmin() { + $assets = new Assets(); + + WP_Mock::userFunction( 'is_admin', [ 'return' => true, 'times' => 1 ] ); + WP_Mock::passthruFunction( 'wp_get_theme', [ 'times' => 0 ] ); + + $this->assertNull( $assets->register_assets() ); + } + + public function testRegisterFiles() { + $assets = new Assets(); + $container = Framework::get_container(); + $theme_mock = $this->getMockBuilder( stdclass::class )->addMethods( [ 'get' ] )->getMock(); + $assets->register( $container ); + + WP_Mock::userFunction( + 'is_admin', + [ + 'return' => false, + 'times' => 1, + ] + ); + WP_Mock::userFunction( + 'wp_get_theme', + [ + 'times' => 1, + 'return' => $theme_mock, + ] + ); + WP_Mock::passthruFunction( 'wp_register_script', [ 'times' => 2 ] ); + WP_Mock::passthruFunction( 'wp_register_style', [ 'times' => 1 ] ); + WP_Mock::passthruFunction( 'get_theme_file_path', [ 'times' => 1 ] ); + WP_Mock::passthruFunction( 'get_theme_file_uri' ); + WP_Mock::userFunction( 'get_stylesheet_uri', [ 'return' => false ] ); + + // Check the version is called + $theme_mock->expects( $this->atLeastOnce() )->method( 'get' ); + + $this->assertNull( $assets->register_assets() ); + } + + public function testCheckFiltersAdded() { + $assets = new Assets(); + + // Check filters + WP_Mock::expectFilterAdded( 'stylesheet_uri', [ $assets, 'stylesheet_uri' ] ); + WP_Mock::expectFilterAdded( 'wp_login_page_theme_css', [ $assets, 'login_stylesheet_uri' ] ); + + // Check Action + WP_Mock::expectActionAdded( 'wp', [ $assets, 'register_assets' ] ); + WP_Mock::expectActionAdded( 'wp_enqueue_scripts', [ $assets, 'enqueue_scripts' ] ); + WP_Mock::expectActionAdded( 'wp_print_styles', [ $assets, 'enqueue_styles' ] ); + + $assets->boot( Framework::get_container() ); + } } From e8173966381d201269fcde311dd32aae629a33a4 Mon Sep 17 00:00:00 2001 From: Nicolas JUEN Date: Thu, 10 Feb 2022 17:35:07 +0100 Subject: [PATCH 12/18] Force the \ to the native functions --- inc/Services/Assets.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/inc/Services/Assets.php b/inc/Services/Assets.php index 20a61b8c..76631da0 100644 --- a/inc/Services/Assets.php +++ b/inc/Services/Assets.php @@ -69,7 +69,7 @@ public function register_assets(): void { $this->assets_tools->register_script( 'scripts', 'dist/' . $file, $scripts_dependencies, $theme->get( 'Version' ), true ); // CSS - wp_register_style( 'theme-style', get_stylesheet_uri(), [], $theme->get( 'Version' ) ); + \wp_register_style( 'theme-style', \get_stylesheet_uri(), [], $theme->get( 'Version' ) ); } /** @@ -98,6 +98,7 @@ public function enqueue_styles(): void { */ public function stylesheet_uri( string $stylesheet_uri ): string { if ( ! defined( 'SCRIPT_DEBUG' ) || SCRIPT_DEBUG === false ) { + $file = $this->get_min_file( 'css' ); if ( ! empty( $file ) && file_exists( \get_theme_file_path( '/dist/' . $file ) ) ) { return \get_theme_file_uri( '/dist/' . $file ); From 8219d43d92e653cb285208fa9dffe3637d2c6cc5 Mon Sep 17 00:00:00 2001 From: Nicolas JUEN Date: Thu, 10 Feb 2022 18:52:17 +0100 Subject: [PATCH 13/18] Only on PR --- .github/workflows/php-quality.yml | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/.github/workflows/php-quality.yml b/.github/workflows/php-quality.yml index ddbe6b53..b4ef1524 100644 --- a/.github/workflows/php-quality.yml +++ b/.github/workflows/php-quality.yml @@ -1,6 +1,6 @@ name: PHP Quality -on: [push, pull_request] +on: [ pull_request ] jobs: run: @@ -8,7 +8,7 @@ jobs: strategy: fail-fast: false matrix: - php-versions: [ 7.4' ] + php-versions: [ '7.4' ] name: PHP ${{ matrix.php-versions }} Test steps: - name: Checkout @@ -18,8 +18,6 @@ jobs: uses: shivammathur/setup-php@v2 with: php-version: ${{ matrix.php-versions }} - extensions: mbstring, intl #optional, setup extensions - - name: Check composer run: composer validate From aaba1febdf0d4ebf109472d4a0c2394e56c14854 Mon Sep 17 00:00:00 2001 From: Nicolas JUEN Date: Thu, 10 Feb 2022 19:04:01 +0100 Subject: [PATCH 14/18] Fix tests based on the last changes on repo --- tests/Helpers/Formatting/LinkTest.php | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/Helpers/Formatting/LinkTest.php b/tests/Helpers/Formatting/LinkTest.php index 534f9521..9cc5ccea 100644 --- a/tests/Helpers/Formatting/LinkTest.php +++ b/tests/Helpers/Formatting/LinkTest.php @@ -34,7 +34,7 @@ public function testGetTheLinkTargetBlank() { // Auto noopener $this->assertSame( - 'New windowTITLE LINK', + 'New window', get_the_link( [ 'href' => 'https://localhost.dev', @@ -57,7 +57,7 @@ public function testGetTheLinkTargetBlank() { // self title $this->assertSame( - 'TITLE LINK', + '', get_the_link( [ 'href' => 'https://localhost.dev', @@ -157,7 +157,7 @@ public function testGetAcfLinkEmptyURLOrTitle() { public function testGetAcfLinkWithAttributes() { $this->assertSame( - 'New windowTitle', + 'New windowTitle', get_acf_link( [ 'field' => [ @@ -170,7 +170,7 @@ public function testGetAcfLinkWithAttributes() { ); $this->assertSame( - 'Title', + 'Title', get_acf_link( [ 'field' => [ @@ -183,7 +183,7 @@ public function testGetAcfLinkWithAttributes() { ); $this->assertSame( - 'CONTENT', + 'CONTENT', get_acf_link( [ 'field' => [ From 8074e6ecbf2d296c198a71f08d599b1650497f79 Mon Sep 17 00:00:00 2001 From: Nicolas JUEN Date: Thu, 10 Feb 2022 17:35:07 +0100 Subject: [PATCH 15/18] Force the \ to the native functions --- inc/Services/Assets.php | 29 ++++++++++------------------- 1 file changed, 10 insertions(+), 19 deletions(-) diff --git a/inc/Services/Assets.php b/inc/Services/Assets.php index 032e3138..76631da0 100644 --- a/inc/Services/Assets.php +++ b/inc/Services/Assets.php @@ -59,17 +59,17 @@ public function register_assets(): void { // Js theme // Theme js dependencies - $scripts_dependencies = [ 'jquery' ]; + $scripts_dependencies = [ 'jquery', 'global-polyfill' ]; - // Async and footer - $file = $this->is_minified() ? $this->get_min_file( 'js' ) : 'app.js'; + // Polyfill + \wp_register_script( 'global-polyfill', 'https://cdn.polyfill.io/v3/polyfill.min.js?features=es5,es6,fetch,Array.prototype.includes,CustomEvent,Element.prototype.closest,NodeList.prototype.forEach', null, null, true ); //phpcs:ignore WordPress.WP.EnqueuedResourceParameters.MissingVersion - // Do not add version if minified - $version = $this->is_minified() ? null : $theme->get( 'Version' ); - $this->assets_tools->register_script( 'scripts', 'dist/' . $file, $scripts_dependencies, $version, true ); + // Async and footer + $file = ( ! defined( 'SCRIPT_DEBUG' ) || SCRIPT_DEBUG === false ) ? $this->get_min_file( 'js' ) : 'app.js'; + $this->assets_tools->register_script( 'scripts', 'dist/' . $file, $scripts_dependencies, $theme->get( 'Version' ), true ); // CSS - wp_register_style( 'theme-style', get_stylesheet_uri(), [], $version ); + \wp_register_style( 'theme-style', \get_stylesheet_uri(), [], $theme->get( 'Version' ) ); } /** @@ -97,7 +97,8 @@ public function enqueue_styles(): void { * @author Nicolas Juen */ public function stylesheet_uri( string $stylesheet_uri ): string { - if ( $this->is_minified() ) { + if ( ! defined( 'SCRIPT_DEBUG' ) || SCRIPT_DEBUG === false ) { + $file = $this->get_min_file( 'css' ); if ( ! empty( $file ) && file_exists( \get_theme_file_path( '/dist/' . $file ) ) ) { return \get_theme_file_uri( '/dist/' . $file ); @@ -167,21 +168,11 @@ public function get_min_file( string $type ): string { return $file; } - /** - * Check if we are on minified environment. - * - * @return bool - * @author Nicolas JUEN - */ - private function is_minified(): bool { - return ( ! defined( 'SCRIPT_DEBUG' ) || SCRIPT_DEBUG === false ); - } - /** * Change login CSS URL * @return string */ public function login_stylesheet_uri(): string { - return $this->is_minified() ? 'dist/' . $this->get_min_file( 'login' ) : 'dist/login.css'; + return ( ! defined( 'SCRIPT_DEBUG' ) || SCRIPT_DEBUG === false ) ? 'dist/' . $this->get_min_file( 'login' ) : 'dist/login.css'; } } From 6af29918baecd06518cec0abaf2fd8897c5d7158 Mon Sep 17 00:00:00 2001 From: Nicolas JUEN Date: Thu, 5 May 2022 16:18:28 +0200 Subject: [PATCH 16/18] Optimize imports --- tests/FrameworkTest.php | 13 +++++++------ tests/Services/SvgTest.php | 9 ++++++--- tests/Tools/Body_Class_Test.php | 16 +++++++++------- tests/Tools/Template_Parts_Test.php | 14 ++++++++------ 4 files changed, 30 insertions(+), 22 deletions(-) diff --git a/tests/FrameworkTest.php b/tests/FrameworkTest.php index 4d2bab4d..f1ab22b3 100644 --- a/tests/FrameworkTest.php +++ b/tests/FrameworkTest.php @@ -1,5 +1,6 @@ assertFalse( \BEA\Theme\Framework\Framework::get_container()->get_service( 'not-a-service' ) ); + $this->assertFalse( Framework::get_container()->get_service( 'not-a-service' ) ); } public function testSameContainer() { - $container = \BEA\Theme\Framework\Framework::get_container(); + $container = Framework::get_container(); - $this->assertSame( $container, \BEA\Theme\Framework\Framework::get_container() ); + $this->assertSame( $container, Framework::get_container() ); } public function testServiceSet() { - \BEA\Theme\Framework\Framework::register_service( Acf::class ); + Framework::register_service( Acf::class ); - $this->assertNotEmpty( \BEA\Theme\Framework\Framework::get_container()->get_service( Acf::class ) ); + $this->assertNotEmpty( Framework::get_container()->get_service( Acf::class ) ); } } diff --git a/tests/Services/SvgTest.php b/tests/Services/SvgTest.php index f3ce79b4..a151c125 100644 --- a/tests/Services/SvgTest.php +++ b/tests/Services/SvgTest.php @@ -5,8 +5,11 @@ use BEA\Theme\Framework\Service_Container; use BEA\Theme\Framework\Services\Svg; use WP_Mock; +use WP_Mock\Tools\TestCase; +use function ob_end_clean; +use function ob_start; -class SvgTest extends \WP_Mock\Tools\TestCase { +class SvgTest extends TestCase { public function setUp(): void { WP_Mock::setUp(); } @@ -61,10 +64,10 @@ public function testTheIcon() { 'return' => 'test.example.fr', ] ); - \ob_start(); + ob_start(); $this->AssertNull( $svg->the_icon( 'test' ) ); $this->AssertNull( $svg->the_icon( 'test', [ 'class1', 'class2' ] ) ); - \ob_end_clean(); + ob_end_clean(); } public function testGetIconWithClass() { diff --git a/tests/Tools/Body_Class_Test.php b/tests/Tools/Body_Class_Test.php index c25d9754..307f7100 100644 --- a/tests/Tools/Body_Class_Test.php +++ b/tests/Tools/Body_Class_Test.php @@ -3,9 +3,11 @@ namespace Tools; use BEA\Theme\Framework\Service_Container; +use BEA\Theme\Framework\Tools\Body_Class; use WP_Mock; +use WP_Mock\Tools\TestCase; -class Body_Class_Test extends \WP_Mock\Tools\TestCase { +class Body_Class_Test extends TestCase { public function setUp(): void { WP_Mock::setUp(); } @@ -15,7 +17,7 @@ public function tearDown(): void { } public function testAdd() { - $body_class = new \BEA\Theme\Framework\Tools\Body_Class(); + $body_class = new Body_Class(); $body_class->add( 'test' ); $this->assertEquals( [ 'test' ], $body_class->body_class( [] ) ); @@ -23,7 +25,7 @@ public function testAdd() { } public function testRemove() { - $body_class = new \BEA\Theme\Framework\Tools\Body_Class(); + $body_class = new Body_Class(); $body_class->add( 'test' ); $body_class->remove( 'test' ); @@ -31,7 +33,7 @@ public function testRemove() { } public function testAddRemoveWithInitialData() { - $body_class = new \BEA\Theme\Framework\Tools\Body_Class(); + $body_class = new Body_Class(); $body_class->add( 'test' ); $body_class->remove( 'test' ); @@ -39,15 +41,15 @@ public function testAddRemoveWithInitialData() { } public function testName() { - $body_class = new \BEA\Theme\Framework\Tools\Body_Class(); + $body_class = new Body_Class(); self::assertEquals( 'body-class', $body_class->get_service_name() ); } public function testBoot() { - $body_class = new \BEA\Theme\Framework\Tools\Body_Class(); + $body_class = new Body_Class(); $container = $this->createStub( Service_Container::class ); - \WP_Mock::expectFilterAdded( 'body_class', [ $body_class, 'body_class' ] ); + WP_Mock::expectFilterAdded( 'body_class', [ $body_class, 'body_class' ] ); $body_class->boot( $container ); } diff --git a/tests/Tools/Template_Parts_Test.php b/tests/Tools/Template_Parts_Test.php index 4c445671..fa679a02 100644 --- a/tests/Tools/Template_Parts_Test.php +++ b/tests/Tools/Template_Parts_Test.php @@ -2,9 +2,11 @@ namespace Tools; +use BEA\Theme\Framework\Tools\Template_Parts; use WP_Mock; +use WP_Mock\Tools\TestCase; -class Template_Parts_Test extends \WP_Mock\Tools\TestCase { +class Template_Parts_Test extends TestCase { public function setUp(): void { WP_Mock::setUp(); } @@ -14,13 +16,13 @@ public function tearDown(): void { } public function testAdd() { - $template = new \BEA\Theme\Framework\Tools\Template_Parts(); + $template = new Template_Parts(); $this->assertTrue( $template->add_var( 'slug', 'key', 'value' ) ); } public function testGet() { - $template = new \BEA\Theme\Framework\Tools\Template_Parts(); + $template = new Template_Parts(); $this->assertNull( $template->get_var( 'slug', 'key' ) ); @@ -30,7 +32,7 @@ public function testGet() { } public function testGetOtherslug() { - $template = new \BEA\Theme\Framework\Tools\Template_Parts(); + $template = new Template_Parts(); $template->add_var( 'slug', 'key', 'value' ); @@ -38,7 +40,7 @@ public function testGetOtherslug() { } public function testGetVars() { - $template = new \BEA\Theme\Framework\Tools\Template_Parts(); + $template = new Template_Parts(); $template->add_var( 'slug', 'key', 'value' ); $template->add_var( 'slug', 'key2', 'value3' ); @@ -50,7 +52,7 @@ public function testGetVars() { } public function testName() { - $template = new \BEA\Theme\Framework\Tools\Template_Parts(); + $template = new Template_Parts(); self::assertEquals( 'template-parts', $template->get_service_name() ); } From e34b4ef1342a1c34fb096c183b538b299393bfa5 Mon Sep 17 00:00:00 2001 From: Nicolas JUEN Date: Thu, 5 May 2022 19:06:29 +0200 Subject: [PATCH 17/18] Fix the tests for id 0 on get_the_image --- tests/Helpers/Formatting/ImageTest.php | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/tests/Helpers/Formatting/ImageTest.php b/tests/Helpers/Formatting/ImageTest.php index b4a97597..4a691329 100644 --- a/tests/Helpers/Formatting/ImageTest.php +++ b/tests/Helpers/Formatting/ImageTest.php @@ -24,6 +24,11 @@ public function tearDown(): void { } public function testGetZeroImage() { + WP_Mock::userFunction( 'wp_get_attachment_image', [ + 'return' => '', + ] + ); + $this->assertSame( '', get_the_image( 0, [] ) ); } From e4ec5b7013b10e13a1a33b7e64b1641de45741bd Mon Sep 17 00:00:00 2001 From: Nicolas JUEN Date: Thu, 5 May 2022 19:09:26 +0200 Subject: [PATCH 18/18] Fix the wp_parse_args function --- tests/polyfills.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/polyfills.php b/tests/polyfills.php index 3bf191d1..06371619 100644 --- a/tests/polyfills.php +++ b/tests/polyfills.php @@ -16,4 +16,5 @@ function wp_parse_args( $args, $defaults = array() ) { if ( is_array( $defaults ) && $defaults ) { return array_merge( $defaults, $parsed_args ); } -} \ No newline at end of file + return $parsed_args; +}