Skip to content

Commit

Permalink
Fixed more coding standards.
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexSkrypnyk committed Apr 19, 2024
1 parent bd2f143 commit 5ec22d5
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 16 deletions.
18 changes: 14 additions & 4 deletions scaffold/tests/phpunit/Dirs.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');
Expand All @@ -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));
}

}
18 changes: 12 additions & 6 deletions scaffold/tests/phpunit/Traits/ComposerTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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',
Expand Down Expand Up @@ -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;
}

}
2 changes: 1 addition & 1 deletion scaffold/tests/phpunit/Traits/EnvTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
4 changes: 2 additions & 2 deletions scaffold/tests/phpunit/Traits/FileTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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__);
}
Expand All @@ -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);
Expand Down
8 changes: 5 additions & 3 deletions scaffold/tests/phpunit/Traits/JsonAssertTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down

0 comments on commit 5ec22d5

Please sign in to comment.