Skip to content

Commit

Permalink
never write into tests/Fixture dir
Browse files Browse the repository at this point in the history
  • Loading branch information
m-vo committed Mar 11, 2021
1 parent 69e7aa6 commit 4e5fa52
Show file tree
Hide file tree
Showing 9 changed files with 255 additions and 224 deletions.
72 changes: 36 additions & 36 deletions core-bundle/tests/Cache/ContaoCacheWarmerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,89 +21,89 @@
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Filesystem\Filesystem;
use Webmozart\PathUtil\Path;

class ContaoCacheWarmerTest extends TestCase
{
/**
* @var ContaoCacheWarmer
*/
private $warmer;

protected function setUp(): void
public static function setUpBeforeClass(): void
{
parent::setUp();
parent::setUpBeforeClass();

$this->warmer = $this->getCacheWarmer();
(new Filesystem())->mkdir([
Path::join(self::getTempDir(), 'var/cache'),
Path::join(self::getTempDir(), 'other'),
]);
}

protected function tearDown(): void
{
parent::tearDown();

$fs = new Filesystem();
$fs->remove($this->getFixturesDir().'/var/cache/contao');
(new Filesystem())->remove(
Path::join($this->getTempDir(), 'var/cache/contao')
);
}

public function testCreatesTheCacheFolder(): void
{
$container = $this->getContainerWithContaoConfiguration($this->getFixturesDir());
$container = $this->getContainerWithContaoConfiguration($this->getTempDir());
$container->set('database_connection', $this->createMock(Connection::class));

System::setContainer($container);

$warmer = $this->getCacheWarmer();
$warmer->warmUp($this->getFixturesDir().'/var/cache');

$this->assertFileExists($this->getFixturesDir().'/var/cache/contao');
$this->assertFileExists($this->getFixturesDir().'/var/cache/contao/config');
$this->assertFileExists($this->getFixturesDir().'/var/cache/contao/config/autoload.php');
$this->assertFileExists($this->getFixturesDir().'/var/cache/contao/config/config.php');
$this->assertFileExists($this->getFixturesDir().'/var/cache/contao/config/templates.php');
$this->assertFileExists($this->getFixturesDir().'/var/cache/contao/dca');
$this->assertFileExists($this->getFixturesDir().'/var/cache/contao/dca/tl_test.php');
$this->assertFileExists($this->getFixturesDir().'/var/cache/contao/languages');
$this->assertFileExists($this->getFixturesDir().'/var/cache/contao/languages/en');
$this->assertFileExists($this->getFixturesDir().'/var/cache/contao/languages/en/default.php');
$this->assertFileExists($this->getFixturesDir().'/var/cache/contao/sql');
$this->assertFileExists($this->getFixturesDir().'/var/cache/contao/sql/tl_test.php');
$warmer->warmUp(Path::join($this->getTempDir(), 'var/cache'));

$this->assertFileExists(Path::join($this->getTempDir(), 'var/cache/contao'));
$this->assertFileExists(Path::join($this->getTempDir(), 'var/cache/contao/config'));
$this->assertFileExists(Path::join($this->getTempDir(), 'var/cache/contao/config/autoload.php'));
$this->assertFileExists(Path::join($this->getTempDir(), 'var/cache/contao/config/config.php'));
$this->assertFileExists(Path::join($this->getTempDir(), 'var/cache/contao/config/templates.php'));
$this->assertFileExists(Path::join($this->getTempDir(), 'var/cache/contao/dca'));
$this->assertFileExists(Path::join($this->getTempDir(), 'var/cache/contao/dca/tl_test.php'));
$this->assertFileExists(Path::join($this->getTempDir(), 'var/cache/contao/languages'));
$this->assertFileExists(Path::join($this->getTempDir(), 'var/cache/contao/languages/en'));
$this->assertFileExists(Path::join($this->getTempDir(), 'var/cache/contao/languages/en/default.php'));
$this->assertFileExists(Path::join($this->getTempDir(), 'var/cache/contao/sql'));
$this->assertFileExists(Path::join($this->getTempDir(), 'var/cache/contao/sql/tl_test.php'));

$this->assertStringContainsString(
"\$GLOBALS['TL_TEST'] = true;",
file_get_contents($this->getFixturesDir().'/var/cache/contao/config/config.php')
file_get_contents(Path::join($this->getTempDir(), 'var/cache/contao/config/config.php'))
);

$this->assertStringContainsString(
"'dummy' => 'templates'",
file_get_contents($this->getFixturesDir().'/var/cache/contao/config/templates.php')
file_get_contents(Path::join($this->getTempDir(), 'var/cache/contao/config/templates.php'))
);

$this->assertStringContainsString(
"\$GLOBALS['TL_DCA']['tl_test'] = [\n",
file_get_contents($this->getFixturesDir().'/var/cache/contao/dca/tl_test.php')
file_get_contents(Path::join($this->getTempDir(), 'var/cache/contao/dca/tl_test.php'))
);

$this->assertStringContainsString(
"\$GLOBALS['TL_LANG']['MSC']['first']",
file_get_contents($this->getFixturesDir().'/var/cache/contao/languages/en/default.php')
file_get_contents(Path::join($this->getTempDir(), 'var/cache/contao/languages/en/default.php'))
);

$this->assertStringContainsString(
"\$this->arrFields = array (\n 'id' => 'int(10) unsigned NOT NULL auto_increment',\n);",
file_get_contents($this->getFixturesDir().'/var/cache/contao/sql/tl_test.php')
file_get_contents(Path::join($this->getTempDir(), 'var/cache/contao/sql/tl_test.php'))
);
}

public function testIsAnOptionalWarmer(): void
{
$this->assertTrue($this->warmer->isOptional());
$this->assertTrue($this->getCacheWarmer()->isOptional());
}

public function testDoesNotCreateTheCacheFolderIfThereAreNoContaoResources(): void
{
$warmer = $this->getCacheWarmer(null, null, 'empty-bundle');
$warmer->warmUp($this->getFixturesDir().'/var/cache/contao');
$warmer->warmUp(Path::join($this->getTempDir(), 'other'));

$this->assertFileNotExists($this->getFixturesDir().'/var/cache/contao');
$this->assertFileNotExists(Path::join($this->getTempDir(), 'var/cache/contao'));
}

public function testDoesNotCreateTheCacheFolderIfTheInstallationIsIncomplete(): void
Expand All @@ -121,9 +121,9 @@ public function testDoesNotCreateTheCacheFolderIfTheInstallationIsIncomplete():
;

$warmer = $this->getCacheWarmer($connection, $framework);
$warmer->warmUp($this->getFixturesDir().'/var/cache/contao');
$warmer->warmUp(Path::join($this->getTempDir(), 'var/cache/contao'));

$this->assertFileNotExists($this->getFixturesDir().'/var/cache/contao');
$this->assertFileNotExists(Path::join($this->getTempDir(), 'var/cache/contao'));
}

