|
| 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 | +} |
0 commit comments