Skip to content

Commit

Permalink
Cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolas-grekas committed Sep 2, 2019
1 parent 71a8ecb commit a461943
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 39 deletions.
7 changes: 6 additions & 1 deletion src/Symfony/Component/HttpKernel/Kernel.php
Expand Up @@ -354,7 +354,12 @@ public function getProjectDir()
{
if (null === $this->projectDir) {
$r = new \ReflectionObject($this);
$dir = $rootDir = \dirname($r->getFileName());

if (!file_exists($dir = $r->getFileName())) {
throw new \LogicException(sprintf('Cannot auto-detect project dir for kernel of class "%s".', $r->name));
}

$dir = $rootDir = \dirname($dir);
while (!file_exists($dir.'/composer.json')) {
if ($dir === \dirname($dir)) {
return $this->projectDir = $rootDir;
Expand Down
Expand Up @@ -34,4 +34,9 @@ public function isBooted()
{
return $this->booted;
}

public function getProjectDir()
{
return __DIR__;
}
}
23 changes: 14 additions & 9 deletions src/Symfony/Component/HttpKernel/Tests/KernelTest.php
Expand Up @@ -22,7 +22,6 @@
use Symfony\Component\HttpKernel\Config\EnvParametersResource;
use Symfony\Component\HttpKernel\DependencyInjection\ResettableServicePass;
use Symfony\Component\HttpKernel\DependencyInjection\ServicesResetter;
use Symfony\Component\HttpKernel\HttpKernel;
use Symfony\Component\HttpKernel\HttpKernelInterface;
use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\HttpKernel\Tests\Fixtures\KernelForOverrideName;
Expand Down Expand Up @@ -119,7 +118,7 @@ public function testBootSetsTheContainerToTheBundles()
public function testBootSetsTheBootedFlagToTrue()
{
// use test kernel to access isBooted()
$kernel = $this->getKernelForTest(['initializeBundles', 'initializeContainer']);
$kernel = $this->getKernel(['initializeBundles', 'initializeContainer']);
$kernel->boot();

$this->assertTrue($kernel->isBooted());
Expand Down Expand Up @@ -899,7 +898,7 @@ public function testServicesResetter()
*/
public function testKernelStartTimeIsResetWhileBootingAlreadyBootedKernel()
{
$kernel = $this->getKernelForTest(['initializeBundles'], true);
$kernel = $this->getKernel(['initializeBundles'], [], true);
$kernel->boot();
$preReBoot = $kernel->getStartTime();

Expand Down Expand Up @@ -957,15 +956,15 @@ protected function getBundle($dir = null, $parent = null, $className = null, $bu
*
* @return Kernel
*/
protected function getKernel(array $methods = [], array $bundles = [])
protected function getKernel(array $methods = [], array $bundles = [], $debug = false)
{
$methods[] = 'registerBundles';

$kernel = $this
->getMockBuilder('Symfony\Component\HttpKernel\Kernel')
->getMockBuilder(KernelForTest::class)
->setMethods($methods)
->setConstructorArgs(['test', false])
->getMockForAbstractClass()
->setConstructorArgs(['test', $debug])
->getMock()
;
$kernel->expects($this->any())
->method('registerBundles')
Expand All @@ -980,10 +979,11 @@ protected function getKernel(array $methods = [], array $bundles = [])

protected function getKernelForTest(array $methods = [], $debug = false)
{
$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest')
$kernel = $this->getMockBuilder(KernelForTest::class)
->setConstructorArgs(['test', $debug])
->setMethods($methods)
->getMock();
->getMock()
;
$p = new \ReflectionProperty($kernel, 'rootDir');
$p->setAccessible(true);
$p->setValue($kernel, __DIR__.'/Fixtures');
Expand All @@ -1004,6 +1004,11 @@ public function terminate()
public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
{
}

public function getProjectDir()
{
return __DIR__.'/Fixtures';
}
}

class CustomProjectDirKernel extends Kernel
Expand Down
Expand Up @@ -30,6 +30,10 @@ abstract protected function getStore();
*
* This test is time sensible: the $clockDelay could be adjust.
*
* It also fails when run with the global ./phpunit test suite.
*
* @group transient
*
* @requires extension pcntl
* @requires extension posix
* @requires function pcntl_sigwaitinfo
Expand Down
Expand Up @@ -84,42 +84,13 @@ public function testNeedsNormalizationBasic()
$this->assertTrue($this->chainEncoder->needsNormalization(self::FORMAT_2));
}

/**
* @dataProvider booleanProvider
*/
public function testNeedsNormalizationChainNormalizationAware($bool)
{
$chainEncoder = $this
->getMockBuilder('Symfony\Component\Serializer\Tests\Encoder\ChainNormalizationAwareEncoder')
->getMock();

$chainEncoder->method('supportsEncoding')->willReturn(true);
$chainEncoder->method('needsNormalization')->willReturn($bool);

$sut = new ChainEncoder([$chainEncoder]);

$this->assertEquals($bool, $sut->needsNormalization(self::FORMAT_1));
}

public function testNeedsNormalizationNormalizationAware()
{
$encoder = new NormalizationAwareEncoder();
$sut = new ChainEncoder([$encoder]);

$this->assertFalse($sut->needsNormalization(self::FORMAT_1));
}

public function booleanProvider()
{
return [
[true],
[false],
];
}
}

class ChainNormalizationAwareEncoder extends ChainEncoder implements NormalizationAwareInterface
{
}

class NormalizationAwareEncoder implements EncoderInterface, NormalizationAwareInterface
Expand Down

0 comments on commit a461943

Please sign in to comment.