From 14a4f9b0174e7ca9510a758736a1c435e6f3eef9 Mon Sep 17 00:00:00 2001 From: Karlo Biscan Date: Tue, 28 Jun 2022 11:59:15 +0200 Subject: [PATCH 1/6] Exctract handles to public methods --- src/Enqueue/Admin/AbstractEnqueueAdmin.php | 25 ++++++- src/Enqueue/Blocks/AbstractEnqueueBlocks.php | 74 ++++++++++++++++---- src/Enqueue/Theme/AbstractEnqueueTheme.php | 24 ++++++- 3 files changed, 107 insertions(+), 16 deletions(-) diff --git a/src/Enqueue/Admin/AbstractEnqueueAdmin.php b/src/Enqueue/Admin/AbstractEnqueueAdmin.php index dde77f4ae..5f744f18a 100644 --- a/src/Enqueue/Admin/AbstractEnqueueAdmin.php +++ b/src/Enqueue/Admin/AbstractEnqueueAdmin.php @@ -30,6 +30,27 @@ abstract class AbstractEnqueueAdmin extends AbstractAssets */ protected ManifestInterface $manifest; + + /** + * Get admin Stylesheet handle. + * + * @return string + */ + public function getAdminStyleHandle(): string + { + return "{$this->getAssetsPrefix()}-styles"; + } + + /** + * Get admin JavaScript handle. + * + * @return string + */ + public function getAdminScriptHandle(): string + { + return "{$this->getAssetsPrefix()}-scripts"; + } + /** * Register the Stylesheets for the admin area. * @@ -38,7 +59,7 @@ abstract class AbstractEnqueueAdmin extends AbstractAssets public function enqueueStyles(): void { if (!$this->getConditionUse()) { - $handle = "{$this->getAssetsPrefix()}-styles"; + $handle = $this->getAdminStyleHandle(); \wp_register_style( $handle, @@ -60,7 +81,7 @@ public function enqueueStyles(): void public function enqueueScripts(): void { if (!$this->getConditionUse()) { - $handle = "{$this->getAssetsPrefix()}-scripts"; + $handle = $this->getAdminScriptHandle(); \wp_register_script( $handle, diff --git a/src/Enqueue/Blocks/AbstractEnqueueBlocks.php b/src/Enqueue/Blocks/AbstractEnqueueBlocks.php index 4673588ff..7708b43ba 100644 --- a/src/Enqueue/Blocks/AbstractEnqueueBlocks.php +++ b/src/Enqueue/Blocks/AbstractEnqueueBlocks.php @@ -33,6 +33,56 @@ abstract class AbstractEnqueueBlocks extends AbstractAssets */ protected ManifestInterface $manifest; + /** + * Get block editor JavaScript handle. + * + * @return string + */ + public function getBlockEditorScriptsHandle(): string + { + return "{$this->getAssetsPrefix()}-block-editor-scripts"; + } + + /** + * Get block editor Stylesheet handle. + * + * @return string + */ + public function getBlockEditorStyleHandle(): string + { + return "{$this->getAssetsPrefix()}-block-editor-style"; + } + + /** + * Get block Stylesheet handle. + * + * @return string + */ + public function getBlockStyleHandle(): string + { + return "{$this->getAssetsPrefix()}-block-style"; + } + + /** + * Get block frontend JavaScript handle. + * + * @return string + */ + public function getBlockFrontentScriptHandle(): string + { + return "{$this->getAssetsPrefix()}-block-frontend-scripts"; + } + + /** + * Get block frontend Stylesheet handle. + * + * @return string + */ + public function getBlockFrontentStyleHandle(): string + { + return "{$this->getAssetsPrefix()}-block-frontend-style"; + } + /** * Enqueue blocks script for editor only. * @@ -40,7 +90,7 @@ abstract class AbstractEnqueueBlocks extends AbstractAssets */ public function enqueueBlockEditorScript(): void { - $handle = "{$this->getAssetsPrefix()}-block-editor-scripts"; + $handle = $this->getBlockEditorScriptsHandle(); \wp_register_script( $handle, @@ -64,17 +114,17 @@ public function enqueueBlockEditorScript(): void */ public function enqueueBlockEditorStyle(): void { - $handler = "{$this->getAssetsPrefix()}-block-editor-style"; + $handle = $this->getBlockEditorStyleHandle(); \wp_register_style( - $handler, + $handle, $this->manifest->getAssetsManifestItem(static::BLOCKS_EDITOR_STYLE_URI), $this->getAdminStyleDependencies(), $this->getAssetsVersion(), $this->getMedia() ); - \wp_enqueue_style($handler); + \wp_enqueue_style($handle); } /** @@ -84,17 +134,17 @@ public function enqueueBlockEditorStyle(): void */ public function enqueueBlockStyle(): void { - $handler = "{$this->getAssetsPrefix()}-block-style"; + $handle = $this->getBlockStyleHandle(); \wp_register_style( - $handler, + $handle, $this->manifest->getAssetsManifestItem(static::BLOCKS_STYLE_URI), $this->getFrontendStyleDependencies(), $this->getAssetsVersion(), $this->getMedia() ); - \wp_enqueue_style($handler); + \wp_enqueue_style($handle); } /** @@ -104,7 +154,7 @@ public function enqueueBlockStyle(): void */ public function enqueueBlockFrontendScript(): void { - $handle = "{$this->getAssetsPrefix()}-block-frontend-scripts"; + $handle = $this->getBlockFrontentScriptHandle(); \wp_register_script( $handle, @@ -118,7 +168,7 @@ public function enqueueBlockFrontendScript(): void foreach ($this->getLocalizations() as $objectName => $dataArray) { - \wp_localize_script($handle, $objectName, $dataArray); + \wp_localize_script($this->getBlockFrontentScriptHandle(), $objectName, $dataArray); } } @@ -129,17 +179,17 @@ public function enqueueBlockFrontendScript(): void */ public function enqueueBlockFrontendStyle(): void { - $handler = "{$this->getAssetsPrefix()}-block-frontend-style"; + $handle = $this->getBlockFrontentStyleHandle(); \wp_register_style( - $handler, + $handle, $this->manifest->getAssetsManifestItem(static::BLOCKS_FRONTEND_STYLE_URI), $this->getFrontendStyleDependencies(), $this->getAssetsVersion(), $this->getMedia() ); - \wp_enqueue_style($handler); + \wp_enqueue_style($handle); } /** diff --git a/src/Enqueue/Theme/AbstractEnqueueTheme.php b/src/Enqueue/Theme/AbstractEnqueueTheme.php index e10463f27..9b30f0b88 100644 --- a/src/Enqueue/Theme/AbstractEnqueueTheme.php +++ b/src/Enqueue/Theme/AbstractEnqueueTheme.php @@ -28,6 +28,26 @@ abstract class AbstractEnqueueTheme extends AbstractAssets */ protected ManifestInterface $manifest; + /** + * Get theme Stylesheet handle. + * + * @return string + */ + public function getThemeStyleHandle(): string + { + return "{$this->getAssetsPrefix()}-theme-styles"; + } + + /** + * Get theme JavaScript handle. + * + * @return string + */ + public function getThemeScriptHandle(): string + { + return "{$this->getAssetsPrefix()}-scripts"; + } + /** * Register the Stylesheets for the front end of the theme. * @@ -35,7 +55,7 @@ abstract class AbstractEnqueueTheme extends AbstractAssets */ public function enqueueStyles(): void { - $handle = "{$this->getAssetsPrefix()}-theme-styles"; + $handle = $this->getThemeStyleHandle(); \wp_register_style( $handle, @@ -55,7 +75,7 @@ public function enqueueStyles(): void */ public function enqueueScripts(): void { - $handle = "{$this->getAssetsPrefix()}-scripts"; + $handle = $this->getThemeScriptHandle(); \wp_register_script( $handle, From 75db2298ea2b60fde7c8a88fe7399244f170fa9a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Denis=20=C5=BDoljom?= Date: Tue, 28 Jun 2022 14:06:36 +0200 Subject: [PATCH 2/6] WIP: testing session --- tests/Datasets/Arguments.php | 12 +++++++++++- tests/Enqueue/Admin/EnqueueAdminCliTest.php | 9 +++++---- tests/Enqueue/Admin/EnqueueAdminExampleTest.php | 12 ++++++++++++ tests/Helpers/SelectorsTraitTest.php | 7 ++++++- 4 files changed, 34 insertions(+), 6 deletions(-) diff --git a/tests/Datasets/Arguments.php b/tests/Datasets/Arguments.php index cf29fec22..8aa8210d9 100644 --- a/tests/Datasets/Arguments.php +++ b/tests/Datasets/Arguments.php @@ -21,7 +21,17 @@ dataset('classesArray', [ [['medium', 'large']], [['small']], - [['key' => 'bold--all']] + [['key' => 'bold--all']], + [[new stdClass()]], +]); + +dataset('classesString', [ + 'bla', + new stdClass(), + '😄', + 1, + false, + null ]); // Rest/Fields arguments diff --git a/tests/Enqueue/Admin/EnqueueAdminCliTest.php b/tests/Enqueue/Admin/EnqueueAdminCliTest.php index 1650df990..ff5ede06d 100644 --- a/tests/Enqueue/Admin/EnqueueAdminCliTest.php +++ b/tests/Enqueue/Admin/EnqueueAdminCliTest.php @@ -28,7 +28,7 @@ * Cleanup after tests. */ afterEach(function () { - deleteCliOutput(\dirname(__FILE__, 4) . '/cliOutput'); +// deleteCliOutput(\dirname(__FILE__, 4) . '/cliOutput'); }); test('Custom Enqueue Admin CLI command will correctly copy the Enqueue Admin class', function () { @@ -38,9 +38,10 @@ // Check the output dir if the generated method is correctly generated. $generatedAdmin = \file_get_contents(\dirname(__FILE__, 4) . '/cliOutput/src/Enqueue/Admin/EnqueueAdmin.php'); - $this->assertStringContainsString('class EnqueueAdmin extends AbstractEnqueueAdmin', $generatedAdmin); - $this->assertStringContainsString('admin_enqueue_scripts', $generatedAdmin); - $this->assertStringNotContainsString('wp_enqueue_scripts', $generatedAdmin); + expect($generatedAdmin) + ->toContain('class EnqueueAdmin extends AbstractEnqueueAdmin') + ->toContain('admin_enqueue_scripts') + ->not->toContain('wp_enqueue_scripts'); }); diff --git a/tests/Enqueue/Admin/EnqueueAdminExampleTest.php b/tests/Enqueue/Admin/EnqueueAdminExampleTest.php index b109ec62c..bacd42620 100644 --- a/tests/Enqueue/Admin/EnqueueAdminExampleTest.php +++ b/tests/Enqueue/Admin/EnqueueAdminExampleTest.php @@ -153,3 +153,15 @@ public function getLocalizations(): array $this->assertIsArray($localizationExample->getLocalizations()); $this->assertEmpty($localizationExample->getLocalizations()); }); + +test('will correctly return admin style handle', function () { + $adminHandle = $this->adminEnqueue->getAdminStyleHandle(); + + expect($adminHandle) + ->toBeString() + ->not->toBeArray() + ->toBe('MyProject-styles') + ->not->toBe('boilerplate-handle'); +}); + + diff --git a/tests/Helpers/SelectorsTraitTest.php b/tests/Helpers/SelectorsTraitTest.php index 8d97f10c3..64f7dd7c7 100644 --- a/tests/Helpers/SelectorsTraitTest.php +++ b/tests/Helpers/SelectorsTraitTest.php @@ -100,7 +100,12 @@ test('Asserts classnames returns a string', function ($args) { $this->assertIsString(Components::classnames($args)); -})->with('classesArray'); +})->with('classesArray')->only(); + + +test('Asserts classnames throws an error in case non array is passed', function ($args) { + Components::classnames($args); +})->with('classesString')->expectError(); // ------------------------------------------ // classnames From 6c781524eb6c4aa763350f3c212679912cc78651 Mon Sep 17 00:00:00 2001 From: Karlo Biscan Date: Thu, 30 Jun 2022 13:53:11 +0200 Subject: [PATCH 3/6] Ignore launch.json file --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index b69fa788f..14efaa7ae 100644 --- a/.gitignore +++ b/.gitignore @@ -14,4 +14,5 @@ latest_dump.tar.gz # Editor settings .vscode/settings.json +.vscode/launch.json .idea/* From f010c3467b361cb2657b6f24a926e3e940ce6452 Mon Sep 17 00:00:00 2001 From: Karlo Biscan Date: Thu, 30 Jun 2022 14:05:26 +0200 Subject: [PATCH 4/6] Cleanup code --- tests/Datasets/Arguments.php | 10 ---------- tests/Enqueue/Admin/EnqueueAdminCliTest.php | 9 ++++----- tests/Helpers/SelectorsTraitTest.php | 8 +------- 3 files changed, 5 insertions(+), 22 deletions(-) diff --git a/tests/Datasets/Arguments.php b/tests/Datasets/Arguments.php index 8aa8210d9..51bbd3ee4 100644 --- a/tests/Datasets/Arguments.php +++ b/tests/Datasets/Arguments.php @@ -22,16 +22,6 @@ [['medium', 'large']], [['small']], [['key' => 'bold--all']], - [[new stdClass()]], -]); - -dataset('classesString', [ - 'bla', - new stdClass(), - '😄', - 1, - false, - null ]); // Rest/Fields arguments diff --git a/tests/Enqueue/Admin/EnqueueAdminCliTest.php b/tests/Enqueue/Admin/EnqueueAdminCliTest.php index ff5ede06d..1650df990 100644 --- a/tests/Enqueue/Admin/EnqueueAdminCliTest.php +++ b/tests/Enqueue/Admin/EnqueueAdminCliTest.php @@ -28,7 +28,7 @@ * Cleanup after tests. */ afterEach(function () { -// deleteCliOutput(\dirname(__FILE__, 4) . '/cliOutput'); + deleteCliOutput(\dirname(__FILE__, 4) . '/cliOutput'); }); test('Custom Enqueue Admin CLI command will correctly copy the Enqueue Admin class', function () { @@ -38,10 +38,9 @@ // Check the output dir if the generated method is correctly generated. $generatedAdmin = \file_get_contents(\dirname(__FILE__, 4) . '/cliOutput/src/Enqueue/Admin/EnqueueAdmin.php'); - expect($generatedAdmin) - ->toContain('class EnqueueAdmin extends AbstractEnqueueAdmin') - ->toContain('admin_enqueue_scripts') - ->not->toContain('wp_enqueue_scripts'); + $this->assertStringContainsString('class EnqueueAdmin extends AbstractEnqueueAdmin', $generatedAdmin); + $this->assertStringContainsString('admin_enqueue_scripts', $generatedAdmin); + $this->assertStringNotContainsString('wp_enqueue_scripts', $generatedAdmin); }); diff --git a/tests/Helpers/SelectorsTraitTest.php b/tests/Helpers/SelectorsTraitTest.php index 64f7dd7c7..d4aa6d0df 100644 --- a/tests/Helpers/SelectorsTraitTest.php +++ b/tests/Helpers/SelectorsTraitTest.php @@ -100,12 +100,7 @@ test('Asserts classnames returns a string', function ($args) { $this->assertIsString(Components::classnames($args)); -})->with('classesArray')->only(); - - -test('Asserts classnames throws an error in case non array is passed', function ($args) { - Components::classnames($args); -})->with('classesString')->expectError(); +})->with('classesArray'); // ------------------------------------------ // classnames @@ -115,4 +110,3 @@ Components::classnames($argument); })->throws(\TypeError::class) ->with('errorStringArguments'); - From 25fbc98ad58c260bd2a1e915826ee543a04497de Mon Sep 17 00:00:00 2001 From: Karlo Biscan Date: Thu, 30 Jun 2022 14:11:07 +0200 Subject: [PATCH 5/6] Add tests for handles --- .../Enqueue/Admin/EnqueueAdminExampleTest.php | 12 ++++-- .../Blocks/EnqueueBlockExampleTest.php | 40 +++++++++++++++++++ .../Enqueue/Theme/EnqueueThemeExampleTest.php | 16 ++++++++ 3 files changed, 64 insertions(+), 4 deletions(-) diff --git a/tests/Enqueue/Admin/EnqueueAdminExampleTest.php b/tests/Enqueue/Admin/EnqueueAdminExampleTest.php index bacd42620..dd5a9406d 100644 --- a/tests/Enqueue/Admin/EnqueueAdminExampleTest.php +++ b/tests/Enqueue/Admin/EnqueueAdminExampleTest.php @@ -154,14 +154,18 @@ public function getLocalizations(): array $this->assertEmpty($localizationExample->getLocalizations()); }); -test('will correctly return admin style handle', function () { +test('getAdminStyleHandle will return string', function () { $adminHandle = $this->adminEnqueue->getAdminStyleHandle(); expect($adminHandle) ->toBeString() - ->not->toBeArray() - ->toBe('MyProject-styles') - ->not->toBe('boilerplate-handle'); + ->not->toBeArray(); }); +test('getAdminScriptHandle will return string', function () { + $adminHandle = $this->adminEnqueue->getAdminScriptHandle(); + expect($adminHandle) + ->toBeString() + ->not->toBeArray(); +}); diff --git a/tests/Enqueue/Blocks/EnqueueBlockExampleTest.php b/tests/Enqueue/Blocks/EnqueueBlockExampleTest.php index 9f5b4b976..12465fc2e 100644 --- a/tests/Enqueue/Blocks/EnqueueBlockExampleTest.php +++ b/tests/Enqueue/Blocks/EnqueueBlockExampleTest.php @@ -166,3 +166,43 @@ protected function getLocalizations(): array $this->assertSame(\getenv('REGISTER_STYLE'), "{$this->projectName}-block-frontend-style", 'Method enqueueStyles() failed to register style'); $this->assertSame(\getenv('ENQUEUE_STYLE'), "{$this->projectName}-block-frontend-style", 'Method enqueueScripts() failed to enqueue style'); }); + +test('getBlockEditorScriptsHandle will return string', function () { + $adminHandle = $this->blockEnqueue->getBlockEditorScriptsHandle(); + + expect($adminHandle) + ->toBeString() + ->not->toBeArray(); +}); + +test('getBlockEditorStyleHandle will return string', function () { + $adminHandle = $this->blockEnqueue->getBlockEditorStyleHandle(); + + expect($adminHandle) + ->toBeString() + ->not->toBeArray(); +}); + +test('getBlockFrontentScriptHandle will return string', function () { + $adminHandle = $this->blockEnqueue->getBlockFrontentScriptHandle(); + + expect($adminHandle) + ->toBeString() + ->not->toBeArray(); +}); + +test('getBlockFrontentStyleHandle will return string', function () { + $adminHandle = $this->blockEnqueue->getBlockFrontentStyleHandle(); + + expect($adminHandle) + ->toBeString() + ->not->toBeArray(); +}); + +test('getBlockStyleHandle will return string', function () { + $adminHandle = $this->blockEnqueue->getBlockStyleHandle(); + + expect($adminHandle) + ->toBeString() + ->not->toBeArray(); +}); diff --git a/tests/Enqueue/Theme/EnqueueThemeExampleTest.php b/tests/Enqueue/Theme/EnqueueThemeExampleTest.php index 145218af3..0d29cbd8a 100644 --- a/tests/Enqueue/Theme/EnqueueThemeExampleTest.php +++ b/tests/Enqueue/Theme/EnqueueThemeExampleTest.php @@ -109,3 +109,19 @@ protected function getLocalizations(): array $this->assertSame(\getenv('ENQUEUE_SCRIPT'), 'MyProject-scripts', 'Method enqueueScripts() failed to enqueue style'); $this->assertSame(\getenv('SIDEAFFECT'), 'localize', 'Method wp_localize_script() failed'); }); + +test('getThemeScriptHandle will return string', function () { + $adminHandle = $this->themeEnqueue->getThemeScriptHandle(); + + expect($adminHandle) + ->toBeString() + ->not->toBeArray(); +}); + +test('getThemeStyleHandle will return string', function () { + $adminHandle = $this->themeEnqueue->getThemeStyleHandle(); + + expect($adminHandle) + ->toBeString() + ->not->toBeArray(); +}); From 03d18a167681d1657c019175372b690a6d0baa19 Mon Sep 17 00:00:00 2001 From: Karlo Biscan Date: Thu, 30 Jun 2022 14:12:34 +0200 Subject: [PATCH 6/6] Add tests for getConditionUse --- .../Enqueue/Admin/EnqueueAdminExampleTest.php | 25 +++++++++++++++++++ 1 file changed, 25 insertions(+) diff --git a/tests/Enqueue/Admin/EnqueueAdminExampleTest.php b/tests/Enqueue/Admin/EnqueueAdminExampleTest.php index dd5a9406d..74accc1d6 100644 --- a/tests/Enqueue/Admin/EnqueueAdminExampleTest.php +++ b/tests/Enqueue/Admin/EnqueueAdminExampleTest.php @@ -169,3 +169,28 @@ public function getLocalizations(): array ->toBeString() ->not->toBeArray(); }); + +test('getConditionUse will be false if outside of admin', function () { + Functions\when('is_admin')->justReturn(false); + + $conditionUse = $this->adminEnqueue->getConditionUse(); + + expect($conditionUse) + ->toBeFalse() + ->not->toBeNull(); +}); + +test('getConditionUse will be true if inside block editor', function () { + Functions\when('get_current_screen')->alias(function () { + return new class + { + public $is_block_editor = true; // We are in the block editor. + }; + }); + + $conditionUse = $this->adminEnqueue->getConditionUse(); + + expect($conditionUse) + ->toBeTrue() + ->not->toBeNull(); +});