Skip to content

Commit 4517008

Browse files
Refactor test
1 parent 0135f06 commit 4517008

File tree

2 files changed

+141
-79
lines changed

2 files changed

+141
-79
lines changed

tests/_files/MyTestListener.php

Lines changed: 121 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
/*
3+
* This file is part of PHPUnit.
4+
*
5+
* (c) Sebastian Bergmann <sebastian@phpunit.de>
6+
*
7+
* For the full copyright and license information, please view the LICENSE
8+
* file that was distributed with this source code.
9+
*/
10+
use PHPUnit\Framework\AssertionFailedError;
11+
use PHPUnit\Framework\Test;
12+
use PHPUnit\Framework\TestListener;
13+
use PHPUnit\Framework\TestSuite;
14+
use PHPUnit\Framework\Warning;
15+
16+
final class MyTestListener implements TestListener
17+
{
18+
private $endCount = 0;
19+
20+
private $errorCount = 0;
21+
22+
private $failureCount = 0;
23+
24+
private $warningCount = 0;
25+
26+
private $notImplementedCount = 0;
27+
28+
private $riskyCount = 0;
29+
30+
private $skippedCount = 0;
31+
32+
private $startCount = 0;
33+
34+
public function addError(Test $test, \Throwable $t, float $time): void
35+
{
36+
$this->errorCount++;
37+
}
38+
39+
public function addWarning(Test $test, Warning $e, float $time): void
40+
{
41+
$this->warningCount++;
42+
}
43+
44+
public function addFailure(Test $test, AssertionFailedError $e, float $time): void
45+
{
46+
$this->failureCount++;
47+
}
48+
49+
public function addIncompleteTest(Test $test, \Throwable $t, float $time): void
50+
{
51+
$this->notImplementedCount++;
52+
}
53+
54+
public function addRiskyTest(Test $test, \Throwable $t, float $time): void
55+
{
56+
$this->riskyCount++;
57+
}
58+
59+
public function addSkippedTest(Test $test, \Throwable $t, float $time): void
60+
{
61+
$this->skippedCount++;
62+
}
63+
64+
public function startTestSuite(TestSuite $suite): void
65+
{
66+
}
67+
68+
public function endTestSuite(TestSuite $suite): void
69+
{
70+
}
71+
72+
public function startTest(Test $test): void
73+
{
74+
$this->startCount++;
75+
}
76+
77+
public function endTest(Test $test, float $time): void
78+
{
79+
$this->endCount++;
80+
}
81+
82+
public function endCount(): int
83+
{
84+
return $this->endCount;
85+
}
86+
87+
public function errorCount(): int
88+
{
89+
return $this->errorCount;
90+
}
91+
92+
public function failureCount(): int
93+
{
94+
return $this->failureCount;
95+
}
96+
97+
public function warningCount(): int
98+
{
99+
return $this->warningCount;
100+
}
101+
102+
public function notImplementedCount(): int
103+
{
104+
return $this->notImplementedCount;
105+
}
106+
107+
public function riskyCount(): int
108+
{
109+
return $this->riskyCount;
110+
}
111+
112+
public function skippedCount(): int
113+
{
114+
return $this->skippedCount;
115+
}
116+
117+
public function startCount(): int
118+
{
119+
return $this->startCount;
120+
}
121+
}

tests/unit/Framework/TestListenerTest.php

Lines changed: 20 additions & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -9,111 +9,52 @@
99
*/
1010
namespace PHPUnit\Framework;
1111

12-
class TestListenerTest extends TestCase implements TestListener
13-
{
14-
protected $endCount;
15-
16-
protected $errorCount;
17-
18-
protected $failureCount;
19-
20-
protected $warningCount;
21-
22-
protected $notImplementedCount;
12+
use MyTestListener;
2313

24-
protected $riskyCount;
25-
26-
protected $skippedCount;
27-
28-
protected $result;
14+
final class TestListenerTest extends TestCase
15+
{
16+
/**
17+
* @var TestResult
18+
*/
19+
private $result;
2920

30-
protected $startCount;
21+
/**
22+
* @var MyTestListener
23+
*/
24+
private $listener;
3125

3226
protected function setUp(): void
3327
{
34-
$this->result = new TestResult;
35-
$this->result->addListener($this);
36-
37-
$this->endCount = 0;
38-
$this->failureCount = 0;
39-
$this->notImplementedCount = 0;
40-
$this->riskyCount = 0;
41-
$this->skippedCount = 0;
42-
$this->startCount = 0;
43-
}
44-
45-
public function addError(Test $test, \Throwable $t, float $time): void
46-
{
47-
$this->errorCount++;
48-
}
49-
50-
public function addWarning(Test $test, Warning $e, float $time): void
51-
{
52-
$this->warningCount++;
53-
}
54-
55-
public function addFailure(Test $test, AssertionFailedError $e, float $time): void
56-
{
57-
$this->failureCount++;
58-
}
59-
60-
public function addIncompleteTest(Test $test, \Throwable $t, float $time): void
61-
{
62-
$this->notImplementedCount++;
63-
}
64-
65-
public function addRiskyTest(Test $test, \Throwable $t, float $time): void
66-
{
67-
$this->riskyCount++;
68-
}
28+
$this->result = new TestResult;
29+
$this->listener = new MyTestListener;
6930

70-
public function addSkippedTest(Test $test, \Throwable $t, float $time): void
71-
{
72-
$this->skippedCount++;
73-
}
74-
75-
public function startTestSuite(TestSuite $suite): void
76-
{
77-
}
78-
79-
public function endTestSuite(TestSuite $suite): void
80-
{
81-
}
82-
83-
public function startTest(Test $test): void
84-
{
85-
$this->startCount++;
86-
}
87-
88-
public function endTest(Test $test, float $time): void
89-
{
90-
$this->endCount++;
31+
$this->result->addListener($this->listener);
9132
}
9233

9334
public function testError(): void
9435
{
9536
$test = new \TestError;
9637
$test->run($this->result);
9738

98-
$this->assertEquals(1, $this->errorCount);
99-
$this->assertEquals(1, $this->endCount);
39+
$this->assertEquals(1, $this->listener->errorCount());
40+
$this->assertEquals(1, $this->listener->endCount());
10041
}
10142

10243
public function testFailure(): void
10344
{
10445
$test = new \Failure;
10546
$test->run($this->result);
10647

107-
$this->assertEquals(1, $this->failureCount);
108-
$this->assertEquals(1, $this->endCount);
48+
$this->assertEquals(1, $this->listener->failureCount());
49+
$this->assertEquals(1, $this->listener->endCount());
10950
}
11051

11152
public function testStartStop(): void
11253
{
11354
$test = new \Success;
11455
$test->run($this->result);
11556

116-
$this->assertEquals(1, $this->startCount);
117-
$this->assertEquals(1, $this->endCount);
57+
$this->assertEquals(1, $this->listener->startCount());
58+
$this->assertEquals(1, $this->listener->endCount());
11859
}
11960
}

0 commit comments

Comments
 (0)