diff --git a/tests/phpunit/Functional/AbstractFunctionalTestCase.php b/tests/phpunit/Functional/AbstractFunctionalTestCase.php index 74ab592..c1b36fd 100644 --- a/tests/phpunit/Functional/AbstractFunctionalTestCase.php +++ b/tests/phpunit/Functional/AbstractFunctionalTestCase.php @@ -182,4 +182,36 @@ protected function runArtifactCommand(?array $args = [], bool $expect_fail = FAL return $this->consoleApplicationRun($input, [], $expect_fail); } + /** + * Assert that files exist. + * + * @param string $path + * Repository location. + * @param array|string $files + * File or array of files. + */ + protected function assertFilesExist(string $path, array|string $files): void { + $files = is_array($files) ? $files : [$files]; + + foreach ($files as $file) { + $this->assertFileExists($path . DIRECTORY_SEPARATOR . $file); + } + } + + /** + * Assert that files do not exist. + * + * @param string $path + * Repository location. + * @param array|string $files + * File or array of files. + */ + protected function assertFilesNotExist(string $path, array|string $files): void { + $files = is_array($files) ? $files : [$files]; + + foreach ($files as $file) { + $this->assertFileDoesNotExist($path . DIRECTORY_SEPARATOR . $file); + } + } + } diff --git a/tests/phpunit/Functional/ForcePushModeTest.php b/tests/phpunit/Functional/ForcePushModeTest.php index 74541a1..6a1912b 100644 --- a/tests/phpunit/Functional/ForcePushModeTest.php +++ b/tests/phpunit/Functional/ForcePushModeTest.php @@ -70,10 +70,10 @@ public function testSubRepos(): void { $this->gitInitRepo($this->src . DIRECTORY_SEPARATOR . 'r3/r31/r311'); $this->fixtureCreateFile($this->src, 'r3/r31/r311/c'); - $this->gitAssertFilesExist($this->src, ['r1/c']); - $this->gitAssertFilesNotExist($this->src, ['r1/.git/index']); - $this->gitAssertFilesNotExist($this->src, ['r2/r21.git/index']); - $this->gitAssertFilesNotExist($this->src, ['r3/r31/r311/.git/index']); + $this->assertFilesExist($this->src, ['r1/c']); + $this->assertFilesNotExist($this->src, ['r1/.git/index']); + $this->assertFilesNotExist($this->src, ['r2/r21.git/index']); + $this->assertFilesNotExist($this->src, ['r3/r31/r311/.git/index']); $output = $this->assertArtifactCommandSuccess(['-vvv' => TRUE]); $this->assertStringContainsString(sprintf('Removing sub-repository "%s"', $this->fsGetAbsolutePath($this->src . DIRECTORY_SEPARATOR . 'r1/.git')), $output); @@ -81,15 +81,15 @@ public function testSubRepos(): void { $this->assertStringContainsString(sprintf('Removing sub-repository "%s"', $this->fsGetAbsolutePath($this->src . DIRECTORY_SEPARATOR . 'r3/r31/r311/.git')), $output); $this->gitAssertFixtureCommits(2, $this->dst, 'testbranch', ['Commit number 3', 'Deployment commit']); - $this->gitAssertFilesExist($this->dst, ['r1/c']); - $this->gitAssertFilesExist($this->dst, ['r2/r21/c']); - $this->gitAssertFilesExist($this->dst, ['r3/r31/r311/c']); - $this->gitAssertFilesNotExist($this->dst, ['r1/.git/index']); - $this->gitAssertFilesNotExist($this->dst, ['r1/.git']); - $this->gitAssertFilesNotExist($this->dst, ['r2/r21/.git/index']); - $this->gitAssertFilesNotExist($this->dst, ['r2/r21/.git']); - $this->gitAssertFilesNotExist($this->dst, ['r3/r31/311/.git/index']); - $this->gitAssertFilesNotExist($this->dst, ['r3/r31/311/.git']); + $this->assertFilesExist($this->dst, ['r1/c']); + $this->assertFilesExist($this->dst, ['r2/r21/c']); + $this->assertFilesExist($this->dst, ['r3/r31/r311/c']); + $this->assertFilesNotExist($this->dst, ['r1/.git/index']); + $this->assertFilesNotExist($this->dst, ['r1/.git']); + $this->assertFilesNotExist($this->dst, ['r2/r21/.git/index']); + $this->assertFilesNotExist($this->dst, ['r2/r21/.git']); + $this->assertFilesNotExist($this->dst, ['r3/r31/311/.git/index']); + $this->assertFilesNotExist($this->dst, ['r3/r31/311/.git']); } public function testCleanupAfterSuccess(): void { @@ -120,7 +120,7 @@ public function testGitignore(): void { $this->assertArtifactCommandSuccess(); $this->gitAssertFixtureCommits(2, $this->dst, 'testbranch', ['Deployment commit']); - $this->gitAssertFilesNotExist($this->dst, 'f3'); + $this->assertFilesNotExist($this->dst, 'f3'); // Now, remove the .gitignore and push again. $this->fixtureRemoveFile($this->src, '.gitignore'); @@ -139,8 +139,8 @@ public function testGitignoreCustom(): void { $this->assertArtifactCommandSuccess(['--gitignore' => $this->src . DIRECTORY_SEPARATOR . 'mygitignore']); $this->gitAssertFixtureCommits(2, $this->dst, 'testbranch', ['Deployment commit']); - $this->gitAssertFilesNotExist($this->dst, 'uic'); - $this->gitAssertFilesExist($this->dst, 'uc'); + $this->assertFilesNotExist($this->dst, 'uic'); + $this->assertFilesExist($this->dst, 'uc'); // Now, remove the .gitignore and push again. // We have to create 'uic' file since it was rightfully @@ -154,7 +154,7 @@ public function testGitignoreCustom(): void { $this->gitAssertFixtureCommits(3, $this->dst, 'testbranch', ['Deployment commit'], FALSE); $this->gitAssertFilesCommitted($this->dst, ['f1', 'f2', 'uic'], 'testbranch'); - $this->gitAssertFilesExist($this->dst, ['f1', 'f2', 'uic'], 'testbranch'); + $this->assertFilesExist($this->dst, ['f1', 'f2', 'uic']); $this->gitAssertFilesNotCommitted($this->dst, ['uc'], 'testbranch'); } @@ -179,8 +179,8 @@ public function testGitignoreCustomRemoveCommittedFiles(): void { $this->gitAssertFixtureCommits(2, $this->dst, 'testbranch', ['Custom third commit', 'Deployment commit'], FALSE); $this->gitAssertFilesCommitted($this->dst, ['.gitignore', 'f2', 'ic', 'd/cc', 'uc'], 'testbranch'); $this->gitAssertFilesNotCommitted($this->dst, ['f1', 'ii', 'd/ci', 'ui'], 'testbranch'); - $this->gitAssertFilesExist($this->dst, ['f2', 'ic', 'd/cc', 'uc'], 'testbranch'); - $this->gitAssertFilesNotExist($this->dst, ['f1', 'ii', 'd/ci', 'ui'], 'testbranch'); + $this->assertFilesExist($this->dst, ['f2', 'ic', 'd/cc', 'uc']); + $this->assertFilesNotExist($this->dst, ['f1', 'ii', 'd/ci', 'ui']); } public function testGitignoreCustomAllowlisting(): void { @@ -254,17 +254,17 @@ public function testGitignoreCustomAllowlisting(): void { 'vendor_cc', 'dir_other/vendor/ve_cc', 'vendor_com with space com.txt', ], 'testbranch'); - $this->gitAssertFilesExist($this->dst, [ + $this->assertFilesExist($this->dst, [ 'f2', 'ic', 'cc', 'uc', 'd_cc/sub_cc', 'd_ic/sub_ic', 'd_uc/sub_uc', 'vendor/ve_ii', - ], 'testbranch'); - $this->gitAssertFilesNotExist($this->dst, [ + ]); + $this->assertFilesNotExist($this->dst, [ 'f1', 'ii', 'ci', 'ui', 'ud', 'd_ci/sub_ci', 'd_ii/sub_ii', 'd_ui/sub_ui', 'd_ud/sub_ud', 'vendor_cc', 'dir_other/vendor/ve_cc', 'vendor_com with space com.txt', - ], 'testbranch'); + ]); } public function testBuildTag(): void { diff --git a/tests/phpunit/Functional/GeneralTest.php b/tests/phpunit/Functional/GeneralTest.php index 3c2049c..09c07ea 100644 --- a/tests/phpunit/Functional/GeneralTest.php +++ b/tests/phpunit/Functional/GeneralTest.php @@ -42,7 +42,8 @@ public function testInfo(): void { $this->assertStringContainsString('Cowardly refusing to push to remote. Use without --dry-run to perform an actual push.', $output); - $this->gitAssertFilesNotExist($this->dst, 'f1', $this->currentBranch); + $this->gitCheckout($this->dst, $this->currentBranch); + $this->assertFilesNotExist($this->dst, 'f1'); } public function testShowChanges(): void { @@ -56,7 +57,8 @@ public function testShowChanges(): void { $this->assertStringContainsString('Added changes:', $output); $this->assertStringContainsString('Cowardly refusing to push to remote. Use without --dry-run to perform an actual push.', $output); - $this->gitAssertFilesNotExist($this->dst, 'f1', $this->currentBranch); + $this->gitCheckout($this->dst, $this->currentBranch); + $this->assertFilesNotExist($this->dst, 'f1'); } public function testNoCleanup(): void { @@ -69,7 +71,7 @@ public function testNoCleanup(): void { $this->gitAssertCurrentBranch($this->src, $this->artifactBranch); $this->assertStringContainsString('Cowardly refusing to push to remote. Use without --dry-run to perform an actual push.', $output); - $this->gitAssertFilesNotExist($this->dst, 'f1', $this->currentBranch); + $this->assertFilesNotExist($this->dst, 'f1'); } public function testDebug(): void { @@ -97,7 +99,8 @@ public function testDebug(): void { $this->assertStringContainsString('Push result: Success', $output); $this->assertStringContainsString('Cowardly refusing to push to remote. Use without --dry-run to perform an actual push.', $output); - $this->gitAssertFilesNotExist($this->dst, 'f1', $this->currentBranch); + $this->gitCheckout($this->dst, $this->currentBranch); + $this->assertFilesNotExist($this->dst, 'f1'); } public function testDebugLogFile(): void { @@ -154,7 +157,8 @@ public function testDebugDisabled(): void { $this->assertStringNotContainsString('Debug messages enabled', $output); $this->assertStringContainsString('Cowardly refusing to push to remote. Use without --dry-run to perform an actual push.', $output); - $this->gitAssertFilesNotExist($this->dst, 'f1', $this->currentBranch); + $this->gitCheckout($this->dst, $this->currentBranch); + $this->assertFilesNotExist($this->dst, 'f1'); } } diff --git a/tests/phpunit/Traits/GitTrait.php b/tests/phpunit/Traits/GitTrait.php index c583147..3e35625 100644 --- a/tests/phpunit/Traits/GitTrait.php +++ b/tests/phpunit/Traits/GitTrait.php @@ -275,51 +275,7 @@ protected function gitAssertFixtureCommits(int $count, string $path, string $bra $this->assertEquals($expected_commits, $commits, 'All fixture commits are present'); if ($should_assert_files) { - $this->gitAssertFilesExist($this->dst, $expected_files, $branch); - } - } - - /** - * Assert that files exist in repository in specified branch. - * - * @param string $path - * Repository location. - * @param array|string $files - * File or array of files. - * @param string|null $branch - * Optional branch. If set, will be checked out before assertion. - */ - protected function gitAssertFilesExist(string $path, array|string $files, ?string $branch = NULL): void { - $files = is_array($files) ? $files : [$files]; - - if ($branch) { - $this->gitCheckout($path, $branch); - } - - foreach ($files as $file) { - $this->assertFileExists($path . DIRECTORY_SEPARATOR . $file); - } - } - - /** - * Assert that files do not exist in repository in specified branch. - * - * @param string $path - * Repository location. - * @param array|string $files - * File or array of files. - * @param string|null $branch - * Optional branch. If set, will be checked out before assertion. - */ - protected function gitAssertFilesNotExist(string $path, array|string $files, ?string $branch = NULL): void { - $files = is_array($files) ? $files : [$files]; - - if ($branch) { - $this->gitCheckout($path, $branch); - } - - foreach ($files as $file) { - $this->assertFileDoesNotExist($path . DIRECTORY_SEPARATOR . $file); + $this->assertFilesExist($this->dst, $expected_files); } }