Skip to content

Commit

Permalink
refactor(Generator\WPUnit) remove Compat. layer
Browse files Browse the repository at this point in the history
  • Loading branch information
lucatume committed Sep 20, 2023
1 parent f86db19 commit d5962f6
Show file tree
Hide file tree
Showing 2 changed files with 115 additions and 132 deletions.
23 changes: 2 additions & 21 deletions src/Codeception/Lib/Generator/WPUnit.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Codeception\Lib\Generator\Shared\Classname;
use Codeception\Util\Shared\Namespaces;
use Codeception\Util\Template;
use PHPUnit\Runner\Version;
use tad\WPBrowser\Compat\Compatibility;

/**
Expand Down Expand Up @@ -74,13 +75,6 @@ public function test_it_works()
EOF;

/**
* The injectable compatibility layer.
*
* @var Compatibility
*/
protected $compatibilityLayer;

/**
* WPUnit constructor.
*
Expand All @@ -94,7 +88,6 @@ public function __construct($settings, $name, $baseClass)
$this->settings = $settings;
$this->name = $this->removeSuffix($name, 'Test');
$this->baseClass = $baseClass;
$this->compatibilityLayer = new Compatibility();
}

/**
Expand All @@ -106,7 +99,7 @@ public function produce()
{
$ns = $this->getNamespaceHeader($this->settings['namespace'] . '\\' . $this->name);

$phpunitSeries = $this->compatibilityLayer->phpunitVersion();
$phpunitSeries = Version::series();

/** @var string $phpunitSeries */
$voidReturnType = is_string($phpunitSeries) && version_compare($phpunitSeries, '8.0', '<') ?
Expand Down Expand Up @@ -155,16 +148,4 @@ protected function getTester()

return ltrim($testerFrag);
}

/**
* Injects the compatibility layer object.
*
* @param Compatibility $compatibility An instance of the compatibility layer.
*
* @return void
*/
public function setCompatibilityLayer(Compatibility $compatibility)
{
$this->compatibilityLayer = $compatibility;
}
}
224 changes: 113 additions & 111 deletions tests/unit/Codeception/Lib/Generator/WPUnitTest.php
Original file line number Diff line number Diff line change
@@ -1,118 +1,120 @@
<?php namespace Codeception\Lib\Generator;

use Codeception\TestCase\WPTestCase;
use lucatume\WPBrowser\Tests\Traits\WithUopz;
use PHPUnit\Runner\Version;
use ReflectionClass;
use tad\Codeception\SnapshotAssertions\SnapshotAssertions;
use tad\WPBrowser\Compat\Compatibility;

