forked from mockery/mockery
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request mockery#638 from classmarkets/restore-phpunit-list…
…ener Restore the PHPUnit listener
- Loading branch information
Showing
4 changed files
with
175 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,64 @@ | ||
<?php | ||
/** | ||
* Mockery | ||
* | ||
* LICENSE | ||
* | ||
* This source file is subject to the new BSD license that is bundled | ||
* with this package in the file LICENSE.txt. | ||
* It is also available through the world-wide-web at this URL: | ||
* http://github.com/padraic/mockery/blob/master/LICENSE | ||
* If you did not receive a copy of the license and are unable to | ||
* obtain it through the world-wide-web, please send an email | ||
* to padraic@php.net so we can send you a copy immediately. | ||
* | ||
* @category Mockery | ||
* @package Mockery | ||
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com) | ||
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License | ||
*/ | ||
|
||
namespace Mockery\Adapter\Phpunit; | ||
|
||
class TestListener extends \PHPUnit_Framework_BaseTestListener | ||
{ | ||
/** | ||
* endTest is called after each test and checks if \Mockery::close() has | ||
* been called, and will let the test fail if it hasn't. | ||
* | ||
* @param PHPUnit_Framework_Test $test | ||
* @param float $time | ||
*/ | ||
public function endTest(\PHPUnit_Framework_Test $test, $time) | ||
{ | ||
if (!$test instanceof \PHPUnit_Framework_TestCase) { | ||
// We need the getTestResultObject and getStatus methods which are | ||
// not part of the interface. | ||
return; | ||
} | ||
|
||
if ($test->getStatus() !== \PHPUnit_Runner_BaseTestRunner::STATUS_PASSED) { | ||
// If the test didn't pass there is no guarantee that | ||
// verifyMockObjects and assertPostConditions have been called. | ||
// And even if it did, the point here is to prevent false | ||
// negatives, not to make failing tests fail for more reasons. | ||
return; | ||
} | ||
|
||
try { | ||
// The self() call is used as a sentinel. Anything that throws if | ||
// the container is closed already will do. | ||
\Mockery::self(); | ||
} catch (\LogicException $_) { | ||
return; | ||
} | ||
|
||
$e = new \PHPUnit_Framework_ExpectationFailedException(sprintf( | ||
"Mockery's expectations have not been verified. Make sure that \Mockery::close() is called at the end of the test. Consider using %s\MockeryPHPUnitIntegration or extending %s\MockeryTestCase.", | ||
__NAMESPACE__, | ||
__NAMESPACE__ | ||
)); | ||
$result = $test->getTestResultObject(); | ||
$result->addFailure($test, $e, $time); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
<?php | ||
/** | ||
* Mockery | ||
* | ||
* LICENSE | ||
* | ||
* This source file is subject to the new BSD license that is bundled | ||
* with this package in the file LICENSE.txt. | ||
* It is also available through the world-wide-web at this URL: | ||
* http://github.com/padraic/mockery/blob/master/LICENSE | ||
* If you did not receive a copy of the license and are unable to | ||
* obtain it through the world-wide-web, please send an email | ||
* to padraic@php.net so we can send you a copy immediately. | ||
* | ||
* @category Mockery | ||
* @package Mockery | ||
* @subpackage UnitTests | ||
* @copyright Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com) | ||
* @license http://github.com/padraic/mockery/blob/master/LICENSE New BSD License | ||
*/ | ||
|
||
class Mockery_Adapter_Phpunit_TestListenerTest extends PHPUnit_Framework_TestCase | ||
{ | ||
protected function setUp() | ||
{ | ||
// We intentionally test the static container here. That is what the | ||
// listener will check. | ||
$this->container = \Mockery::getContainer(); | ||
$this->listener = new \Mockery\Adapter\Phpunit\TestListener(); | ||
$this->testResult = new \PHPUnit_Framework_TestResult(); | ||
$this->test = new \Mockery_Adapter_Phpunit_EmptyTestCase(); | ||
|
||
$this->test->setTestResultObject($this->testResult); | ||
$this->testResult->addListener($this->listener); | ||
|
||
$this->assertTrue($this->testResult->wasSuccessful(), 'sanity check: empty test results should be considered successful'); | ||
} | ||
|
||
public function testSuccessOnClose() | ||
{ | ||
$mock = $this->container->mock(); | ||
$mock->shouldReceive('bar')->once(); | ||
$mock->bar(); | ||
|
||
// This is what MockeryPHPUnitIntegration and MockeryTestCase trait | ||
// will do. We intentionally call the static close method. | ||
$this->test->addToAssertionCount($this->container->mockery_getExpectationCount()); | ||
\Mockery::close(); | ||
|
||
$this->listener->endTest($this->test, 0); | ||
$this->assertTrue($this->testResult->wasSuccessful(), 'expected test result to indicate success'); | ||
} | ||
|
||
public function testFailureOnMissingClose() | ||
{ | ||
$mock = $this->container->mock(); | ||
$mock->shouldReceive('bar')->once(); | ||
|
||
$this->listener->endTest($this->test, 0); | ||
$this->assertFalse($this->testResult->wasSuccessful(), 'expected test result to indicate failure'); | ||
|
||
// Satisfy the expectation and close the global container now so we | ||
// don't taint the environment. | ||
$mock->bar(); | ||
\Mockery::close(); | ||
} | ||
} | ||
|
||
class Mockery_Adapter_Phpunit_EmptyTestCase extends PHPUnit_Framework_TestCase | ||
{ | ||
public function getStatus() | ||
{ | ||
return \PHPUnit_Runner_BaseTestRunner::STATUS_PASSED; | ||
} | ||
} |