From 451700848676926491aaa646819f8cd367d172c0 Mon Sep 17 00:00:00 2001 From: Sebastian Bergmann Date: Thu, 11 Oct 2018 14:01:49 +0200 Subject: [PATCH] Refactor test --- tests/_files/MyTestListener.php | 121 ++++++++++++++++++++++ tests/unit/Framework/TestListenerTest.php | 99 ++++-------------- 2 files changed, 141 insertions(+), 79 deletions(-) create mode 100644 tests/_files/MyTestListener.php diff --git a/tests/_files/MyTestListener.php b/tests/_files/MyTestListener.php new file mode 100644 index 00000000000..7e8e17d7f70 --- /dev/null +++ b/tests/_files/MyTestListener.php @@ -0,0 +1,121 @@ + + * + * For the full copyright and license information, please view the LICENSE + * file that was distributed with this source code. + */ +use PHPUnit\Framework\AssertionFailedError; +use PHPUnit\Framework\Test; +use PHPUnit\Framework\TestListener; +use PHPUnit\Framework\TestSuite; +use PHPUnit\Framework\Warning; + +final class MyTestListener implements TestListener +{ + private $endCount = 0; + + private $errorCount = 0; + + private $failureCount = 0; + + private $warningCount = 0; + + private $notImplementedCount = 0; + + private $riskyCount = 0; + + private $skippedCount = 0; + + private $startCount = 0; + + public function addError(Test $test, \Throwable $t, float $time): void + { + $this->errorCount++; + } + + public function addWarning(Test $test, Warning $e, float $time): void + { + $this->warningCount++; + } + + public function addFailure(Test $test, AssertionFailedError $e, float $time): void + { + $this->failureCount++; + } + + public function addIncompleteTest(Test $test, \Throwable $t, float $time): void + { + $this->notImplementedCount++; + } + + public function addRiskyTest(Test $test, \Throwable $t, float $time): void + { + $this->riskyCount++; + } + + public function addSkippedTest(Test $test, \Throwable $t, float $time): void + { + $this->skippedCount++; + } + + public function startTestSuite(TestSuite $suite): void + { + } + + public function endTestSuite(TestSuite $suite): void + { + } + + public function startTest(Test $test): void + { + $this->startCount++; + } + + public function endTest(Test $test, float $time): void + { + $this->endCount++; + } + + public function endCount(): int + { + return $this->endCount; + } + + public function errorCount(): int + { + return $this->errorCount; + } + + public function failureCount(): int + { + return $this->failureCount; + } + + public function warningCount(): int + { + return $this->warningCount; + } + + public function notImplementedCount(): int + { + return $this->notImplementedCount; + } + + public function riskyCount(): int + { + return $this->riskyCount; + } + + public function skippedCount(): int + { + return $this->skippedCount; + } + + public function startCount(): int + { + return $this->startCount; + } +} diff --git a/tests/unit/Framework/TestListenerTest.php b/tests/unit/Framework/TestListenerTest.php index f0ac4079219..d4e725f8d4c 100644 --- a/tests/unit/Framework/TestListenerTest.php +++ b/tests/unit/Framework/TestListenerTest.php @@ -9,85 +9,26 @@ */ namespace PHPUnit\Framework; -class TestListenerTest extends TestCase implements TestListener -{ - protected $endCount; - - protected $errorCount; - - protected $failureCount; - - protected $warningCount; - - protected $notImplementedCount; +use MyTestListener; - protected $riskyCount; - - protected $skippedCount; - - protected $result; +final class TestListenerTest extends TestCase +{ + /** + * @var TestResult + */ + private $result; - protected $startCount; + /** + * @var MyTestListener + */ + private $listener; protected function setUp(): void { - $this->result = new TestResult; - $this->result->addListener($this); - - $this->endCount = 0; - $this->failureCount = 0; - $this->notImplementedCount = 0; - $this->riskyCount = 0; - $this->skippedCount = 0; - $this->startCount = 0; - } - - public function addError(Test $test, \Throwable $t, float $time): void - { - $this->errorCount++; - } - - public function addWarning(Test $test, Warning $e, float $time): void - { - $this->warningCount++; - } - - public function addFailure(Test $test, AssertionFailedError $e, float $time): void - { - $this->failureCount++; - } - - public function addIncompleteTest(Test $test, \Throwable $t, float $time): void - { - $this->notImplementedCount++; - } - - public function addRiskyTest(Test $test, \Throwable $t, float $time): void - { - $this->riskyCount++; - } + $this->result = new TestResult; + $this->listener = new MyTestListener; - public function addSkippedTest(Test $test, \Throwable $t, float $time): void - { - $this->skippedCount++; - } - - public function startTestSuite(TestSuite $suite): void - { - } - - public function endTestSuite(TestSuite $suite): void - { - } - - public function startTest(Test $test): void - { - $this->startCount++; - } - - public function endTest(Test $test, float $time): void - { - $this->endCount++; + $this->result->addListener($this->listener); } public function testError(): void @@ -95,8 +36,8 @@ public function testError(): void $test = new \TestError; $test->run($this->result); - $this->assertEquals(1, $this->errorCount); - $this->assertEquals(1, $this->endCount); + $this->assertEquals(1, $this->listener->errorCount()); + $this->assertEquals(1, $this->listener->endCount()); } public function testFailure(): void @@ -104,8 +45,8 @@ public function testFailure(): void $test = new \Failure; $test->run($this->result); - $this->assertEquals(1, $this->failureCount); - $this->assertEquals(1, $this->endCount); + $this->assertEquals(1, $this->listener->failureCount()); + $this->assertEquals(1, $this->listener->endCount()); } public function testStartStop(): void @@ -113,7 +54,7 @@ public function testStartStop(): void $test = new \Success; $test->run($this->result); - $this->assertEquals(1, $this->startCount); - $this->assertEquals(1, $this->endCount); + $this->assertEquals(1, $this->listener->startCount()); + $this->assertEquals(1, $this->listener->endCount()); } }