Skip to content

Commit 3b75812

Browse files
committed
Add match any support
1 parent fa94c2d commit 3b75812

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

src/Context.php

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -336,6 +336,18 @@ public function matches(Condition ...$condition): bool
336336
return true;
337337
}
338338

339+
public function matchAny(Condition ...$condition): bool
340+
{
341+
foreach($condition as $cond)
342+
{
343+
if($cond->isSatisfied($this))
344+
{
345+
return true;
346+
}
347+
}
348+
return false;
349+
}
350+
339351
public function __debugInfo()
340352
{
341353
return [

tests/ContextTest.php

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Packaged\Tests\Context;
44

55
use Packaged\Config\Provider\ConfigProvider;
6+
use Packaged\Context\Conditions\ExpectEnvironment;
67
use Packaged\Context\Context;
78
use Packaged\Event\Channel\Channel;
89
use Packaged\Helpers\Arrays;
@@ -166,6 +167,58 @@ public function testExtendingContext()
166167
$this->assertSame($cnf, $ctx2->getConfig());
167168
$this->assertEquals('def', $ctx2->meta()->get('abc'));
168169
$this->assertEquals('456', $ctx2->routeData()->get('123'));
170+
}
169171

172+
/**
173+
* @param $environment
174+
* @param $condition
175+
* @param $expected
176+
*
177+
* @return void
178+
* @dataProvider providerMatches
179+
*/
180+
public function testMatches($environment, $condition, $expected)
181+
{
182+
$ctx = new Context();
183+
$ctx->setEnvironment($environment);
184+
self::assertEquals($expected, $ctx->matches($condition));
185+
}
186+
187+
public function providerMatches()
188+
{
189+
return [
190+
[Context::ENV_PROD, ExpectEnvironment::prod(), true],
191+
[Context::ENV_LOCAL, ExpectEnvironment::local(), true],
192+
[Context::ENV_DEV, ExpectEnvironment::dev(), true],
193+
[Context::ENV_PHPUNIT, ExpectEnvironment::phpunit(), true],
194+
[Context::ENV_PROD, ExpectEnvironment::local(), false],
195+
[Context::ENV_PROD, ExpectEnvironment::dev(), false],
196+
[Context::ENV_PROD, ExpectEnvironment::phpunit(), false],
197+
];
198+
}
199+
200+
/**
201+
* @param $environment
202+
* @param $conditions
203+
* @param $expected
204+
*
205+
* @return void
206+
* @dataProvider providerMatchAny
207+
*/
208+
public function testMatchAny($environment, array $conditions, $expected)
209+
{
210+
$ctx = new Context();
211+
$ctx->setEnvironment($environment);
212+
self::assertEquals($expected, $ctx->matchAny(...$conditions));
213+
}
214+
215+
public function providerMatchAny()
216+
{
217+
return [
218+
[Context::ENV_PROD, [ExpectEnvironment::prod()], true],
219+
[Context::ENV_PROD, [ExpectEnvironment::prod(), ExpectEnvironment::uat()], true],
220+
[Context::ENV_UAT, [ExpectEnvironment::prod(), ExpectEnvironment::uat()], true],
221+
[Context::ENV_LOCAL, [ExpectEnvironment::prod(), ExpectEnvironment::uat()], false],
222+
];
170223
}
171224
}

0 commit comments

Comments
 (0)