-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
ChainToggleStrategyFactoryTest.php
97 lines (85 loc) · 4 KB
/
ChainToggleStrategyFactoryTest.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?php
declare(strict_types=1);
namespace Pheature\Test\Core\Toggle\Read;
use Pheature\Core\Toggle\Exception\InvalidStrategyTypeGiven;
use Pheature\Core\Toggle\Read\ChainToggleStrategyFactory;
use Pheature\Core\Toggle\Read\Segment;
use Pheature\Core\Toggle\Read\SegmentFactory;
use Pheature\Core\Toggle\Read\ToggleStrategy;
use Pheature\Core\Toggle\Read\ToggleStrategyFactory;
use PHPUnit\Framework\TestCase;
final class ChainToggleStrategyFactoryTest extends TestCase
{
private const STRATEGY_ID = 'some_strategy_id';
private const STRATEGY_TYPE = 'some_type';
private const SEGMENTS = [
[
'segment_id' => 'some_segment_id',
'segment_type' => 'some_segment_type',
'criteria' => [
'some' => 'data'
],
],
];
public function testItShouldThrowAnExceptionWhenItCantCreateAToggleStrategyType(): void
{
$this->expectException(InvalidStrategyTypeGiven::class);
$segmentFactory = $this->createMock(SegmentFactory::class);
$toggleStrategyFactory = $this->createMock(ToggleStrategyFactory::class);
$toggleStrategyFactory->expects(self::once())
->method('types')
->willReturn([]);
$chainToggleStrategyFactory = new ChainToggleStrategyFactory($segmentFactory, $toggleStrategyFactory);
$chainToggleStrategyFactory->createFromArray([
'strategy_id' => self::STRATEGY_ID,
'strategy_type' => self::STRATEGY_TYPE,
'segments' => []
]);
}
public function testItShouldBeCreatedWithAtLeastOneToggleStrategyFactoryInstance(): void
{
$segmentFactory = $this->createMock(SegmentFactory::class);
$toggleStrategyFactory = $this->createMock(ToggleStrategyFactory::class);
$toggleStrategyFactory->expects(self::atLeastOnce())
->method('types')
->willReturn([self::STRATEGY_TYPE]);
$expectedStrategy = $this->createMock(ToggleStrategy::class);
$toggleStrategyFactory->expects(self::once())
->method('create')
->with(self::STRATEGY_ID, self::STRATEGY_TYPE)
->willReturn($expectedStrategy);
$chainToggleStrategyFactory = new ChainToggleStrategyFactory($segmentFactory, $toggleStrategyFactory);
$current = $chainToggleStrategyFactory->createFromArray([
'strategy_id' => self::STRATEGY_ID,
'strategy_type' => self::STRATEGY_TYPE,
'segments' => []
]);
$this->assertSame($expectedStrategy, $current);
$this->assertSame([self::STRATEGY_TYPE], $chainToggleStrategyFactory->types());
}
public function testItShouldBeCreatedWithAtLeastOneToggleStrategyFactoryInstanceAndSegments(): void
{
$segmentFactory = $this->createMock(SegmentFactory::class);
$segmentFactory->expects(self::once())
->method('create')
->with(self::SEGMENTS[0]['segment_id'], self::SEGMENTS[0]['segment_type'],self::SEGMENTS[0]['criteria'])
->willReturn($this->createMock(Segment::class));
$toggleStrategyFactory = $this->createMock(ToggleStrategyFactory::class);
$toggleStrategyFactory->expects(self::atLeastOnce())
->method('types')
->willReturn([self::STRATEGY_TYPE, 'other_type']);
$expectedStrategy = $this->createMock(ToggleStrategy::class);
$toggleStrategyFactory->expects(self::once())
->method('create')
->with(self::STRATEGY_ID, self::STRATEGY_TYPE)
->willReturn($expectedStrategy);
$chainToggleStrategyFactory = new ChainToggleStrategyFactory($segmentFactory, $toggleStrategyFactory);
$current = $chainToggleStrategyFactory->createFromArray([
'strategy_id' => self::STRATEGY_ID,
'strategy_type' => self::STRATEGY_TYPE,
'segments' => self::SEGMENTS
]);
$this->assertSame($expectedStrategy, $current);
$this->assertSame([self::STRATEGY_TYPE, 'other_type'], $chainToggleStrategyFactory->types());
}
}