From 1b61be32a3ca58670b5e9ee5e4db7973ea18ef05 Mon Sep 17 00:00:00 2001 From: Jesse Rushlow <40327885+jrushlow@users.noreply.github.com> Date: Fri, 14 Jun 2024 04:54:00 -0400 Subject: [PATCH] minor #324 use internal-test-helpers for app tests --- composer.json | 10 +- .../ResetPasswordFunctionalTest.php | 98 ++++--------------- 2 files changed, 30 insertions(+), 78 deletions(-) diff --git a/composer.json b/composer.json index 1639aee..2ddcef5 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,8 @@ "symfony/phpunit-bridge": "^5.4 | ^6.0 | ^7.0", "doctrine/doctrine-bundle": "^2.8", "doctrine/annotations": "^1.0", - "symfony/process": "^6.4 | ^7.0 | ^7.1" + "symfony/process": "^6.4 | ^7.0 | ^7.1", + "symfonycasts/internal-test-helpers": "dev-main" }, "autoload": { "psr-4": { @@ -30,6 +31,13 @@ "SymfonyCasts\\Bundle\\ResetPassword\\Tests\\": "tests/" } }, + "repositories": [ + { + "type": "vcs", + "name": "symfonycasts/internal-test-helpers", + "url": "https://github.com/symfonycasts/internal-test-helpers" + } + ], "scripts": { "tools:upgrade": [ "@tools:upgrade:php-cs-fixer", diff --git a/tests/FunctionalTests/ResetPasswordFunctionalTest.php b/tests/FunctionalTests/ResetPasswordFunctionalTest.php index 3d5dfd4..1013a88 100644 --- a/tests/FunctionalTests/ResetPasswordFunctionalTest.php +++ b/tests/FunctionalTests/ResetPasswordFunctionalTest.php @@ -10,98 +10,42 @@ namespace SymfonyCasts\Bundle\ResetPassword\Tests\Functional; use PHPUnit\Framework\TestCase; -use Symfony\Component\Filesystem\Filesystem; -use Symfony\Component\Process\Exception\ProcessFailedException; -use Symfony\Component\Process\Process; +use SymfonyCasts\Bundle\ResetPassword\SymfonyCastsResetPasswordBundle; +use SymfonyCasts\InternalTestHelpers\AppTestHelper; +use SymfonyCasts\InternalTestHelpers\TestProcessHelper; -class ResetPasswordFunctionalTest extends TestCase +final class ResetPasswordFunctionalTest extends TestCase { - private string $appPath; + private AppTestHelper $appTestHelper; protected function setUp(): void { - $fs = new Filesystem(); - $rootPath = realpath(\dirname(__DIR__, 2)); - $cachePath = sprintf('%s/tests/tmp/cache', $rootPath); - $this->appPath = sprintf('%s/app', $cachePath); - $bundlePath = sprintf('%s/bundle', $cachePath); - - if ($fs->exists($cachePath)) { - $fs->remove($cachePath); - } - - $fs->mkdir($cachePath); - - // Copy bundle to a "repo" dir for tests - $this->runProcess(sprintf('git clone %s %s/bundle', $rootPath, $cachePath), $cachePath); - - // Install Symfony Skeleton - $this->runProcess(sprintf('composer create-project symfony/skeleton %s --prefer-dist', $this->appPath), $cachePath); - - // Setup the app as a "webapp" (similar to symfony new --webapp) - $this->runProcess('composer require symfony/webapp-pack --prefer-dist', $this->appPath); - - // Tell composer to use "our" reset-password-bundle instead of fetching it from packagist. - $composerJson = json_decode(file_get_contents(sprintf('%s/composer.json', $this->appPath)), associative: true, flags: \JSON_THROW_ON_ERROR); - - $composerJson['repositories']['symfonycasts/reset-password-bundle'] = [ - 'type' => 'path', - 'url' => $bundlePath, - 'options' => [ - 'versions' => [ - 'symfonycasts/reset-password-bundle' => '9999.99', - ], - ], - ]; - - file_put_contents(sprintf('%s/composer.json', $this->appPath), json_encode($composerJson, \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES)); - - $this->runProcess('composer require symfonycasts/reset-password-bundle', $this->appPath); - - $bundleVendorPath = sprintf('%s/vendor/symfonycasts/reset-password-bundle', $this->appPath); - - // DX - So we can test local changes without having to commit them. - if (!is_link($bundleVendorPath)) { - $fs->remove($bundleVendorPath); - $fs->symlink($bundlePath, $bundleVendorPath); - } + $this->appTestHelper = (new AppTestHelper(SymfonyCastsResetPasswordBundle::class)) + ->init('symfonycasts/reset-password-bundle') + ; + } - // Ensure the app has fresh cache on the first test run - $fs->remove(sprintf('%s/var/cache', $this->appPath)); + public function testAppResetPasswordWorksInAWebApp(): void + { + $appPath = $this->appTestHelper->createAppForTest(); // Use MakerBundle's `make:user` to create a user because I'm too lazy to make a fixture... - $this->runProcess('bin/console make:user --is-entity --with-password -n --identity-property-name email User ', $this->appPath); + TestProcessHelper::runNow('bin/console make:user --is-entity --with-password -n --identity-property-name email User ', $appPath); // Copy over app fixtures that were "generated" by maker-bundle - we should replace this with make:reset-password like // we do above for make:user... reason for the fixtures: lazy - $fixturesPath = sprintf('%s/tests/Fixtures/App', $rootPath); - $fs->mirror($fixturesPath, $this->appPath, options: ['override' => true]); + $fixturesPath = sprintf('%s/tests/Fixtures/App', $this->appTestHelper->rootPath); + $this->appTestHelper->fs->mirror($fixturesPath, $appPath, options: ['override' => true]); // Setup persistence - $this->runProcess('bin/console d:s:create', $this->appPath); - } - - private function runProcess(string $cmd, string $workingDir): void - { - $process = Process::fromShellCommandline($cmd, $workingDir); - - if (0 !== ($exitCode = $process->run())) { - dump($process->getErrorOutput()); + TestProcessHelper::runNow('bin/console d:s:create', $appPath); - throw new \RuntimeException(sprintf('Process Failed - Exit Code: %d - %s Cmd: %s', $exitCode, Process::$exitCodes[$exitCode] ?? '', $cmd)); - } - } - - public function testApp(): void - { - try { - // Run test app tests - Process::fromShellCommandline('bin/phpunit', $this->appPath) - ->mustRun(); - } catch (ProcessFailedException $exception) { - $this->fail($exception->getMessage()); - } + // Run the unit tests (Fixtures/App/tests) in the web app. + TestProcessHelper::runNow('bin/phpunit', $appPath); + // If any of the tests within the app fail, an exception is thrown and + // "this" test will fail. But we need this assertion because we are not + // actually performing any assertions directly. $this->expectNotToPerformAssertions(); } }