Skip to content

Commit

Permalink
Simplify the resetting of state properties in each trait
Browse files Browse the repository at this point in the history
Instead of relying on `@before` fixtures to reset the [private] property that is tracking what's changed in each trait, set the default value in the property definition.
  • Loading branch information
stevegrunwell committed Oct 29, 2020
1 parent 582f8db commit a14f08b
Show file tree
Hide file tree
Showing 4 changed files with 142 additions and 54 deletions.
32 changes: 11 additions & 21 deletions src/Constants.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,10 @@ trait Constants
*
* @var array[]
*/
private $_constants;

/**
* @before
*
* @return void
*/
protected function resetConstants()
{
$this->_constants = [
'created' => [],
'updated' => [],
];
}
private $constants = [
'created' => [],
'updated' => [],
];

/**
* @after
Expand All @@ -36,15 +26,15 @@ protected function resetConstants()
*/
protected function restoreConstants()
{
foreach ($this->_constants['updated'] as $name => $value) {
foreach ($this->constants['updated'] as $name => $value) {
if (defined($name)) {
Runkit::constant_redefine($name, $value);
} else {
define($name, $value);
}
}

foreach ($this->_constants['created'] as $name) {
foreach ($this->constants['created'] as $name) {
if (defined($name)) {
Runkit::constant_remove($name);
}
Expand All @@ -68,8 +58,8 @@ protected function setConstant($name, $value = null)
$this->requiresRunkit('setConstant() requires Runkit be available, skipping.');

if (defined($name)) {
if (! isset($this->_constants['updated'][$name])) {
$this->_constants['updated'][$name] = constant($name);
if (! isset($this->constants['updated'][$name])) {
$this->constants['updated'][$name] = constant($name);
}

try {
Expand All @@ -82,7 +72,7 @@ protected function setConstant($name, $value = null)
));
}
} else {
$this->_constants['created'][] = $name;
$this->constants['created'][] = $name;
define($name, $value);
}

Expand All @@ -104,8 +94,8 @@ protected function deleteConstant($name)

$this->requiresRunkit('deleteConstant() requires Runkit be available, skipping.');

if (! isset($this->_constants[$name])) {
$this->_constants['updated'][$name] = constant($name);
if (! isset($this->constants[$name])) {
$this->constants['updated'][$name] = constant($name);
}

Runkit::constant_remove($name);
Expand Down
18 changes: 4 additions & 14 deletions src/EnvironmentVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,7 @@ trait EnvironmentVariables
*
* @var mixed[]
*/
private $_environmentVariables;

/**
* @before
*
* @return void
*/
protected function resetEnvironmentVariableRegistry()
{
$this->_environmentVariables = [];
}
private $environmentVariables = [];

/**
* @after
Expand All @@ -28,7 +18,7 @@ protected function resetEnvironmentVariableRegistry()
*/
protected function restoreEnvironmentVariables()
{
foreach ($this->_environmentVariables as $variable => $value) {
foreach ($this->environmentVariables as $variable => $value) {
putenv(false === $value ? $variable : "${variable}=${value}");
}
}
Expand All @@ -46,8 +36,8 @@ protected function restoreEnvironmentVariables()
*/
protected function setEnvironmentVariable($variable, $value = null)
{
if (! isset($this->_environmentVariables[$variable])) {
$this->_environmentVariables[$variable] = getenv($variable);
if (! isset($this->environmentVariables[$variable])) {
$this->environmentVariables[$variable] = getenv($variable);
}

putenv(null === $value ? $variable : "${variable}=${value}");
Expand Down
28 changes: 9 additions & 19 deletions src/GlobalVariables.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,20 +7,10 @@ trait GlobalVariables
/**
* @var array[]
*/
private $_globalVariables;

/**
* @before
*
* @return void
*/
protected function resetGlobalVariables()
{
$this->_globalVariables = [
'created' => [],
'updated' => [],
];
}
private $globalVariables = [
'created' => [],
'updated' => [],
];

/**
* @after
Expand All @@ -30,12 +20,12 @@ protected function resetGlobalVariables()
protected function restoreGlobalVariables()
{
// Restore existing values.
foreach ($this->_globalVariables['updated'] as $var => $value) {
foreach ($this->globalVariables['updated'] as $var => $value) {
$GLOBALS[$var] = $value;
}

// Remove anything that was freshly-defined.
foreach ($this->_globalVariables['created'] as $var) {
foreach ($this->globalVariables['created'] as $var) {
unset($GLOBALS[$var]);
}
}
Expand All @@ -52,9 +42,9 @@ protected function restoreGlobalVariables()
protected function setGlobalVariable($variable, $value)
{
if (! isset($GLOBALS[$variable])) {
$this->_globalVariables['created'][] = $variable;
} elseif (! isset($this->_globalVariables['updated'][$variable])) {
$this->_globalVariables['updated'][$variable] = $GLOBALS[$variable];
$this->globalVariables['created'][] = $variable;
} elseif (! isset($this->globalVariables['updated'][$variable])) {
$this->globalVariables['updated'][$variable] = $GLOBALS[$variable];
}

if (null === $value) {
Expand Down
118 changes: 118 additions & 0 deletions tests/FixtureTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
<?php

namespace Tests;

use AssertWell\PHPUnitGlobalState\Exceptions\RedefineException;
use PHPUnit\Framework\SkippedTestError;

/**
* Tests to ensure that state may be set in PHPUnit fixtures.
*
* @ticket https://github.com/assertwell/phpunit-global-state/issues/14
*/
class FixtureTest extends TestCase
{
protected $backupGlobalsBlacklist = [
'FIXTURE_BEFORE_GLOBAL',
'FIXTURE_SETUP_GLOBAL',
];

public function setUp(): void
{
parent::setUp();

$this->setConstant('FIXTURE_SETUP_CONSTANT', true);
$this->setEnvironmentVariable('FIXTURE_SETUP_ENV', 'abc');
$this->setGlobalVariable('FIXTURE_SETUP_GLOBAL', true);
}

/**
* @before
*/
protected function defineInitialValues()
{
$this->setConstant('FIXTURE_BEFORE_CONSTANT', true);
$this->setEnvironmentVariable('FIXTURE_BEFORE_ENV', 'xyz');
$this->setGlobalVariable('FIXTURE_BEFORE_GLOBAL', true);
}

/**
* @test
* @group Constants
*/
public function it_should_permit_constants_to_be_set_in_fixtures_method()
{
$this->assertTrue(
defined('FIXTURE_SETUP_CONSTANT'),
'The constant should have been defined in the setUp() method.'
);
$this->assertTrue(
defined('FIXTURE_BEFORE_CONSTANT'),
'The constant should have been defined in the @before method.'
);

$this->restoreConstants();
$this->assertFalse(
defined('FIXTURE_SETUP_CONSTANT'),
'The constant should have been undefined by restoreConstants().'
);
$this->assertFalse(
defined('FIXTURE_BEFORE_CONSTANT'),
'The constant should have been undefined by restoreConstants().'
);
}

/**
* @test
* @group EnvironmentVariables
*/
public function it_should_permit_environment_variables_to_be_set_in_fixtures_method()
{
$this->assertSame(
'abc',
getenv('FIXTURE_SETUP_ENV'),
'The environment variable should have been defined in the setUp() method.'
);
$this->assertSame(
'xyz',
getenv('FIXTURE_BEFORE_ENV'),
'The environment variable should have been defined in the @before method.'
);

$this->restoreEnvironmentVariables();
$this->assertFalse(
getenv('FIXTURE_SETUP_ENV'),
'The environment variable should have been undefined by restoreGlobalVariables().'
);
$this->assertFalse(
getenv('FIXTURE_BEFORE_ENV'),
'The environment variable should have been undefined by restoreGlobalVariables().'
);
}

/**
* @test
* @group GlobalVariables
*/
public function it_should_permit_global_variables_to_be_set_in_fixtures_method()
{
$this->assertTrue(
isset($GLOBALS['FIXTURE_SETUP_GLOBAL']),
'The global variable should have been defined in the setUp() method.'
);
$this->assertTrue(
isset($GLOBALS['FIXTURE_BEFORE_GLOBAL']),
'The global variable should have been defined in the @before method.'
);

$this->restoreGlobalVariables();
$this->assertFalse(
isset($GLOBALS['FIXTURE_SETUP_GLOBAL']),
'The global variable should have been undefined by restoreGlobalVariables().'
);
$this->assertFalse(
isset($GLOBALS['FIXTURE_BEFORE_GLOBAL']),
'The global variable should have been undefined by restoreGlobalVariables().'
);
}
}

0 comments on commit a14f08b

Please sign in to comment.