Skip to content

Commit

Permalink
Fix the global state unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
leofeyer committed May 6, 2022
1 parent a5c8c84 commit 32306fa
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 17 deletions.
7 changes: 7 additions & 0 deletions calendar-bundle/tests/EventListener/SitemapListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,13 @@

class SitemapListenerTest extends ContaoTestCase
{
protected function tearDown(): void
{
unset($GLOBALS['TL_CONFIG']);

parent::tearDown();
}

public function testNothingIsAddedIfNoPublishedCalendar(): void
{
$adapters = [
Expand Down
12 changes: 6 additions & 6 deletions core-bundle/src/Resources/contao/library/Contao/Input.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,23 @@ class Input
/**
* Parameters set via setGet() are stored by request
*
* @var \WeakMap<Request,array<string,array|string>>
* @var \WeakMap<Request,array<string,array|string>>|null
*/
private static \WeakMap $setGet;
private static \WeakMap|null $setGet = null;

/**
* Parameters set via setPost() are stored by request
*
* @var \WeakMap<Request,array<string,array|string>>
* @var \WeakMap<Request,array<string,array|string>>|null
*/
private static \WeakMap $setPost;
private static \WeakMap|null $setPost = null;

/**
* Parameters set via setCookie() are stored by request
*
* @var \WeakMap<Request,array<string,array|string>>
* @var \WeakMap<Request,array<string,array|string>>|null
*/
private static \WeakMap $setCookie;
private static \WeakMap|null $setCookie = null;

/**
* Clean the global GPC arrays
Expand Down
6 changes: 6 additions & 0 deletions core-bundle/tests/Contao/DcaExtractorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@

namespace Contao\CoreBundle\Tests\Contao;

use Contao\Config;
use Contao\CoreBundle\Config\ResourceFinder;
use Contao\CoreBundle\Tests\TestCase;
use Contao\DcaExtractor;
use Contao\DcaLoader;
use Contao\System;
use Doctrine\DBAL\Connection;
use Symfony\Component\Config\FileLocator;
Expand Down Expand Up @@ -47,6 +49,10 @@ protected function setUp(): void

protected function tearDown(): void
{
unset($GLOBALS['TL_MIME'], $GLOBALS['TL_TEST'], $GLOBALS['TL_LANG'], $GLOBALS['TL_DCA']);

$this->resetStaticProperties([System::class, DcaExtractor::class, Config::class, DcaLoader::class]);

parent::tearDown();

(new Filesystem())->remove($this->getTempDir());
Expand Down
10 changes: 9 additions & 1 deletion core-bundle/tests/Contao/EnvironmentTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
use Contao\System;
use Symfony\Bridge\PhpUnit\ExpectDeprecationTrait;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\HttpFoundation\IpUtils;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\RequestStack;

Expand Down Expand Up @@ -50,7 +51,14 @@ protected function setUp(): void
protected function tearDown(): void
{
$this->restoreServerEnvGetPost();
$this->resetStaticProperties([Environment::class, [Environment::class, ['strSapi']], System::class]);

$this->resetStaticProperties([
Environment::class,
[Environment::class, ['strSapi']],
System::class,
Request::class,
IpUtils::class,
]);

parent::tearDown();
}
Expand Down
17 changes: 7 additions & 10 deletions core-bundle/tests/Twig/Loader/ContaoFilesystemLoaderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,13 @@

class ContaoFilesystemLoaderTest extends TestCase
{
protected function tearDown(): void
{
unset($GLOBALS['objPage']);

parent::tearDown();
}

public function testAddsPath(): void
{
$loader = $this->getContaoFilesystemLoader();
Expand Down Expand Up @@ -201,8 +208,6 @@ public function testGetCacheKeyDelegatesToThemeTemplate(): void
Path::join($basePath, 'templates/my/theme/text.html.twig'),
Path::normalize($loader->getCacheKey('@Contao/text.html.twig'))
);

unset($GLOBALS['objPage']);
}

public function testGetsSourceContext(): void
Expand Down Expand Up @@ -243,8 +248,6 @@ public function testGetSourceContextDelegatesToThemeTemplate(): void

$this->assertSame('@Contao_Theme_my_theme/text.html.twig', $source->getName());
$this->assertSame(Path::join($basePath, 'templates/my/theme/text.html.twig'), Path::normalize($source->getPath()));

unset($GLOBALS['objPage']);
}

public function testGetsSourceContextFromHtml5File(): void
Expand Down Expand Up @@ -303,8 +306,6 @@ public function testExistsDelegatesToThemeTemplate(): void

$this->assertTrue($loader->exists('@Contao/text.html.twig'));
$this->assertFalse($loader->exists('@Contao/foo.html.twig'));

unset($GLOBALS['objPage']);
}

/**
Expand Down Expand Up @@ -390,8 +391,6 @@ public function testIsFreshDelegatesToThemeTemplate(): void
$GLOBALS['objPage'] = $page;

$this->assertFalse($loader->isFresh('@Contao/text.html.twig', $cacheTime));

unset($GLOBALS['objPage']);
}

public function testGetsHierarchy(): void
Expand Down Expand Up @@ -622,8 +621,6 @@ public function testThrowsInvalidThemePathExceptionWhenGeneratingSlug(): void
$GLOBALS['objPage'] = $page;

$this->assertFalse($loader->exists('@Contao/foo.html.twig'));

unset($GLOBALS['objPage']);
}

private function getTemplateLocator(string $projectDir = '/', array $themePaths = [], array $bundles = [], array $bundlesMetadata = []): TemplateLocator
Expand Down
2 changes: 2 additions & 0 deletions test-case/src/ContaoTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ protected function backupServerEnvGetPost(): void
'_ENV' => $_ENV,
'_GET' => $_GET,
'_POST' => $_POST,
'_COOKIE' => $_COOKIE,
];
}

Expand All @@ -284,6 +285,7 @@ protected function restoreServerEnvGetPost(): void
$_ENV = $this->backupServerEnvGetPost['_ENV'] ?? $_ENV;
$_GET = $this->backupServerEnvGetPost['_GET'] ?? [];
$_POST = $this->backupServerEnvGetPost['_POST'] ?? [];
$_COOKIE = $this->backupServerEnvGetPost['_COOKIE'] ?? [];
}

/**
Expand Down

0 comments on commit 32306fa

Please sign in to comment.