Completed
Pull Request — master (#638)
by Peter
07:32
created

TestListener::endTest()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 32
Code Lines 15

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 32
rs 8.5806
cc 4
eloc 15
nc 4
nop 2
1
<?php
2
/**
3
 * Mockery
4
 *
5
 * LICENSE
6
 *
7
 * This source file is subject to the new BSD license that is bundled
8
 * with this package in the file LICENSE.txt.
9
 * It is also available through the world-wide-web at this URL:
10
 * http://github.com/padraic/mockery/blob/master/LICENSE
11
 * If you did not receive a copy of the license and are unable to
12
 * obtain it through the world-wide-web, please send an email
13
 * to [email protected] so we can send you a copy immediately.
14
 *
15
 * @category   Mockery
16
 * @package    Mockery
17
 * @copyright  Copyright (c) 2010 Pádraic Brady (http://blog.astrumfutura.com)
18
 * @license    http://github.com/padraic/mockery/blob/master/LICENSE New BSD License
19
 */
20
21
namespace Mockery\Adapter\Phpunit;
22
23
class TestListener extends \PHPUnit_Framework_BaseTestListener
24
{
25
    /**
26
     * endTest is called after each test and checks if \Mockery::close() has
27
     * been called, and will let the test fail if it hasn't.
28
     *
29
     * @param  PHPUnit_Framework_Test $test
30
     * @param  float                  $time
31
     */
32
    public function endTest(\PHPUnit_Framework_Test $test, $time)
33
    {
34
        if (!$test instanceof \PHPUnit_Framework_TestCase) {
35
            // We need the getTestResultObject and getStatus methods which are
36
            // not part of the interface.
37
            return;
38
        }
39
40
        if ($test->getStatus() !== \PHPUnit_Runner_BaseTestRunner::STATUS_PASSED) {
41
            // If the test didn't pass there is no guarantee that
42
            // verifyMockObjects and assertPostConditions have been called.
43
            // And even if it did, the point here is to prevent false
44
            // negatives, not to make failing tests fail for more reasons.
45
            return;
46
        }
47
48
        try {
49
            // The self() call is used as a sentinel. Anything that throws if
50
            // the container is closed already will do.
51
            \Mockery::self();
52
        } catch (\LogicException $_) {
53
            return;
54
        }
55
56
        $e = new \PHPUnit_Framework_ExpectationFailedException(sprintf(
57
            "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.",
58
            __NAMESPACE__,
59
            __NAMESPACE__
60
        ));
61
        $result = $test->getTestResultObject();
62
        $result->addFailure($test, $e, $time);
63
    }
64
}
65