/**
Expand All @@ -140,7 +140,7 @@ private function getCacheWarmer(Connection $connection = null, ContaoFramework $
$framework = $this->mockContaoFramework();
}

$fixtures = $this->getFixturesDir().'/vendor/contao/'.$bundle.'/Resources/contao';
$fixtures = Path::join($this->getFixturesDir(), 'vendor/contao/'.$bundle.'/Resources/contao');

$filesystem = new Filesystem();
$finder = new ResourceFinder($fixtures);
Expand Down
7 changes: 5 additions & 2 deletions core-bundle/tests/Command/MigrateCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Symfony\Component\Config\FileLocator;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Filesystem\Filesystem;
use Webmozart\PathUtil\Path;

class MigrateCommandTest extends TestCase
{
Expand Down Expand Up @@ -65,7 +66,7 @@ public function testExecutesPendingMigrations(): void
*/
public function testExecutesRunOnceFiles(): void
{
$runOnceFile = $this->getFixturesDir().'/runonceFile.php';
$runOnceFile = Path::join($this->getTempDir(), 'runonceFile.php');

(new Filesystem())->dumpFile($runOnceFile, '<?php $GLOBALS["test_'.self::class.'"] = "executed";');

Expand All @@ -84,6 +85,8 @@ public function testExecutesRunOnceFiles(): void
$this->assertSame(0, $code);
$this->assertRegExp('/runonceFile.php/', $display);
$this->assertRegExp('/All migrations completed/', $display);

$this->assertFileNotExists($runOnceFile, 'File should be gone once executed');
}

