Skip to content

Commit e8a9571

Browse files
Merge pull request #16 from assertwell/fix/track-initialization-of-fixtures
Simplify the setup of traits
2 parents 582f8db + d1a4e6e commit e8a9571

File tree

6 files changed

+157
-56
lines changed

6 files changed

+157
-56
lines changed

src/Constants.php

+11-21
Original file line numberDiff line numberDiff line change
@@ -14,20 +14,10 @@ trait Constants
1414
*
1515
* @var array[]
1616
*/
17-
private $_constants;
18-
19-
/**
20-
* @before
21-
*
22-
* @return void
23-
*/
24-
protected function resetConstants()
25-
{
26-
$this->_constants = [
27-
'created' => [],
28-
'updated' => [],
29-
];
30-
}
17+
private $constants = [
18+
'created' => [],
19+
'updated' => [],
20+
];
3121

3222
/**
3323
* @after
@@ -36,15 +26,15 @@ protected function resetConstants()
3626
*/
3727
protected function restoreConstants()
3828
{
39-
foreach ($this->_constants['updated'] as $name => $value) {
29+
foreach ($this->constants['updated'] as $name => $value) {
4030
if (defined($name)) {
4131
Runkit::constant_redefine($name, $value);
4232
} else {
4333
define($name, $value);
4434
}
4535
}
4636

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

7060
if (defined($name)) {
71-
if (! isset($this->_constants['updated'][$name])) {
72-
$this->_constants['updated'][$name] = constant($name);
61+
if (! isset($this->constants['updated'][$name])) {
62+
$this->constants['updated'][$name] = constant($name);
7363
}
7464

7565
try {
@@ -82,7 +72,7 @@ protected function setConstant($name, $value = null)
8272
));
8373
}
8474
} else {
85-
$this->_constants['created'][] = $name;
75+
$this->constants['created'][] = $name;
8676
define($name, $value);
8777
}
8878

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

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

107-
if (! isset($this->_constants[$name])) {
108-
$this->_constants['updated'][$name] = constant($name);
97+
if (! isset($this->constants[$name])) {
98+
$this->constants['updated'][$name] = constant($name);
10999
}
110100

111101
Runkit::constant_remove($name);

src/EnvironmentVariables.php

+4-14
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,7 @@ trait EnvironmentVariables
99
*
1010
* @var mixed[]
1111
*/
12-
private $_environmentVariables;
13-
14-
/**
15-
* @before
16-
*
17-
* @return void
18-
*/
19-
protected function resetEnvironmentVariableRegistry()
20-
{
21-
$this->_environmentVariables = [];
22-
}
12+
private $environmentVariables = [];
2313

2414
/**
2515
* @after
@@ -28,7 +18,7 @@ protected function resetEnvironmentVariableRegistry()
2818
*/
2919
protected function restoreEnvironmentVariables()
3020
{
31-
foreach ($this->_environmentVariables as $variable => $value) {
21+
foreach ($this->environmentVariables as $variable => $value) {
3222
putenv(false === $value ? $variable : "${variable}=${value}");
3323
}
3424
}
@@ -46,8 +36,8 @@ protected function restoreEnvironmentVariables()
4636
*/
4737
protected function setEnvironmentVariable($variable, $value = null)
4838
{
49-
if (! isset($this->_environmentVariables[$variable])) {
50-
$this->_environmentVariables[$variable] = getenv($variable);
39+
if (! isset($this->environmentVariables[$variable])) {
40+
$this->environmentVariables[$variable] = getenv($variable);
5141
}
5242

5343
putenv(null === $value ? $variable : "${variable}=${value}");

src/GlobalVariables.php

+9-19
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,10 @@ trait GlobalVariables
77
/**
88
* @var array[]
99
*/
10-
private $_globalVariables;
11-
12-
/**
13-
* @before
14-
*
15-
* @return void
16-
*/
17-
protected function resetGlobalVariables()
18-
{
19-
$this->_globalVariables = [
20-
'created' => [],
21-
'updated' => [],
22-
];
23-
}
10+
private $globalVariables = [
11+
'created' => [],
12+
'updated' => [],
13+
];
2414

2515
/**
2616
* @after
@@ -30,12 +20,12 @@ protected function resetGlobalVariables()
3020
protected function restoreGlobalVariables()
3121
{
3222
// Restore existing values.
33-
foreach ($this->_globalVariables['updated'] as $var => $value) {
23+
foreach ($this->globalVariables['updated'] as $var => $value) {
3424
$GLOBALS[$var] = $value;
3525
}
3626

3727
// Remove anything that was freshly-defined.
38-
foreach ($this->_globalVariables['created'] as $var) {
28+
foreach ($this->globalVariables['created'] as $var) {
3929
unset($GLOBALS[$var]);
4030
}
4131
}
@@ -52,9 +42,9 @@ protected function restoreGlobalVariables()
5242
protected function setGlobalVariable($variable, $value)
5343
{
5444
if (! isset($GLOBALS[$variable])) {
55-
$this->_globalVariables['created'][] = $variable;
56-
} elseif (! isset($this->_globalVariables['updated'][$variable])) {
57-
$this->_globalVariables['updated'][$variable] = $GLOBALS[$variable];
45+
$this->globalVariables['created'][] = $variable;
46+
} elseif (! isset($this->globalVariables['updated'][$variable])) {
47+
$this->globalVariables['updated'][$variable] = $GLOBALS[$variable];
5848
}
5949

6050
if (null === $value) {

tests/Concerns/RunkitTest.php

+8-2
Original file line numberDiff line numberDiff line change
@@ -56,8 +56,14 @@ public function it_should_skip_tests_that_require_runkit_if_it_is_unavailable()
5656
$method = new \ReflectionMethod($this->instance, 'requiresRunkit');
5757
$method->setAccessible(true);
5858

59-
$this->expectException(SkippedTestError::class);
59+
// Older versions of PHPUnit will actually try to mark this as skipped.
60+
try {
61+
$method->invoke($this->instance);
62+
} catch (SkippedTestError $e) {
63+
$this->assertInstanceOf(SkippedTestError::class, $e);
64+
return;
65+
}
6066

61-
$method->invoke($this->instance);
67+
$this->fail('Did not catch the expected SkippedTestError.');
6268
}
6369
}

tests/FixtureTest.php

+121
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,121 @@
1+
<?php
2+
3+
namespace Tests;
4+
5+
use AssertWell\PHPUnitGlobalState\Exceptions\RedefineException;
6+
use PHPUnit\Framework\SkippedTestError;
7+
use Symfony\Bridge\PhpUnit\SetUpTearDownTrait;
8+
9+
/**
10+
* Tests to ensure that state may be set in PHPUnit fixtures.
11+
*
12+
* @ticket https://github.com/assertwell/phpunit-global-state/issues/14
13+
*/
14+
class FixtureTest extends TestCase
15+
{
16+
use SetUpTearDownTrait;
17+
18+
protected $backupGlobalsBlacklist = [
19+
'FIXTURE_BEFORE_GLOBAL',
20+
'FIXTURE_SETUP_GLOBAL',
21+
];
22+
23+
public function doSetUp()
24+
{
25+
parent::setUp();
26+
27+
$this->setConstant('FIXTURE_SETUP_CONSTANT', true);
28+
$this->setEnvironmentVariable('FIXTURE_SETUP_ENV', 'abc');
29+
$this->setGlobalVariable('FIXTURE_SETUP_GLOBAL', true);
30+
}
31+
32+
/**
33+
* @before
34+
*/
35+
protected function defineInitialValues()
36+
{
37+
$this->setConstant('FIXTURE_BEFORE_CONSTANT', true);
38+
$this->setEnvironmentVariable('FIXTURE_BEFORE_ENV', 'xyz');
39+
$this->setGlobalVariable('FIXTURE_BEFORE_GLOBAL', true);
40+
}
41+
42+
/**
43+
* @test
44+
* @group Constants
45+
*/
46+
public function it_should_permit_constants_to_be_set_in_fixtures_method()
47+
{
48+
$this->assertTrue(
49+
defined('FIXTURE_SETUP_CONSTANT'),
50+
'The constant should have been defined in the setUp() method.'
51+
);
52+
$this->assertTrue(
53+
defined('FIXTURE_BEFORE_CONSTANT'),
54+
'The constant should have been defined in the @before method.'
55+
);
56+
57+
$this->restoreConstants();
58+
$this->assertFalse(
59+
defined('FIXTURE_SETUP_CONSTANT'),
60+
'The constant should have been undefined by restoreConstants().'
61+
);
62+
$this->assertFalse(
63+
defined('FIXTURE_BEFORE_CONSTANT'),
64+
'The constant should have been undefined by restoreConstants().'
65+
);
66+
}
67+
68+
/**
69+
* @test
70+
* @group EnvironmentVariables
71+
*/
72+
public function it_should_permit_environment_variables_to_be_set_in_fixtures_method()
73+
{
74+
$this->assertSame(
75+
'abc',
76+
getenv('FIXTURE_SETUP_ENV'),
77+
'The environment variable should have been defined in the setUp() method.'
78+
);
79+
$this->assertSame(
80+
'xyz',
81+
getenv('FIXTURE_BEFORE_ENV'),
82+
'The environment variable should have been defined in the @before method.'
83+
);
84+
85+
$this->restoreEnvironmentVariables();
86+
$this->assertFalse(
87+
getenv('FIXTURE_SETUP_ENV'),
88+
'The environment variable should have been undefined by restoreGlobalVariables().'
89+
);
90+
$this->assertFalse(
91+
getenv('FIXTURE_BEFORE_ENV'),
92+
'The environment variable should have been undefined by restoreGlobalVariables().'
93+
);
94+
}
95+
96+
/**
97+
* @test
98+
* @group GlobalVariables
99+
*/
100+
public function it_should_permit_global_variables_to_be_set_in_fixtures_method()
101+
{
102+
$this->assertTrue(
103+
isset($GLOBALS['FIXTURE_SETUP_GLOBAL']),
104+
'The global variable should have been defined in the setUp() method.'
105+
);
106+
$this->assertTrue(
107+
isset($GLOBALS['FIXTURE_BEFORE_GLOBAL']),
108+
'The global variable should have been defined in the @before method.'
109+
);
110+
111+
$this->restoreGlobalVariables();
112+
$this->assertFalse(
113+
isset($GLOBALS['FIXTURE_SETUP_GLOBAL']),
114+
'The global variable should have been undefined by restoreGlobalVariables().'
115+
);
116+
$this->assertFalse(
117+
isset($GLOBALS['FIXTURE_BEFORE_GLOBAL']),
118+
'The global variable should have been undefined by restoreGlobalVariables().'
119+
);
120+
}
121+
}

tests/GlobalVariablesTest.php

+4
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,10 @@
99
*/
1010
class GlobalVariablesTest extends TestCase
1111
{
12+
protected $backupGlobalsBlacklist = [
13+
'setGlobalVariable',
14+
];
15+
1216
/**
1317
* @test
1418
* @testdox setGlobalVariable() should be able to handle new global variables

0 commit comments

Comments
 (0)