-
Notifications
You must be signed in to change notification settings - Fork 0
/
ChainTest.php
124 lines (98 loc) · 3.8 KB
/
ChainTest.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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
<?php
namespace MockChainTest;
use MockChain\Chain;
use MockChainTest\Anatomy\Body;
use MockChainTest\Anatomy\System;
use MockChainTest\Anatomy\Organ;
use MockChain\Options;
use MockChain\Sequence;
use PHPUnit\Framework\TestCase;
class ChainTest extends TestCase
{
public function test()
{
$organNames = (new Sequence())->add('mouth')->add('stomach');
$organ = (new Chain($this))
->add(Organ::class, "getName", $organNames)
->getMock();
$system = (new Chain($this))
->add(System::class, 'getName', "digestive")
->addd('getOrgans', [$organ, $organ])
->addd('getOrgan', new Organ('mouth'))
->getMock();
$body = new Body();
$body->addSystem($system);
$this->assertEquals(json_encode($body->getOrgans()), json_encode(['mouth', 'stomach']));
$this->assertEquals(json_encode($body->getSystems()), json_encode(['digestive']));
$this->assertEquals($body->getSystem('digestive')->getOrgan('mouth')->getName(), 'mouth');
}
public function test2()
{
$organNames = (new Sequence())->add('mouth')->add('stomach');
$organ = (new Chain($this))
->add(Organ::class, "getName", $organNames)
->getMock();
$system = (new Chain($this))
->add(System::class, 'getName', "digestive")
->add(System::class, 'getOrgans', [$organ, $organ])
->add(System::class, 'getOrgan', Organ::class)
->add(Organ::class, 'getName', 'mouth')
->getMock();
$body = new Body();
$body->addSystem($system);
$this->assertEquals(json_encode($body->getOrgans()), json_encode(['mouth', 'stomach']));
$this->assertEquals(json_encode($body->getSystems()), json_encode(['digestive']));
$this->assertEquals('mouth', $body->getSystem('digestive')->getOrgan('mouth')->getName());
}
public function test3()
{
$organs = (new Options())
->add('mouth', Organ::class)
->add('stomach', Organ::class);
$organNames = (new Options())
->add('mouth', 'mouth')
->add('stomach', 'stomach')
->use('organ');
$chain = (new Chain($this))
->add(System::class, 'getName', "digestive")
->add(System::class, 'getOrgan', $organs, 'organ')
->add(Organ::class, "getName", $organNames);
$system = $chain->getMock();
$body = new Body();
$body->addSystem($system);
$this->assertEquals($body->getSystem('digestive')->getOrgan('mouth')->getName(), 'mouth');
$this->assertEquals($body->getSystem('digestive')->getOrgan('stomach')->getName(), 'stomach');
$this->assertEquals(json_encode(['stomach']), json_encode($chain->getStoredInput('organ')));
}
public function test4()
{
$this->expectExceptionMessage("blah");
$system = (new Chain($this))
->add(System::class, 'getName', new \Exception("blah"))
->getMock();
$body = new Body();
$body->addSystem($system);
$body->getSystem("blah");
}
public function testNonExistentMethod()
{
$this->expectExceptionMessage('Trying to set mock method "blah" with onlyMethods');
(new Chain($this))
->add(Organ::class, 'blah', null)
->getMock();
}
public function testUsingAdddIncorrectly()
{
$this->expectExceptionMessage("You should use the add method before using addd.");
(new Chain($this))->addd("hello");
}
public function testNonExistentOption()
{
$this->expectExceptionMessage('Option digestive does not exist');
$options = new Options();
$mock = (new Chain($this))
->add(Body::class, 'getSystem', $options)
->getMock();
$mock->getSystem("digestive");
}
}