From 1fd21e94e32e83d0e33e879b7e91047853e059eb Mon Sep 17 00:00:00 2001 From: Sarah Mount Date: Tue, 19 Sep 2023 16:04:21 +0100 Subject: [PATCH 1/4] Migrate phpunit config to current schema This resolves a deprecation warning. --- phpunit.xml | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/phpunit.xml b/phpunit.xml index f255df3..1e26142 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,12 +1,13 @@ - + + + + + src + + tests - - - src - - From 2726af3e6b49799ba96e122aa11c973ad5d58671 Mon Sep 17 00:00:00 2001 From: Sarah Mount Date: Fri, 29 Sep 2023 18:29:52 +0100 Subject: [PATCH 2/4] Add assertions to tests that do not have them We have a small number of extant tests with no assertions. These raise a "risky test" warning when running ./script/test. This commit adds some sensible assertions to the tests to avoid this warning. --- tests/services/inspection_checker_test.php | 3 ++- tests/services/inspections_api_test.php | 4 +++- tests/services/json_api_test.php | 4 +++- 3 files changed, 8 insertions(+), 3 deletions(-) diff --git a/tests/services/inspection_checker_test.php b/tests/services/inspection_checker_test.php index 5f0c187..ba3022e 100644 --- a/tests/services/inspection_checker_test.php +++ b/tests/services/inspection_checker_test.php @@ -35,7 +35,8 @@ public function testPluginCallsAPI() 'revision' => '123456', ]; $checker = new \Dxw\Whippet\Services\InspectionChecker($api); - $checker->check('plugins', $my_plugin); + $result = $checker->check('plugins', $my_plugin); + $this->assertFalse($result->isErr()); } public function testPluginWithNoInspectionsGeneratesMessage() diff --git a/tests/services/inspections_api_test.php b/tests/services/inspections_api_test.php index 3221c82..6e43bfe 100644 --- a/tests/services/inspections_api_test.php +++ b/tests/services/inspections_api_test.php @@ -16,7 +16,9 @@ public function testCallsApi() ->andReturn(\Result\Result::ok([])); $api = new \Dxw\Whippet\Services\InspectionsApi('https://advisories.dxw.com', '/wp-json/v1/inspections/', $json_api); - $api->getInspections('my-plugin'); + $result = $api->getInspections('my-plugin'); + $this->assertFalse($result->isErr()); + $this->assertEquals([], $result->unwrap()); } public function testNoInspections() diff --git a/tests/services/json_api_test.php b/tests/services/json_api_test.php index f4a3dd0..3adbb90 100644 --- a/tests/services/json_api_test.php +++ b/tests/services/json_api_test.php @@ -16,7 +16,9 @@ public function testCallsApi() ->andReturn(\Result\Result::ok('[]')); $api = new \Dxw\Whippet\Services\JsonApi($base_api); - $api->get('http://apisite.com/api/endpoint'); + $result = $api->get('http://apisite.com/api/endpoint'); + $this->assertFalse($result->isErr()); + $this->assertEquals([], $result->unwrap()); } public function testEmptyResponse() From 56ce6a4490ed67630c53f2c1f86beee7577c978f Mon Sep 17 00:00:00 2001 From: Sarah Mount Date: Thu, 21 Sep 2023 17:43:58 +0100 Subject: [PATCH 3/4] FIX off-by-one error when gitignore file is empty --- src/Git/Gitignore.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Git/Gitignore.php b/src/Git/Gitignore.php index 9c6c97b..b37fffb 100644 --- a/src/Git/Gitignore.php +++ b/src/Git/Gitignore.php @@ -45,7 +45,7 @@ public function save_ignores($ignores) private function ensure_closing_newline($ignores) { $index_of_last_line = count($ignores) - 1; - $last_line = $ignores[$index_of_last_line]; + $last_line = $index_of_last_line >= 0 ? $ignores[$index_of_last_line] : 0; $last_character = substr($last_line, -1); if ($last_character != "\n") { From 492fc32ca3d007f021abbee6af6f96dc76d81774 Mon Sep 17 00:00:00 2001 From: Sarah Mount Date: Tue, 19 Sep 2023 15:25:39 +0100 Subject: [PATCH 4/4] Dependency types are not hard-coded literals Previously we hard-coded the list of dependency types wherever we needed to use them. In order to manage language packs within Whippet, we now need to add to the list of dependency types. So, rather than have them scattered over the code, we now have them in one place. Once we have dropped PHP 7.4 support, we should really use an enumeration here. Fixes #70 --- spec/dependencies/dependency_types.spec.php | 9 +++++++++ src/Dependencies/DependencyTypes.php | 16 ++++++++++++++++ src/Dependencies/Describer.php | 3 +-- src/Dependencies/Installer.php | 2 +- src/Dependencies/Updater.php | 6 +++--- src/Dependencies/Validator.php | 2 +- 6 files changed, 31 insertions(+), 7 deletions(-) create mode 100644 spec/dependencies/dependency_types.spec.php create mode 100644 src/Dependencies/DependencyTypes.php diff --git a/spec/dependencies/dependency_types.spec.php b/spec/dependencies/dependency_types.spec.php new file mode 100644 index 0000000..1aae91d --- /dev/null +++ b/spec/dependencies/dependency_types.spec.php @@ -0,0 +1,9 @@ +toBe(['themes', 'plugins']); + }); + }); +}); diff --git a/src/Dependencies/DependencyTypes.php b/src/Dependencies/DependencyTypes.php new file mode 100644 index 0000000..e3de5af --- /dev/null +++ b/src/Dependencies/DependencyTypes.php @@ -0,0 +1,16 @@ +isErr()) { return $resultLoad; } - $dependencyTypes = ['themes', 'plugins']; $git = new \Dxw\Whippet\Git\Git($this->dir); $results = []; - foreach ($dependencyTypes as $type) { + foreach (DependencyTypes::getDependencyTypes() as $type) { foreach ($this->lockFile->getDependencies($type) as $dep) { $result = $git::tag_for_commit($dep['src'], $dep['revision']); if ($result->isErr()) { diff --git a/src/Dependencies/Installer.php b/src/Dependencies/Installer.php index f837db2..e76475c 100644 --- a/src/Dependencies/Installer.php +++ b/src/Dependencies/Installer.php @@ -28,7 +28,7 @@ public function installAll() $dependencies = []; - foreach (['themes', 'plugins'] as $type) { + foreach (DependencyTypes::getDependencyTypes() as $type) { $dependencies[$type] = $this->lockFile->getDependencies($type); } diff --git a/src/Dependencies/Updater.php b/src/Dependencies/Updater.php index 37cba7e..d7977dc 100644 --- a/src/Dependencies/Updater.php +++ b/src/Dependencies/Updater.php @@ -57,7 +57,7 @@ public function updateAll() $allDependencies = []; - foreach (['themes', 'plugins'] as $type) { + foreach (DependencyTypes::getDependencyTypes() as $type) { $allDependencies[$type] = $this->jsonFile->getDependencies($type); } @@ -133,7 +133,7 @@ private function updateHash() private function createGitIgnore() { - foreach (['themes', 'plugins'] as $type) { + foreach (DependencyTypes::getDependencyTypes() as $type) { foreach ($this->jsonFile->getDependencies($type) as $dep) { $this->addDependencyToIgnoresArray($type, $dep['name']); } @@ -151,7 +151,7 @@ private function loadGitignore() } // Iterate through locked dependencies and remove from gitignore - foreach (['themes', 'plugins'] as $type) { + foreach (DependencyTypes::getDependencyTypes() as $type) { foreach ($this->lockFile->getDependencies($type) as $dep) { $line = $this->getGitignoreDependencyLine($type, $dep['name']); $index = array_search($line, $this->ignores); diff --git a/src/Dependencies/Validator.php b/src/Dependencies/Validator.php index 17ab56d..209638e 100644 --- a/src/Dependencies/Validator.php +++ b/src/Dependencies/Validator.php @@ -46,7 +46,7 @@ public function validate(bool $enforceRefs = false) // Check that entries in whippet.json // match entries in whippet.lock - foreach (['themes', 'plugins'] as $type) { + foreach (DependencyTypes::getDependencyTypes() as $type) { $whippetJsonDependencies = $whippetJson->getDependencies($type); $whippetLockDependencies = $whippetLock->getDependencies($type); if (count($whippetJsonDependencies) !== count($whippetLockDependencies)) {