Skip to content

Commit

Permalink
Merge branch 'main' into 2.x
Browse files Browse the repository at this point in the history
* main:
  minor #324 use internal-test-helpers for app tests
  • Loading branch information
jrushlow committed Jun 14, 2024
2 parents 2e1c902 + 1b61be3 commit 9e01bdc
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 78 deletions.
10 changes: 9 additions & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,8 @@
"symfony/framework-bundle": "^6.4.5 | ^7.0",
"symfony/phpunit-bridge": "^6.4.5 | ^7.0",
"doctrine/doctrine-bundle": "^2.8",
"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": {
Expand All @@ -29,6 +30,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",
Expand Down
98 changes: 21 additions & 77 deletions tests/Functional/ResetPasswordFunctionalTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
}

0 comments on commit 9e01bdc

Please sign in to comment.