class WPUnitTest extends \Codeception\Test\Unit
{

use SnapshotAssertions;

/**
* A backup of the current PHPUnit Series env var.
* @var array|false|string
*/
protected $phpunitSeriesEnv;
/**
* @var \UnitTester
*/
protected $tester;

protected $compatibility;

/**
* It should scaffold PHPUnit v8 compatible code on series 8
*
* @test
* @dataProvider phpUnitEq8Series
*/
public function should_scaffold_php_unit_v_8_code_on_series_8($series)
{
$this->setPhpUnitSeriesTo('8.0');
$settings = ['namespace' => 'Acme'];
$name = 'SomeClass';
$generator = new WPUnit($settings, $name, WPTestCase::class);
$generator->setCompatibilityLayer($this->compatibility);

$code = $generator->produce();

$this->assertMatchesCodeSnapshot($code, 'php');
}

protected function setPhpUnitSeriesTo($series)
{
$this->compatibility = $this->make(Compatibility::class, [
'phpunitVersion' => $series
]);
}

public function phpUnitLt8Series()
{
return [
'5.5' => ['5.5'],
'5.5.5' => ['5.5'],
'6.2' => ['6.2'],
'6.2.3' => ['6.2'],
'7.5' => ['7.5'],
'7.5.6' => ['7.5'],
];
}

/**
* It should scaffold PHPUnit lt 8.0 compatible code on series lt 8
*
* @test
* @dataProvider phpUnitLt8Series
*/
public function should_scaffold_php_unit_lt_8_0_compatible_code_on_series_lt_8($series)
{
$this->setPhpUnitSeriesTo($series);
$settings = ['namespace' => 'Acme'];
$name = 'SomeClass';
$generator = new WPUnit($settings, $name, WPTestCase::class);
$generator->setCompatibilityLayer($this->compatibility);

$code = $generator->produce();

$this->assertMatchesCodeSnapshot($code, 'php');
}

/**
* It should correctly add the tester property if actor is set in the settings
*
* @test
*/
public function should_correctly_add_the_tester_property_if_actor_is_set_in_the_settings()
{
$this->setPhpUnitSeriesTo('6.0');
$settings = ['namespace' => 'Acme', 'actor' => 'Fixer'];
$name = 'SomeClass';
$generator = new WPUnit($settings, $name, WPTestCase::class);
$generator->setCompatibilityLayer($this->compatibility);

$code = $generator->produce();

$this->assertMatchesCodeSnapshot($code, 'php');
}

public function phpUnitEq8Series()
{
return [
'8.0' => ['8.0'],
'8.0.4' => ['8.0.4'],
'8.1' => ['8.1'],
'8.1.6' => ['8.1.6'],
];
}

protected function _before()
{
$this->phpunitSeriesEnv = getenv('WPBROWSER_PHPUNIT_SERIES');
}

protected function _after()
{
putenv('WPBROWSER_PHPUNIT_SERIES='.$this->phpunitSeriesEnv);
}
class WPUnitTest extends \Codeception\Test\Unit {
use WithUopz;
use SnapshotAssertions;

/**
* A backup of the current PHPUnit Series env var.
* @var array|false|string
*/
protected $phpunitSeriesEnv;
/**
* @var \UnitTester
*/
protected $tester;

/**
* @var string
*/
private $phpunitVersionPropertyBackup = null;

private function setPhpUnitSeriesTo( $series ) {
$reflectionClass = new ReflectionClass( Version::class );
$versionProp = $reflectionClass->getProperty( 'version' );
$versionProp->setAccessible( true );
$this->phpunitVersionPropertyBackup = $versionProp->getValue();
$versionProp->setValue( $series );
}

/**
* @after
*/
public function restorePhpunitVersionProperty() {
if ( null === $this->phpunitVersionPropertyBackup ) {
return;
}
$reflectionClass = new ReflectionClass( Version::class );
$versionProp = $reflectionClass->getProperty( 'version' );
$versionProp->setAccessible( true );
$versionProp->setValue( $this->phpunitVersionPropertyBackup );
$this->phpunitVersionPropertyBackup = null;
}

/**
* It should scaffold PHPUnit v8 compatible code on series 8
*
* @test
* @dataProvider phpUnitEq8Series
*/
public function should_scaffold_php_unit_v_8_code_on_series_8( $series ) {
$this->setPhpUnitSeriesTo( '8.0' );
$settings = [ 'namespace' => 'Acme' ];
$name = 'SomeClass';

$generator = new WPUnit( $settings, $name, WPTestCase::class );
$code = $generator->produce();

$this->assertMatchesCodeSnapshot( $code, 'php' );
}

public function phpUnitLt8Series() {
return [
'5.5' => [ '5.5' ],
'5.5.5' => [ '5.5' ],
'6.2' => [ '6.2' ],
'6.2.3' => [ '6.2' ],
'7.5' => [ '7.5' ],
'7.5.6' => [ '7.5' ],
];
}

/**
* It should scaffold PHPUnit lt 8.0 compatible code on series lt 8
*
* @test
* @dataProvider phpUnitLt8Series
*/
public function should_scaffold_php_unit_lt_8_0_compatible_code_on_series_lt_8( $series ) {
$this->setPhpUnitSeriesTo( $series );
$settings = [ 'namespace' => 'Acme' ];
$name = 'SomeClass';

$generator = new WPUnit( $settings, $name, WPTestCase::class );
$code = $generator->produce();

$this->assertMatchesCodeSnapshot( $code, 'php' );
}

/**
* It should correctly add the tester property if actor is set in the settings
*
* @test
*/
public function should_correctly_add_the_tester_property_if_actor_is_set_in_the_settings() {
$this->setPhpUnitSeriesTo( '6.0' );
$settings = [ 'namespace' => 'Acme', 'actor' => 'Fixer' ];
$name = 'SomeClass';

$generator = new WPUnit( $settings, $name, WPTestCase::class );
$code = $generator->produce();

$this->assertMatchesCodeSnapshot( $code, 'php' );
}

public function phpUnitEq8Series() {
return [
'8.0' => [ '8.0' ],
'8.0.4' => [ '8.0.4' ],
'8.1' => [ '8.1' ],
'8.1.6' => [ '8.1.6' ],
];
}
}

0 comments on commit d5962f6

Please sign in to comment.