1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* This file is part of graze/hamcrest-test-listener. |
5
|
|
|
* |
6
|
|
|
* Copyright (c) 2015 Nature Delivered Ltd. <https://www.graze.com> |
7
|
|
|
* |
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
9
|
|
|
* file that was distributed with this source code. |
10
|
|
|
* |
11
|
|
|
* @license https://github.com/graze/hamcrest-test-listener/blob/master/LICENSE MIT |
12
|
|
|
* @link https://github.com/graze/hamcrest-test-listener |
13
|
|
|
*/ |
14
|
|
|
|
15
|
|
|
namespace Hamcrest\Adapter\PHPUnit; |
16
|
|
|
|
17
|
|
|
use Exception; |
18
|
|
|
use Hamcrest\MatcherAssert; |
19
|
|
|
use Hamcrest\Util; |
20
|
|
|
use Mockery as m; |
21
|
|
|
use Mockery\Adapter\Phpunit\MockeryPHPUnitIntegration; |
22
|
|
|
use PHPUnit\Framework\Test; |
23
|
|
|
use PHPUnit\Framework\TestCase; |
24
|
|
|
use PHPUnit\Framework\TestResult; |
25
|
|
|
|
26
|
|
|
class TestListenerTest extends TestCase |
27
|
|
|
{ |
28
|
|
|
use MockeryPHPUnitIntegration; |
29
|
|
|
|
30
|
|
|
public static function setUpBeforeClass() |
31
|
|
|
{ |
32
|
|
|
// Require the Hamcrest global functions. |
33
|
|
|
Util::registerGlobalFunctions(); |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* @covers \Hamcrest\Adapter\PHPUnit\TestListener::startTest() |
38
|
|
|
*/ |
39
|
|
|
public function testShouldResetAssertionCountOnTestStart() |
40
|
|
|
{ |
41
|
|
|
$listener = new TestListener(); |
42
|
|
|
|
43
|
|
|
// Bump the hamcrest assertion count by 1. |
44
|
|
|
assertThat(true, is(true)); |
45
|
|
|
|
46
|
|
|
$this->assertSame(1, MatcherAssert::getCount(), 'Hamcrest is not counting assertions correctly.'); |
47
|
|
|
|
48
|
|
|
$listener->startTest(m::mock(Test::class)); |
49
|
|
|
|
50
|
|
|
$this->assertSame(0, MatcherAssert::getCount(), 'The listener did not reset the hamcrest assertion count.'); |
51
|
|
|
} |
52
|
|
|
|
53
|
|
|
/** |
54
|
|
|
* @covers \Hamcrest\Adapter\PHPUnit\TestListener::endTest() |
55
|
|
|
*/ |
56
|
|
View Code Duplication |
public function testShouldCallAddToAssertionCountOnTestEnd() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$listener = new TestListener(); |
59
|
|
|
|
60
|
|
|
$test = m::mock(TestCase::class); |
61
|
|
|
$test->shouldReceive('addToAssertionCount')->with(1)->once(); |
62
|
|
|
|
63
|
|
|
// Bump the hamcrest assertion count by 1. |
64
|
|
|
assertThat(true, is(true)); |
65
|
|
|
|
66
|
|
|
$listener->endTest($test, 0.0); |
67
|
|
|
} |
68
|
|
|
|
69
|
|
|
/** |
70
|
|
|
* @covers \Hamcrest\Adapter\PHPUnit\TestListener::endTest() |
71
|
|
|
*/ |
72
|
|
View Code Duplication |
public function testShouldOnlyCallAddToAssertionCountOnTestCases() |
|
|
|
|
73
|
|
|
{ |
74
|
|
|
$listener = new TestListener(); |
75
|
|
|
|
76
|
|
|
$test = m::mock(Test::class); |
77
|
|
|
$test->shouldReceive('addToAssertionCount')->never(); |
78
|
|
|
|
79
|
|
|
// Bump the hamcrest assertion count by 1. |
80
|
|
|
assertThat(true, is(true)); |
81
|
|
|
|
82
|
|
|
$listener->endTest($test, 0.0); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @covers \Hamcrest\Adapter\PHPUnit\TestListener::endTest() |
87
|
|
|
*/ |
88
|
|
|
public function testHandleExceptions() |
89
|
|
|
{ |
90
|
|
|
$listener = new TestListener(); |
91
|
|
|
|
92
|
|
|
$test = m::mock(TestCase::class); |
93
|
|
|
$exception = m::mock(Exception::class); |
94
|
|
|
$test->shouldReceive('addToAssertionCount')->andThrow($exception); |
95
|
|
|
|
96
|
|
|
$result = m::mock(TestResult::class); |
97
|
|
|
$test->shouldReceive('getTestResultObject')->times(1)->andReturn($result); |
98
|
|
|
|
99
|
|
|
$result->shouldReceive('addError')->with($test, $exception, 0.0)->times(1); |
100
|
|
|
|
101
|
|
|
$listener->endTest($test, 0.0); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.