|
| 1 | +<?php |
| 2 | +declare(strict_types=1); |
| 3 | + |
| 4 | +namespace Zalas\PHPUnit\Globals\Tests; |
| 5 | + |
| 6 | +use PHPUnit\Framework\TestCase; |
| 7 | +use PHPUnit\Runner\AfterTestHook; |
| 8 | +use PHPUnit\Runner\BeforeTestHook; |
| 9 | +use Zalas\PHPUnit\Globals\AnnotationExtension; |
| 10 | +use Zalas\PHPUnit\Globals\Attribute\Env; |
| 11 | +use Zalas\PHPUnit\Globals\Attribute\Putenv; |
| 12 | +use Zalas\PHPUnit\Globals\Attribute\Server; |
| 13 | + |
| 14 | +#[Env('APP_ENV', 'test')] |
| 15 | +#[Server('APP_DEBUG', '0')] |
| 16 | +#[Putenv('APP_HOST', 'localhost')] |
| 17 | +class AttributeExtensionTest extends TestCase |
| 18 | +{ |
| 19 | + public function test_it_is_a_test_hook() |
| 20 | + { |
| 21 | + $this->assertInstanceOf(BeforeTestHook::class, new AnnotationExtension()); |
| 22 | + $this->assertInstanceOf(AfterTestHook::class, new AnnotationExtension()); |
| 23 | + } |
| 24 | + |
| 25 | + #[Env('APP_ENV', 'test_foo')] |
| 26 | + #[Server('APP_DEBUG', '1')] |
| 27 | + #[Putenv('APP_HOST', 'dev')] |
| 28 | + public function test_it_reads_global_variables_from_method_annotations() |
| 29 | + { |
| 30 | + $this->assertArraySubset(['APP_ENV' => 'test_foo'], $_ENV); |
| 31 | + $this->assertArraySubset(['APP_DEBUG' => '1'], $_SERVER); |
| 32 | + $this->assertArraySubset(['APP_HOST' => 'dev'], \getenv()); |
| 33 | + } |
| 34 | + |
| 35 | + public function test_it_reads_global_variables_from_class_annotations() |
| 36 | + { |
| 37 | + $this->assertArraySubset(['APP_ENV' => 'test'], $_ENV); |
| 38 | + $this->assertArraySubset(['APP_DEBUG' => '0'], $_SERVER); |
| 39 | + $this->assertArraySubset(['APP_HOST' => 'localhost'], \getenv()); |
| 40 | + } |
| 41 | + |
| 42 | + #[Env('FOO', 'foo')] |
| 43 | + #[Server('BAR', 'bar')] |
| 44 | + #[Putenv('BAZ', 'baz')] |
| 45 | + public function test_it_reads_additional_global_variables_from_methods() |
| 46 | + { |
| 47 | + $this->assertArraySubset(['APP_ENV' => 'test'], $_ENV); |
| 48 | + $this->assertArraySubset(['APP_DEBUG' => '0'], $_SERVER); |
| 49 | + $this->assertArraySubset(['APP_HOST' => 'localhost'], \getenv()); |
| 50 | + $this->assertArraySubset(['FOO' => 'foo'], $_ENV); |
| 51 | + $this->assertArraySubset(['BAR' => 'bar'], $_SERVER); |
| 52 | + $this->assertArraySubset(['BAZ' => 'baz'], \getenv()); |
| 53 | + } |
| 54 | + |
| 55 | + #[Env('APP_ENV', 'test_foo')] |
| 56 | + #[Env('APP_ENV', 'test_foo_bar')] |
| 57 | + #[Server('APP_DEBUG', '1')] |
| 58 | + #[Server('APP_DEBUG', '2')] |
| 59 | + #[PutEnv('APP_HOST', 'host1')] |
| 60 | + #[PutEnv('APP_HOST', 'host2')] |
| 61 | + public function test_it_reads_the_latest_var_defined() |
| 62 | + { |
| 63 | + $this->assertArraySubset(['APP_ENV' => 'test_foo_bar'], $_ENV); |
| 64 | + $this->assertArraySubset(['APP_DEBUG' => '2'], $_SERVER); |
| 65 | + $this->assertArraySubset(['APP_HOST' => 'host2'], \getenv()); |
| 66 | + } |
| 67 | + |
| 68 | + #[Env('APP_ENV')] |
| 69 | + #[Server('APP_DEBUG')] |
| 70 | + #[PutEnv('APP_HOST')] |
| 71 | + public function test_it_reads_empty_vars() |
| 72 | + { |
| 73 | + $this->assertArraySubset(['APP_ENV' => ''], $_ENV); |
| 74 | + $this->assertArraySubset(['APP_DEBUG' => ''], $_SERVER); |
| 75 | + $this->assertArraySubset(['APP_HOST' => ''], \getenv()); |
| 76 | + } |
| 77 | + |
| 78 | + #[Env('APP_ENV', unset: true)] |
| 79 | + #[Server('APP_DEBUG', unset: true)] |
| 80 | + #[PutEnv('APP_HOST', unset: true)] |
| 81 | + public function test_it_unsets_vars() |
| 82 | + { |
| 83 | + $this->assertArrayNotHasKey('APP_ENV', $_ENV); |
| 84 | + $this->assertArrayNotHasKey('APP_DEBUG', $_SERVER); |
| 85 | + $this->assertArrayNotHasKey('APP_HOST', \getenv()); |
| 86 | + } |
| 87 | + |
| 88 | + public function test_it_backups_the_state() |
| 89 | + { |
| 90 | + // this test is only here so the next one could verify the state is brought back |
| 91 | + |
| 92 | + $_ENV['FOO'] = 'env_foo'; |
| 93 | + $_SERVER['BAR'] = 'server_bar'; |
| 94 | + \putenv('FOO=putenv_foo'); |
| 95 | + \putenv('USER=foobar'); |
| 96 | + |
| 97 | + $this->assertArrayHasKey('FOO', $_ENV); |
| 98 | + $this->assertArrayHasKey('BAR', $_SERVER); |
| 99 | + $this->assertSame('putenv_foo', \getenv('FOO')); |
| 100 | + $this->assertSame('foobar', \getenv('USER')); |
| 101 | + } |
| 102 | + |
| 103 | + /** |
| 104 | + * @depends self::test_it_backups_the_state |
| 105 | + */ |
| 106 | + public function test_it_cleans_up_after_itself() |
| 107 | + { |
| 108 | + $this->assertArrayNotHasKey('FOO', $_ENV); |
| 109 | + $this->assertArrayNotHasKey('BAR', $_SERVER); |
| 110 | + $this->assertFalse(\getenv('FOO'), 'It removes environment variables initialised in a test.'); |
| 111 | + $this->assertNotSame('foobar', \getenv('USER'), 'It restores environment variables changed in a test.'); |
| 112 | + $this->assertNotFalse(\getenv('USER'), 'It restores environment variables changed in a test.'); |
| 113 | + } |
| 114 | + |
| 115 | + /** |
| 116 | + * Provides a replacement for the assertion deprecated in PHPUnit 8 and removed in PHPUnit 9. |
| 117 | + * @param array $subset |
| 118 | + * @param array $array |
| 119 | + */ |
| 120 | + public static function assertArraySubset($subset, $array, bool $checkForObjectIdentity = false, string $message = ''): void |
| 121 | + { |
| 122 | + self::assertSame($array, \array_replace_recursive($array, $subset)); |
| 123 | + } |
| 124 | +} |
0 commit comments