public function testExecutesSchemaDiff(): void
Expand Down Expand Up @@ -218,7 +221,7 @@ private function getCommand(array $pendingMigrations = [], array $migrationResul
return new MigrateCommand(
$migrations,
$fileLocator,
$this->getFixturesDir(),
$this->getTempDir(),
$this->createMock(ContaoFramework::class),
$installer ?? $this->createMock(Installer::class)
);
Expand Down
26 changes: 14 additions & 12 deletions core-bundle/tests/Command/ResizeImagesCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,30 @@
use Symfony\Bridge\PhpUnit\ClockMock;
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\Filesystem\Filesystem;
use Webmozart\PathUtil\Path;

class ResizeImagesCommandTest extends TestCase
{
protected function setUp(): void
{
parent::setUp();

(new Filesystem())->mkdir(
Path::join($this->getTempDir(), 'assets/images')
);
}

protected function tearDown(): void
{
parent::tearDown();

$fs = new Filesystem();
$fs->remove($this->getFixturesDir().'/assets/images');
(new Filesystem())->remove(
Path::join($this->getTempDir(), 'assets/images')
);
}

public function testExecutesWithoutPendingImages(): void
{
$fs = new Filesystem();
$fs->mkdir($this->getFixturesDir().'/assets/images');

$storage = $this->createMock(DeferredImageStorageInterface::class);
$storage
->method('listPaths')
Expand All @@ -56,9 +64,6 @@ public function testExecutesWithoutPendingImages(): void

public function testResizesImages(): void
{
$fs = new Filesystem();
$fs->mkdir($this->getFixturesDir().'/assets/images');

$factory = $this->createMock(ImageFactoryInterface::class);
$factory
->method('create')
Expand Down Expand Up @@ -90,9 +95,6 @@ public function testResizesImages(): void

public function testTimeLimit(): void
{
$fs = new Filesystem();
$fs->mkdir($this->getFixturesDir().'/assets/images');

$factory = $this->createMock(ImageFactoryInterface::class);
$factory
->method('create')
Expand Down Expand Up @@ -143,7 +145,7 @@ private function getCommand(ImageFactoryInterface $factory = null, DeferredResiz
return new ResizeImagesCommand(
$factory ?? $this->createMock(ImageFactoryInterface::class),
$resizer ?? $this->createMock(DeferredResizerInterface::class),
$this->getFixturesDir().'/assets/images',
Path::join($this->getTempDir(), 'assets/images'),
$storage ?? $this->createMock(DeferredImageStorageInterface::class)
);
}
Expand Down
47 changes: 32 additions & 15 deletions core-bundle/tests/Command/SymlinksCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,27 +18,39 @@
use Symfony\Component\Console\Tester\CommandTester;
use Symfony\Component\EventDispatcher\EventDispatcherInterface;
use Symfony\Component\Filesystem\Filesystem;
use Webmozart\PathUtil\Path;

class SymlinksCommandTest extends TestCase
{
public static function setUpBeforeClass(): void
{
parent::setUpBeforeClass();

$filesystem = new Filesystem();

foreach (['assets', 'files', 'system', 'vendor'] as $directory) {
$filesystem->mirror(
Path::join(__DIR__.'/../Fixtures', $directory),
Path::join(self::getTempDir(), $directory)
);
}
}

protected function tearDown(): void
{
parent::tearDown();

$fs = new Filesystem();
$fs->remove($this->getFixturesDir().'/system/config');
$fs->remove($this->getFixturesDir().'/system/logs');
$fs->remove($this->getFixturesDir().'/system/themes');
$fs->remove($this->getFixturesDir().'/var');
$fs->remove($this->getFixturesDir().'/web');
(new Filesystem())->remove([
Path::join($this->getTempDir(), 'system/config'),
Path::join($this->getTempDir(), 'system/logs'),
Path::join($this->getTempDir(), 'system/themes'),
Path::join($this->getTempDir(), 'var'),
Path::join($this->getTempDir(), 'web'),
]);
}

public function testSymlinksTheContaoFolders(): void
{
$fs = new Filesystem();
$fs->mkdir($this->getFixturesDir().'/system/themes/default');
$fs->mkdir($this->getFixturesDir().'/var/logs');

$command = $this->getCommand();
$tester = new CommandTester($command);
$code = $tester->execute([]);
Expand All @@ -55,6 +67,11 @@ public function testSymlinksTheContaoFolders(): void
$this->assertRegExp('# web/assets +assets #', $display);
$this->assertRegExp('# web/system/themes +system/themes #', $display);
$this->assertRegExp('# system/logs +var/logs #', $display);

$this->assertFileExists(Path::join(self::getTempDir(), 'web/files/public'));
$this->assertDirectoryExists(Path::join(self::getTempDir(), 'web/system/modules/foobar'));
$this->assertDirectoryExists(Path::join(self::getTempDir(), 'web/system/themes/default'));
$this->assertDirectoryExists(Path::join(self::getTempDir(), 'web/assets'));
}

public function testConvertsAbsolutePathsToRelativePaths(): void
Expand All @@ -64,12 +81,12 @@ public function testConvertsAbsolutePathsToRelativePaths(): void
// Use \ as directory separator in $projectDir
$projectDir = new \ReflectionProperty(SymlinksCommand::class, 'projectDir');
$projectDir->setAccessible(true);
$projectDir->setValue($command, strtr($this->getFixturesDir(), '/', '\\'));
$projectDir->setValue($command, strtr($this->getTempDir(), '/', '\\'));

// Use / as directory separator in $path
$method = new \ReflectionMethod(SymlinksCommand::class, 'getRelativePath');
$method->setAccessible(true);
$relativePath = $method->invoke($command, $this->getFixturesDir().'/var/logs');
$relativePath = $method->invoke($command, Path::join($this->getTempDir(), 'var/logs'));

// The path should be normalized and shortened
$this->assertSame('var/logs', $relativePath);
Expand All @@ -78,10 +95,10 @@ public function testConvertsAbsolutePathsToRelativePaths(): void
private function getCommand(): SymlinksCommand
{
return new SymlinksCommand(
$this->getFixturesDir(),
$this->getTempDir(),
'files',
$this->getFixturesDir().'/var/logs',
new ResourceFinder($this->getFixturesDir().'/vendor/contao/test-bundle/Resources/contao'),
Path::join($this->getTempDir(), '/var/logs'),
new ResourceFinder(Path::join($this->getTempDir(), 'vendor/contao/test-bundle/Resources/contao')),
$this->createMock(EventDispatcherInterface::class)
);
}
Expand Down
13 changes: 8 additions & 5 deletions core-bundle/tests/Composer/ScriptHandlerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
use Contao\CoreBundle\Tests\TestCase;
use PHPUnit\Framework\MockObject\MockObject;
use Symfony\Component\Filesystem\Filesystem;
use Webmozart\PathUtil\Path;

class ScriptHandlerTest extends TestCase
{
Expand Down Expand Up @@ -49,7 +50,7 @@ public function testGeneratesARandomSecretIfTheConfigurationFileDoesNotExist():
$this->getComposerEvent(
[
'incenteev-parameters' => [
'file' => $this->getFixturesDir().'/app/config/parameters.yml',
'file' => Path::join($this->getFixturesDir(), 'app/config/parameters.yml'),
],
]
)
Expand All @@ -62,20 +63,22 @@ public function testDoesNotGenerateARandomSecretIfTheConfigurationFileExists():
{
$this->assertRandomSecretDoesNotExist();

$fs = new Filesystem();
$fs->touch($this->getFixturesDir().'/app/config/parameters.yml');
$parameterFile = Path::join($this->getTempDir(), 'parameters.yml');

$filesystem = new Filesystem();
$filesystem->dumpFile($parameterFile, '');

$this->handler->generateRandomSecret(
$this->getComposerEvent(
[
'incenteev-parameters' => [
'file' => __DIR__.'/../Fixtures/app/config/parameters.yml',
'file' => $parameterFile,
],
]
)
);

$fs->remove($this->getFixturesDir().'/app/config/parameters.yml');
$filesystem->remove($parameterFile);

$this->assertRandomSecretDoesNotExist();
}
Expand Down

0 comments on commit 4e5fa52

Please sign in to comment.