diff --git a/scaffold/tests/phpunit/Dirs.php b/scaffold/tests/phpunit/Dirs.php index 7f98684..15cb8ae 100644 --- a/scaffold/tests/phpunit/Dirs.php +++ b/scaffold/tests/phpunit/Dirs.php @@ -71,7 +71,7 @@ public function printInfo(): void { fwrite(STDERR, PHP_EOL . implode(PHP_EOL, $lines) . PHP_EOL); } - protected function prepareLocalRepo() { + protected function prepareLocalRepo(): void { $root = $this->fileFindDir('composer.json'); $this->fs->copy($root . '/composer.json', $this->repo . '/composer.json'); @@ -80,15 +80,25 @@ protected function prepareLocalRepo() { $this->fs->copy($root . '/myfile1.txt', $this->repo . '/myfile1.txt'); // Add the local repository to the composer.json file. - $dstJson = json_decode(file_get_contents($this->repo . '/composer.json'), TRUE); - $dstJson['repositories'][] = [ + $composerjson = file_get_contents($this->repo . '/composer.json'); + if ($composerjson === FALSE) { + throw new \Exception('Failed to read the local composer.json file.'); + } + + /** @var array $dst_json */ + $dst_json = json_decode($composerjson, TRUE); + if (!$dst_json) { + throw new \Exception('Failed to decode the local composer.json file.'); + } + + $dst_json['repositories'][] = [ 'type' => 'path', 'url' => $this->repo, 'options' => [ 'symlink' => FALSE, ], ]; - file_put_contents($this->repo . '/composer.json', json_encode($dstJson, JSON_PRETTY_PRINT)); + file_put_contents($this->repo . '/composer.json', json_encode($dst_json, JSON_PRETTY_PRINT)); } } diff --git a/scaffold/tests/phpunit/Traits/ComposerTrait.php b/scaffold/tests/phpunit/Traits/ComposerTrait.php index dadc3e2..cc705bd 100644 --- a/scaffold/tests/phpunit/Traits/ComposerTrait.php +++ b/scaffold/tests/phpunit/Traits/ComposerTrait.php @@ -25,7 +25,7 @@ protected function composerCreateProject(array|string $args = NULL): string { * * @param string $cmd * The Composer command to execute (escaped as required) - * @param null $cwd + * @param string|null $cwd * The current working directory to run the command from. * @param array $env * Environment variables to define for the subprocess. @@ -35,8 +35,8 @@ protected function composerCreateProject(array|string $args = NULL): string { * * @throws \Exception */ - public function composerRun($cmd, $cwd = NULL, $env = []): string { - $cwd = $cwd ?? $this->dirs->build; + public function composerRun(string $cmd, ?string $cwd = NULL, array $env = []): string { + $cwd = $cwd ?: $this->dirs->build; $env += [ 'DREVOPS_SCAFFOLD_VERSION' => '@dev', @@ -64,11 +64,17 @@ public function composerRun($cmd, $cwd = NULL, $env = []): string { return $output; } - protected function composerReadJson($path = NULL) { - $path = $path ?? $this->dirs->sut . '/composer.json'; + protected function composerReadJson(?string $path = NULL): array { + $path = $path ?: $this->dirs->sut . '/composer.json'; $this->assertFileExists($path); - return json_decode(file_get_contents($path), TRUE); + $composerjson = file_get_contents($path); + $this->assertIsString($composerjson); + + $data = json_decode($composerjson, TRUE); + $this->assertIsArray($data); + + return $data; } } diff --git a/scaffold/tests/phpunit/Traits/EnvTrait.php b/scaffold/tests/phpunit/Traits/EnvTrait.php index fd645f7..2a13507 100644 --- a/scaffold/tests/phpunit/Traits/EnvTrait.php +++ b/scaffold/tests/phpunit/Traits/EnvTrait.php @@ -63,7 +63,7 @@ public static function envIsSet(string $name): bool { /** * Check if an environment variable is not set. */ - public static function envIsUnset($name): bool { + public static function envIsUnset(string $name): bool { return getenv($name) === FALSE; } diff --git a/scaffold/tests/phpunit/Traits/FileTrait.php b/scaffold/tests/phpunit/Traits/FileTrait.php index c5cf776..02a409b 100644 --- a/scaffold/tests/phpunit/Traits/FileTrait.php +++ b/scaffold/tests/phpunit/Traits/FileTrait.php @@ -9,7 +9,7 @@ */ trait FileTrait { - public function fileFindDir(string $file, $start = NULL) { + public function fileFindDir(string $file, ?string $start = NULL): string { if (empty($start)) { $start = dirname(__FILE__); } @@ -23,7 +23,7 @@ public function fileFindDir(string $file, $start = NULL) { if ($fs->exists($path)) { return $current; } - $current = dirname((string) $current); + $current = dirname($current); } throw new \RuntimeException('File not found: ' . $file); diff --git a/scaffold/tests/phpunit/Traits/JsonAssertTrait.php b/scaffold/tests/phpunit/Traits/JsonAssertTrait.php index fbaea24..9d162ec 100644 --- a/scaffold/tests/phpunit/Traits/JsonAssertTrait.php +++ b/scaffold/tests/phpunit/Traits/JsonAssertTrait.php @@ -6,17 +6,19 @@ use Helmich\JsonAssert\JsonAssertions; /** + * Trait JsonAssertTrait. * + * This trait provides a method to assert JSON data. */ trait JsonAssertTrait { use JsonAssertions; - public function assertJsonHasNoKey($jsonDocument, string $jsonPath, $message = NULL): void { - $result = (new JSONPath($jsonDocument))->find($jsonPath); + public function assertJsonHasNoKey(array $json_data, string $path, ?string $message = NULL): void { + $result = (new JSONPath($json_data))->find($path); if (isset($result[0])) { - $this->fail($message ?: sprintf("The JSON path '%s' exists, but it was expected not to.", $jsonPath)); + $this->fail($message ?: sprintf("The JSON path '%s' exists, but it was expected not to.", $path)); } $this->addToAssertionCount(1);