Skip to content

Commit

Permalink
Fix the global state unit tests (see #4618)
Browse files Browse the repository at this point in the history
Description
-----------

-

Commits
-------

32306fa Fix the global state unit tests
4405ac8 Suggestions for #4618 (#4)

Co-authored-by: Martin Auswöger <martin@auswoeger.com>
  • Loading branch information
leofeyer and ausi committed May 9, 2022
1 parent 3f1bf80 commit 222f391
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 12 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
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
2 changes: 2 additions & 0 deletions core-bundle/tests/Contao/InputTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ protected function tearDown(): void
{
unset($GLOBALS['TL_CONFIG']);

$_COOKIE = [];

$this->restoreServerEnvGetPost();
$this->resetStaticProperties([System::class, Input::class]);

Expand Down
4 changes: 4 additions & 0 deletions core-bundle/tests/PhpunitExtension/GlobalStateWatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,10 @@ private function buildStaticMembers(): string
continue;
}

if ($value instanceof \WeakMap && 0 === $value->count() && $property->hasType() && !$property->getType()->allowsNull()) {
continue;
}

if (\is_array($value)) {
$value = 'array('.\count($value).')';
}
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: 1 addition & 1 deletion test-case/src/ContaoTestCase.php
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ protected function resetStaticProperties(array $classNames): void
&& \is_callable([$class, 'reset'])
&& method_exists($class, 'reset')
&& $reflectionClass->getMethod('reset')->isStatic()
&& $reflectionClass->getMethod('reset')->getDeclaringClass() === $class
&& $reflectionClass->getMethod('reset')->getDeclaringClass()->getName() === $class
&& 0 === \count($reflectionClass->getMethod('reset')->getParameters())
) {
$class::reset();
Expand Down

0 comments on commit 222f391

Please sign in to comment.