|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace MikeFrancis\LaravelUnleash\Tests\Strategies; |
| 4 | + |
| 5 | +use GuzzleHttp\Client; |
| 6 | +use GuzzleHttp\Handler\MockHandler; |
| 7 | +use GuzzleHttp\Psr7\Response; |
| 8 | +use Illuminate\Contracts\Cache\Repository as Cache; |
| 9 | +use Illuminate\Contracts\Config\Repository as Config; |
| 10 | +use Illuminate\Http\Request; |
| 11 | +use MikeFrancis\LaravelUnleash\Tests\Stubs\ImplementedStrategy; |
| 12 | +use MikeFrancis\LaravelUnleash\Unleash; |
| 13 | +use PHPUnit\Framework\TestCase; |
| 14 | + |
| 15 | +class DynamicStrategyTest extends TestCase |
| 16 | +{ |
| 17 | + protected $mockHandler; |
| 18 | + |
| 19 | + protected $client; |
| 20 | + |
| 21 | + public function testWithoutArgs() |
| 22 | + { |
| 23 | + $featureName = 'someFeature'; |
| 24 | + |
| 25 | + $this->setMockHandler($featureName); |
| 26 | + |
| 27 | + $cache = $this->createMock(Cache::class); |
| 28 | + |
| 29 | + $request = $this->createMock(Request::class); |
| 30 | + |
| 31 | + $strategy = $this->createMock(ImplementedStrategy::class); |
| 32 | + $strategy->expects($this->exactly(2))->method('isEnabled') |
| 33 | + ->with([], $request) |
| 34 | + ->willReturn(true); |
| 35 | + |
| 36 | + $config = $this->getMockConfig($strategy); |
| 37 | + |
| 38 | + $unleash = new Unleash($this->client, $cache, $config, $request); |
| 39 | + |
| 40 | + $this->assertTrue($unleash->isFeatureEnabled($featureName)); |
| 41 | + $this->assertFalse($unleash->isFeatureDisabled($featureName)); |
| 42 | + } |
| 43 | + |
| 44 | + public function testWithArg() |
| 45 | + { |
| 46 | + $featureName = 'someFeature'; |
| 47 | + |
| 48 | + $this->setMockHandler($featureName); |
| 49 | + |
| 50 | + $cache = $this->createMock(Cache::class); |
| 51 | + |
| 52 | + $request = $this->createMock(Request::class); |
| 53 | + |
| 54 | + $strategy = $this->createMock(ImplementedStrategy::class); |
| 55 | + $strategy->expects($this->exactly(2))->method('isEnabled') |
| 56 | + ->with([], $request, true) |
| 57 | + ->willReturn(true); |
| 58 | + |
| 59 | + $config = $this->getMockConfig($strategy); |
| 60 | + |
| 61 | + $unleash = new Unleash($this->client, $cache, $config, $request); |
| 62 | + |
| 63 | + $this->assertTrue($unleash->isFeatureEnabled($featureName, true)); |
| 64 | + $this->assertFalse($unleash->isFeatureDisabled($featureName, true)); |
| 65 | + } |
| 66 | + |
| 67 | + public function testWithArgs() |
| 68 | + { |
| 69 | + $featureName = 'someFeature'; |
| 70 | + |
| 71 | + $this->setMockHandler($featureName); |
| 72 | + |
| 73 | + $cache = $this->createMock(Cache::class); |
| 74 | + |
| 75 | + $request = $this->createMock(Request::class); |
| 76 | + |
| 77 | + $strategy = $this->createMock(ImplementedStrategy::class); |
| 78 | + $strategy->expects($this->exactly(2))->method('isEnabled') |
| 79 | + ->with([], $request, 'foo', 'bar', 'baz') |
| 80 | + ->willReturn(true); |
| 81 | + |
| 82 | + $config = $this->getMockConfig($strategy); |
| 83 | + |
| 84 | + $unleash = new Unleash($this->client, $cache, $config, $request); |
| 85 | + |
| 86 | + $this->assertTrue($unleash->isFeatureEnabled($featureName, 'foo', 'bar', 'baz')); |
| 87 | + $this->assertFalse($unleash->isFeatureDisabled($featureName, 'foo', 'bar', 'baz')); |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param \PHPUnit\Framework\MockObject\MockObject $strategy |
| 92 | + * @return Config|\PHPUnit\Framework\MockObject\MockObject |
| 93 | + */ |
| 94 | + protected function getMockConfig(\PHPUnit\Framework\MockObject\MockObject $strategy) |
| 95 | + { |
| 96 | + $config = $this->createMock(Config::class); |
| 97 | + |
| 98 | + $config->expects($this->at(0)) |
| 99 | + ->method('get') |
| 100 | + ->with('unleash.isEnabled') |
| 101 | + ->willReturn(true); |
| 102 | + $config->expects($this->at(1)) |
| 103 | + ->method('get') |
| 104 | + ->with('unleash.cache.isEnabled') |
| 105 | + ->willReturn(false); |
| 106 | + $config->expects($this->at(2)) |
| 107 | + ->method('get') |
| 108 | + ->with('unleash.strategies') |
| 109 | + ->willReturn( |
| 110 | + [ |
| 111 | + 'testStrategy' => function () use ($strategy) { |
| 112 | + return $strategy; |
| 113 | + }, |
| 114 | + ] |
| 115 | + ); |
| 116 | + $config->expects($this->at(3)) |
| 117 | + ->method('get') |
| 118 | + ->with('unleash.strategies') |
| 119 | + ->willReturn( |
| 120 | + [ |
| 121 | + 'testStrategy' => function () use ($strategy) { |
| 122 | + return $strategy; |
| 123 | + }, |
| 124 | + ] |
| 125 | + ); |
| 126 | + return $config; |
| 127 | + } |
| 128 | + |
| 129 | + /** |
| 130 | + * @param string $featureName |
| 131 | + */ |
| 132 | + protected function setMockHandler(string $featureName): void |
| 133 | + { |
| 134 | + $this->mockHandler->append( |
| 135 | + new Response( |
| 136 | + 200, |
| 137 | + [], |
| 138 | + json_encode( |
| 139 | + [ |
| 140 | + 'features' => [ |
| 141 | + [ |
| 142 | + 'name' => $featureName, |
| 143 | + 'enabled' => true, |
| 144 | + 'strategies' => [ |
| 145 | + [ |
| 146 | + 'name' => 'testStrategy', |
| 147 | + ], |
| 148 | + ], |
| 149 | + ], |
| 150 | + ], |
| 151 | + ] |
| 152 | + ) |
| 153 | + ) |
| 154 | + ); |
| 155 | + } |
| 156 | + |
| 157 | + protected function setUp(): void |
| 158 | + { |
| 159 | + parent::setUp(); |
| 160 | + |
| 161 | + $this->mockHandler = new MockHandler(); |
| 162 | + |
| 163 | + $this->client = new Client( |
| 164 | + [ |
| 165 | + 'handler' => $this->mockHandler, |
| 166 | + ] |
| 167 | + ); |
| 168 | + } |
| 169 | +} |
0 commit comments