Skip to content

Commit

Permalink
Refactor test
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastianbergmann committed Oct 11, 2018
1 parent 0135f06 commit 4517008
Show file tree
Hide file tree
Showing 2 changed files with 141 additions and 79 deletions.
121 changes: 121 additions & 0 deletions tests/_files/MyTestListener.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
<?php
/*
* This file is part of PHPUnit.
*
* (c) Sebastian Bergmann <sebastian@phpunit.de>
*
* 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;
}
}
99 changes: 20 additions & 79 deletions tests/unit/Framework/TestListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,111 +9,52 @@
*/
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
{
$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
{
$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
{
$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());
}
}

0 comments on commit 4517008

Please sign in to comment.