Skip to content

Commit 9e01bdc

Browse files
committed
Merge branch 'main' into 2.x
* main: minor #324 use internal-test-helpers for app tests
2 parents 2e1c902 + 1b61be3 commit 9e01bdc

File tree

2 files changed

+30
-78
lines changed

2 files changed

+30
-78
lines changed

composer.json

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@
1717
"symfony/framework-bundle": "^6.4.5 | ^7.0",
1818
"symfony/phpunit-bridge": "^6.4.5 | ^7.0",
1919
"doctrine/doctrine-bundle": "^2.8",
20-
"symfony/process": "^6.4 | ^7.0 | ^7.1"
20+
"symfony/process": "^6.4 | ^7.0 | ^7.1",
21+
"symfonycasts/internal-test-helpers": "dev-main"
2122
},
2223
"autoload": {
2324
"psr-4": {
@@ -29,6 +30,13 @@
2930
"SymfonyCasts\\Bundle\\ResetPassword\\Tests\\": "tests/"
3031
}
3132
},
33+
"repositories": [
34+
{
35+
"type": "vcs",
36+
"name": "symfonycasts/internal-test-helpers",
37+
"url": "https://github.com/symfonycasts/internal-test-helpers"
38+
}
39+
],
3240
"scripts": {
3341
"tools:upgrade": [
3442
"@tools:upgrade:php-cs-fixer",

tests/Functional/ResetPasswordFunctionalTest.php

Lines changed: 21 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -10,98 +10,42 @@
1010
namespace SymfonyCasts\Bundle\ResetPassword\Tests\Functional;
1111

1212
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;
1616

17-
class ResetPasswordFunctionalTest extends TestCase
17+
final class ResetPasswordFunctionalTest extends TestCase
1818
{
19-
private string $appPath;
19+
private AppTestHelper $appTestHelper;
2020

2121
protected function setUp(): void
2222
{
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+
}
6827

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();
7131

7232
// 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);
7434

7535
// Copy over app fixtures that were "generated" by maker-bundle - we should replace this with make:reset-password like
7636
// 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]);
7939

8040
// 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);
9042

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);
10445

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.
10549
$this->expectNotToPerformAssertions();
10650
}
10751
}

0 commit comments

Comments
 (0)