|
10 | 10 | namespace SymfonyCasts\Bundle\ResetPassword\Tests\Functional;
|
11 | 11 |
|
12 | 12 | use PHPUnit\Framework\TestCase;
|
13 |
| -use Symfony\Component\Filesystem\Filesystem; |
14 |
| -use Symfony\Component\Process\Exception\ProcessFailedException; |
15 |
| -use Symfony\Component\Process\Process; |
| 13 | +use SymfonyCasts\Bundle\ResetPassword\SymfonyCastsResetPasswordBundle; |
| 14 | +use SymfonyCasts\InternalTestHelpers\AppTestHelper; |
| 15 | +use SymfonyCasts\InternalTestHelpers\TestProcessHelper; |
16 | 16 |
|
17 |
| -class ResetPasswordFunctionalTest extends TestCase |
| 17 | +final class ResetPasswordFunctionalTest extends TestCase |
18 | 18 | {
|
19 |
| - private string $appPath; |
| 19 | + private AppTestHelper $appTestHelper; |
20 | 20 |
|
21 | 21 | protected function setUp(): void
|
22 | 22 | {
|
23 |
| - $fs = new Filesystem(); |
24 |
| - $rootPath = realpath(\dirname(__DIR__, 2)); |
25 |
| - $cachePath = sprintf('%s/tests/tmp/cache', $rootPath); |
26 |
| - $this->appPath = sprintf('%s/app', $cachePath); |
27 |
| - $bundlePath = sprintf('%s/bundle', $cachePath); |
28 |
| - |
29 |
| - if ($fs->exists($cachePath)) { |
30 |
| - $fs->remove($cachePath); |
31 |
| - } |
32 |
| - |
33 |
| - $fs->mkdir($cachePath); |
34 |
| - |
35 |
| - // Copy bundle to a "repo" dir for tests |
36 |
| - $this->runProcess(sprintf('git clone %s %s/bundle', $rootPath, $cachePath), $cachePath); |
37 |
| - |
38 |
| - // Install Symfony Skeleton |
39 |
| - $this->runProcess(sprintf('composer create-project symfony/skeleton %s --prefer-dist', $this->appPath), $cachePath); |
40 |
| - |
41 |
| - // Setup the app as a "webapp" (similar to symfony new --webapp) |
42 |
| - $this->runProcess('composer require symfony/webapp-pack --prefer-dist', $this->appPath); |
43 |
| - |
44 |
| - // Tell composer to use "our" reset-password-bundle instead of fetching it from packagist. |
45 |
| - $composerJson = json_decode(file_get_contents(sprintf('%s/composer.json', $this->appPath)), associative: true, flags: \JSON_THROW_ON_ERROR); |
46 |
| - |
47 |
| - $composerJson['repositories']['symfonycasts/reset-password-bundle'] = [ |
48 |
| - 'type' => 'path', |
49 |
| - 'url' => $bundlePath, |
50 |
| - 'options' => [ |
51 |
| - 'versions' => [ |
52 |
| - 'symfonycasts/reset-password-bundle' => '9999.99', |
53 |
| - ], |
54 |
| - ], |
55 |
| - ]; |
56 |
| - |
57 |
| - file_put_contents(sprintf('%s/composer.json', $this->appPath), json_encode($composerJson, \JSON_THROW_ON_ERROR | \JSON_PRETTY_PRINT | \JSON_UNESCAPED_SLASHES)); |
58 |
| - |
59 |
| - $this->runProcess('composer require symfonycasts/reset-password-bundle', $this->appPath); |
60 |
| - |
61 |
| - $bundleVendorPath = sprintf('%s/vendor/symfonycasts/reset-password-bundle', $this->appPath); |
62 |
| - |
63 |
| - // DX - So we can test local changes without having to commit them. |
64 |
| - if (!is_link($bundleVendorPath)) { |
65 |
| - $fs->remove($bundleVendorPath); |
66 |
| - $fs->symlink($bundlePath, $bundleVendorPath); |
67 |
| - } |
| 23 | + $this->appTestHelper = (new AppTestHelper(SymfonyCastsResetPasswordBundle::class)) |
| 24 | + ->init('symfonycasts/reset-password-bundle') |
| 25 | + ; |
| 26 | + } |
68 | 27 |
|
69 |
| - // Ensure the app has fresh cache on the first test run |
70 |
| - $fs->remove(sprintf('%s/var/cache', $this->appPath)); |
| 28 | + public function testAppResetPasswordWorksInAWebApp(): void |
| 29 | + { |
| 30 | + $appPath = $this->appTestHelper->createAppForTest(); |
71 | 31 |
|
72 | 32 | // Use MakerBundle's `make:user` to create a user because I'm too lazy to make a fixture...
|
73 |
| - $this->runProcess('bin/console make:user --is-entity --with-password -n --identity-property-name email User ', $this->appPath); |
| 33 | + TestProcessHelper::runNow('bin/console make:user --is-entity --with-password -n --identity-property-name email User ', $appPath); |
74 | 34 |
|
75 | 35 | // Copy over app fixtures that were "generated" by maker-bundle - we should replace this with make:reset-password like
|
76 | 36 | // we do above for make:user... reason for the fixtures: lazy
|
77 |
| - $fixturesPath = sprintf('%s/tests/Fixtures/App', $rootPath); |
78 |
| - $fs->mirror($fixturesPath, $this->appPath, options: ['override' => true]); |
| 37 | + $fixturesPath = sprintf('%s/tests/Fixtures/App', $this->appTestHelper->rootPath); |
| 38 | + $this->appTestHelper->fs->mirror($fixturesPath, $appPath, options: ['override' => true]); |
79 | 39 |
|
80 | 40 | // Setup persistence
|
81 |
| - $this->runProcess('bin/console d:s:create', $this->appPath); |
82 |
| - } |
83 |
| - |
84 |
| - private function runProcess(string $cmd, string $workingDir): void |
85 |
| - { |
86 |
| - $process = Process::fromShellCommandline($cmd, $workingDir); |
87 |
| - |
88 |
| - if (0 !== ($exitCode = $process->run())) { |
89 |
| - dump($process->getErrorOutput()); |
| 41 | + TestProcessHelper::runNow('bin/console d:s:create', $appPath); |
90 | 42 |
|
91 |
| - throw new \RuntimeException(sprintf('Process Failed - Exit Code: %d - %s Cmd: %s', $exitCode, Process::$exitCodes[$exitCode] ?? '', $cmd)); |
92 |
| - } |
93 |
| - } |
94 |
| - |
95 |
| - public function testApp(): void |
96 |
| - { |
97 |
| - try { |
98 |
| - // Run test app tests |
99 |
| - Process::fromShellCommandline('bin/phpunit', $this->appPath) |
100 |
| - ->mustRun(); |
101 |
| - } catch (ProcessFailedException $exception) { |
102 |
| - $this->fail($exception->getMessage()); |
103 |
| - } |
| 43 | + // Run the unit tests (Fixtures/App/tests) in the web app. |
| 44 | + TestProcessHelper::runNow('bin/phpunit', $appPath); |
104 | 45 |
|
| 46 | + // If any of the tests within the app fail, an exception is thrown and |
| 47 | + // "this" test will fail. But we need this assertion because we are not |
| 48 | + // actually performing any assertions directly. |
105 | 49 | $this->expectNotToPerformAssertions();
|
106 | 50 | }
|
107 | 51 | }
|
0 commit comments