Skip to content

Commit

Permalink
minor #18284 [2.3] fix mocking of some methods (xabbuh)
Browse files Browse the repository at this point in the history
This PR was merged into the 2.3 branch.

Discussion
----------

[2.3] fix mocking of some methods

| Q             | A
| ------------- | ---
| Branch?       | 2.3
| Bug fix?      | yes
| New feature?  | no
| BC breaks?    | no
| Deprecations? |no
| Tests pass?   | yes
| Fixed tickets | items 3, 4, 5, and 7 of sebastianbergmann/phpunit-mock-objects#299 (comment)
| License       | MIT
| Doc PR        |

Commits
-------

542cf6b [2.3] fix mocking of some methods
  • Loading branch information
Tobion committed Mar 23, 2016
2 parents a5965fb + 542cf6b commit e375161
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 20 deletions.
Expand Up @@ -19,7 +19,14 @@ public function testAutoloadMainExtension()
{
$container = $this->getMock(
'Symfony\\Component\\DependencyInjection\\ContainerBuilder',
array('getExtensionConfig', 'loadFromExtension', 'getParameterBag')
array(
'getExtensionConfig',
'loadFromExtension',
'getParameterBag',
'getDefinitions',
'getAliases',
'getExtensions',
)
);
$params = $this->getMock('Symfony\\Component\\DependencyInjection\\ParameterBag\\ParameterBag');

Expand Down
Expand Up @@ -104,9 +104,6 @@ public function testSubRequestWithDifferentMethod()
->will($this->returnValue(array()));

$context = new RequestContext();
$requestMatcher->expects($this->any())
->method('getContext')
->will($this->returnValue($context));

$listener = new RouterListener($requestMatcher, new RequestContext());
$listener->onKernelRequest($event);
Expand Down
27 changes: 19 additions & 8 deletions src/Symfony/Component/HttpKernel/Tests/HttpCache/HttpCacheTest.php
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\HttpKernel\HttpCache\HttpCache;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\HttpKernel\HttpKernelInterface;

/**
* @group time-sensitive
Expand All @@ -27,15 +28,11 @@ public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
->getMock();

// does not implement TerminableInterface
$kernelMock = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\HttpKernelInterface')
->disableOriginalConstructor()
->getMock();

$kernelMock->expects($this->never())
->method('terminate');
$kernel = new TestKernel();
$httpCache = new HttpCache($kernel, $storeMock);
$httpCache->terminate(Request::create('/'), new Response());

$kernel = new HttpCache($kernelMock, $storeMock);
$kernel->terminate(Request::create('/'), new Response());
$this->assertFalse($kernel->terminateCalled, 'terminate() is never called if the kernel class does not implement TerminableInterface');

// implements TerminableInterface
$kernelMock = $this->getMockBuilder('Symfony\\Component\\HttpKernel\\Kernel')
Expand Down Expand Up @@ -1228,3 +1225,17 @@ public function testEsiCacheRemoveValidationHeadersIfEmbeddedResponses()
$this->assertNull($this->response->getLastModified());
}
}

class TestKernel implements HttpKernelInterface
{
public $terminateCalled = false;

public function terminate(Request $request, Response $response)
{
$this->terminateCalled = true;
}

public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
{
}
}
26 changes: 18 additions & 8 deletions src/Symfony/Component/HttpKernel/Tests/KernelTest.php
Expand Up @@ -806,13 +806,7 @@ public function testTerminateReturnsSilentlyIfKernelIsNotBooted()
public function testTerminateDelegatesTerminationOnlyForTerminableInterface()
{
// does not implement TerminableInterface
$httpKernelMock = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')
->disableOriginalConstructor()
->getMock();

$httpKernelMock
->expects($this->never())
->method('terminate');
$httpKernel = new TestKernel();

$kernel = $this->getMockBuilder('Symfony\Component\HttpKernel\Tests\Fixtures\KernelForTest')
->disableOriginalConstructor()
Expand All @@ -821,11 +815,13 @@ public function testTerminateDelegatesTerminationOnlyForTerminableInterface()

$kernel->expects($this->once())
->method('getHttpKernel')
->will($this->returnValue($httpKernelMock));
->willReturn($httpKernel);

$kernel->setIsBooted(true);
$kernel->terminate(Request::create('/'), new Response());

$this->assertFalse($httpKernel->terminateCalled, 'terminate() is never called if the kernel class does not implement TerminableInterface');

// implements TerminableInterface
$httpKernelMock = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernel')
->disableOriginalConstructor()
Expand Down Expand Up @@ -903,3 +899,17 @@ protected function getKernelForInvalidLocateResource()
;
}
}

class TestKernel implements HttpKernelInterface
{
public $terminateCalled = false;

public function terminate()
{
$this->terminateCalled = true;
}

public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true)
{
}
}

0 comments on commit e375161

Please sign in to comment.