Skip to content

Commit

Permalink
Merge pull request #64 from Yoast/feature/PHPUnit-10-tweak
Browse files Browse the repository at this point in the history
Tests: tweaks for PHPUnit 10.x compatibility
  • Loading branch information
jrfnl authored Nov 3, 2021
2 parents 33024ff + fd6d557 commit 0670f2e
Show file tree
Hide file tree
Showing 8 changed files with 194 additions and 14 deletions.
22 changes: 22 additions & 0 deletions tests/TestListeners/Fixtures/FailurePHPUnitGte7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures;

use PHPUnit\Framework\TestCase;

/**
* Fixture to generate a "failed test" to pass to the test listener.
*
* @requires PHPUnit 7.0
*/
class FailurePHPUnitGte7 extends TestCase {

/**
* Test resulting in a failed test.
*
* @return void
*/
protected function testForListener() {
$this->fail();
}
}
22 changes: 22 additions & 0 deletions tests/TestListeners/Fixtures/IncompletePHPUnitGte7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures;

use PHPUnit\Framework\TestCase;

/**
* Fixture to generate an "incomplete test" to pass to the test listener.
*
* @requires PHPUnit 7.0
*/
class IncompletePHPUnitGte7 extends TestCase {

/**
* Test resulting in a test marked as incomplete.
*
* @return void
*/
protected function testForListener() {
$this->markTestIncomplete( 'Test incomplete' );
}
}
22 changes: 22 additions & 0 deletions tests/TestListeners/Fixtures/RiskyPHPUnitGte7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures;

use PHPUnit\Framework\TestCase;

/**
* Fixture to generate a "risky test" to pass to the test listener.
*
* @requires PHPUnit 7.0
*/
class RiskyPHPUnitGte7 extends TestCase {

/**
* Test resulting in a test marked as risky.
*
* @return void
*/
protected function testForListener() {
$this->markAsRisky();
}
}
22 changes: 22 additions & 0 deletions tests/TestListeners/Fixtures/SkippedPHPUnitGte7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures;

use PHPUnit\Framework\TestCase;

/**
* Fixture to generate a "skipped test" to pass to the test listener.
*
* @requires PHPUnit 7.0
*/
class SkippedPHPUnitGte7 extends TestCase {

/**
* Test resulting in a test marked as skipped.
*
* @return void
*/
protected function testForListener() {
$this->markTestSkipped( 'Skipped test' );
}
}
22 changes: 22 additions & 0 deletions tests/TestListeners/Fixtures/SuccessPHPUnitGte7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures;

use PHPUnit\Framework\TestCase;

/**
* Fixture to generate a "successfull test" to pass to the test listener.
*
* @requires PHPUnit 7.0
*/
class SuccessPHPUnitGte7 extends TestCase {

/**
* Test resulting in a successfull test.
*
* @return void
*/
protected function testForListener() {
$this->assertTrue( true );
}
}
27 changes: 27 additions & 0 deletions tests/TestListeners/Fixtures/TestErrorPHPUnitGte7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures;

use Exception;
use PHPUnit\Framework\TestCase;

/**
* Fixture to generate a test error to pass to the test listener.
*
* @requires PHPUnit 7.0
*
* @coversNothing
*/
class TestErrorPHPUnitGte7 extends TestCase {

/**
* Test resulting in an error.
*
* @return void
*
* @throws Exception For test purposes.
*/
protected function testForListener() {
throw new Exception();
}
}
25 changes: 25 additions & 0 deletions tests/TestListeners/Fixtures/WarningPHPUnitGte7.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<?php

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures;

use PHPUnit\Framework\TestCase;
use PHPUnit\Framework\Warning as PHPUnit_Warning;

/**
* Fixture to generate a test warning to pass to the test listener.
*
* @requires PHPUnit 7.0
*/
class WarningPHPUnitGte7 extends TestCase {

/**
* Test resulting in a warning.
*
* @return void
*
* @throws PHPUnit_Warning For test purposes.
*/
protected function testForListener() {
throw new PHPUnit_Warning();
}
}
46 changes: 32 additions & 14 deletions tests/TestListeners/TestListenerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,11 @@

