|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/** |
| 6 | + * This file is part of CodeIgniter 4 framework. |
| 7 | + * |
| 8 | + * (c) CodeIgniter Foundation <[email protected]> |
| 9 | + * |
| 10 | + * For the full copyright and license information, please view |
| 11 | + * the LICENSE file that was distributed with this source code. |
| 12 | + */ |
| 13 | + |
| 14 | +namespace CodeIgniter; |
| 15 | + |
| 16 | +use CodeIgniter\Exceptions\InvalidArgumentException; |
| 17 | +use CodeIgniter\Test\CIUnitTestCase; |
| 18 | +use PHPUnit\Framework\Attributes\DataProvider; |
| 19 | +use PHPUnit\Framework\Attributes\Group; |
| 20 | + |
| 21 | +/** |
| 22 | + * @internal |
| 23 | + */ |
| 24 | +#[Group('Others')] |
| 25 | +final class EnvironmentDetectorTest extends CIUnitTestCase |
| 26 | +{ |
| 27 | + public function testDefaultsToEnvironmentConstant(): void |
| 28 | + { |
| 29 | + $detector = new EnvironmentDetector(); |
| 30 | + |
| 31 | + $this->assertSame(ENVIRONMENT, $detector->get()); |
| 32 | + } |
| 33 | + |
| 34 | + public function testExplicitEnvironmentOverridesConstant(): void |
| 35 | + { |
| 36 | + $detector = new EnvironmentDetector('production'); |
| 37 | + |
| 38 | + $this->assertSame('production', $detector->get()); |
| 39 | + } |
| 40 | + |
| 41 | + public function testTrimsSurroundingWhitespace(): void |
| 42 | + { |
| 43 | + $detector = new EnvironmentDetector(" production\n"); |
| 44 | + |
| 45 | + $this->assertSame('production', $detector->get()); |
| 46 | + } |
| 47 | + |
| 48 | + public function testRejectsEmptyString(): void |
| 49 | + { |
| 50 | + $this->expectException(InvalidArgumentException::class); |
| 51 | + $this->expectExceptionMessage('Environment cannot be an empty string.'); |
| 52 | + |
| 53 | + new EnvironmentDetector(''); // @phpstan-ignore argument.type |
| 54 | + } |
| 55 | + |
| 56 | + public function testRejectsWhitespaceOnlyString(): void |
| 57 | + { |
| 58 | + $this->expectException(InvalidArgumentException::class); |
| 59 | + $this->expectExceptionMessage('Environment cannot be an empty string.'); |
| 60 | + |
| 61 | + new EnvironmentDetector(" \t\n"); |
| 62 | + } |
| 63 | + |
| 64 | + public function testIsMatchesSingleEnvironment(): void |
| 65 | + { |
| 66 | + $detector = new EnvironmentDetector('staging'); |
| 67 | + |
| 68 | + $this->assertTrue($detector->is('staging')); |
| 69 | + $this->assertFalse($detector->is('production')); |
| 70 | + } |
| 71 | + |
| 72 | + public function testIsMatchesAnyOfSeveralEnvironments(): void |
| 73 | + { |
| 74 | + $detector = new EnvironmentDetector('staging'); |
| 75 | + |
| 76 | + $this->assertTrue($detector->is('production', 'staging', 'development')); |
| 77 | + $this->assertFalse($detector->is('production', 'development', 'testing')); |
| 78 | + } |
| 79 | + |
| 80 | + public function testIsReturnsFalseWhenNoEnvironmentsGiven(): void |
| 81 | + { |
| 82 | + $detector = new EnvironmentDetector('production'); |
| 83 | + |
| 84 | + $this->assertFalse($detector->is()); |
| 85 | + } |
| 86 | + |
| 87 | + public function testIsIsCaseSensitive(): void |
| 88 | + { |
| 89 | + $detector = new EnvironmentDetector('production'); |
| 90 | + |
| 91 | + $this->assertFalse($detector->is('Production')); |
| 92 | + $this->assertFalse($detector->is('PRODUCTION')); |
| 93 | + } |
| 94 | + |
| 95 | + /** |
| 96 | + * @param non-empty-string $environment |
| 97 | + */ |
| 98 | + #[DataProvider('provideBuiltInEnvironmentHelpers')] |
| 99 | + public function testBuiltInEnvironmentHelpers(string $environment, bool $isProduction, bool $isDevelopment, bool $isTesting): void |
| 100 | + { |
| 101 | + $detector = new EnvironmentDetector($environment); |
| 102 | + |
| 103 | + $this->assertSame($isProduction, $detector->isProduction()); |
| 104 | + $this->assertSame($isDevelopment, $detector->isDevelopment()); |
| 105 | + $this->assertSame($isTesting, $detector->isTesting()); |
| 106 | + } |
| 107 | + |
| 108 | + /** |
| 109 | + * @return iterable<string, array{string, bool, bool, bool}> |
| 110 | + */ |
| 111 | + public static function provideBuiltInEnvironmentHelpers(): iterable |
| 112 | + { |
| 113 | + yield 'production' => ['production', true, false, false]; |
| 114 | + |
| 115 | + yield 'development' => ['development', false, true, false]; |
| 116 | + |
| 117 | + yield 'testing' => ['testing', false, false, true]; |
| 118 | + |
| 119 | + yield 'custom' => ['staging', false, false, false]; |
| 120 | + } |
| 121 | + |
| 122 | + public function testResolvesAsSharedService(): void |
| 123 | + { |
| 124 | + $first = service('environmentdetector'); |
| 125 | + $second = service('environmentdetector'); |
| 126 | + |
| 127 | + $this->assertInstanceOf(EnvironmentDetector::class, $first); |
| 128 | + $this->assertSame($first, $second); |
| 129 | + } |
| 130 | +} |
0 commit comments