namespace Yoast\PHPUnitPolyfills\Tests\TestListeners;

use PHPUnit\Framework\TestCase as PHPUnitTestCase;
use PHPUnit\Framework\TestResult;
use Yoast\PHPUnitPolyfills\Autoload;
use Yoast\PHPUnitPolyfills\TestCases\TestCase;
use Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures\Failure;
use Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures\Incomplete;
use Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures\Risky;
use Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures\Skipped;
use Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures\Success;
use Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures\TestError;
use Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures\TestListenerImplementation;
use Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures\Warning;

/**
* Basic test for the PHPUnit version-based TestListenerDefaultImplementation setup.
Expand Down Expand Up @@ -53,7 +48,7 @@ protected function set_up() {
* @return void
*/
public function testError() {
$test = new TestError( 'runTest' );
$test = $this->getTestObject( 'TestError' );
$test->run( $this->result );

$this->assertSame( 1, $this->listener->startTestCount, 'test start count failed' );
Expand All @@ -72,7 +67,7 @@ public function testError() {
* @return void
*/
public function testWarning() {
$test = new Warning( 'runTest' );
$test = $this->getTestObject( 'Warning' );
$test->run( $this->result );

$this->assertSame( 1, $this->listener->startTestCount, 'test start count failed' );
Expand All @@ -86,7 +81,7 @@ public function testWarning() {
* @return void
*/
public function testFailure() {
$test = new Failure( 'runTest' );
$test = $this->getTestObject( 'Failure' );
$test->run( $this->result );

$this->assertSame( 1, $this->listener->startTestCount, 'test start count failed' );
Expand All @@ -100,7 +95,7 @@ public function testFailure() {
* @return void
*/
public function testIncomplete() {
$test = new Incomplete( 'runTest' );
$test = $this->getTestObject( 'Incomplete' );
$test->run( $this->result );

$this->assertSame( 1, $this->listener->startTestCount, 'test start count failed' );
Expand All @@ -118,7 +113,7 @@ public function testIncomplete() {
* @return void
*/
public function testRisky() {
$test = new Risky( 'runTest' );
$test = $this->getTestObject( 'Risky' );
$test->run( $this->result );

$this->assertSame( 1, $this->listener->startTestCount, 'test start count failed' );
Expand All @@ -132,7 +127,7 @@ public function testRisky() {
* @return void
*/
public function testSkipped() {
$test = new Skipped( 'runTest' );
$test = $this->getTestObject( 'Skipped' );
$test->run( $this->result );

$this->assertSame( 1, $this->listener->startTestCount, 'test start count failed' );
Expand All @@ -146,10 +141,33 @@ public function testSkipped() {
* @return void
*/
public function testStartStop() {
$test = new Success( 'runTest' );
$test = $this->getTestObject( 'Success' );
$test->run( $this->result );

$this->assertSame( 1, $this->listener->startTestCount, 'test start count failed' );
$this->assertSame( 1, $this->listener->endTestCount, 'test end count failed' );
}

/**
* Helper method to get the right Test class object.
*
* Toggles between different versions of the same Test class object:
* - Base version using `runTest()` method, compatible with PHPUnit < 10.0.
* - Version using `testForListener()` method, compatible with PHPUnit > 7.0.
*
* @param string $className Base class name of the test class to instantiate.
*
* @return PHPUnitTestCase
*/
private function getTestObject( $className ) {
$className = '\Yoast\PHPUnitPolyfills\Tests\TestListeners\Fixtures\\' . $className;
$testName = 'runTest';

if ( \version_compare( Autoload::getPHPUnitVersion(), '7.0.0', '>=' ) ) {
$className .= 'PHPUnitGte7';
$testName = 'testForListener';
}

return new $className( $testName );
}
}

0 comments on commit 0670f2e

Please sign in to comment.