|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Behatch\Tests\Units\Context; |
| 4 | + |
| 5 | +use atoum; |
| 6 | +use Behat\Gherkin\Node\TableNode; |
| 7 | +use Behatch\HttpCall\HttpCallResult; |
| 8 | +use Behatch\HttpCall\HttpCallResultPool; |
| 9 | + |
| 10 | +class JsonContext extends atoum |
| 11 | +{ |
| 12 | + /** |
| 13 | + * @var HttpCallResultPool |
| 14 | + */ |
| 15 | + private $httpCallResultPool; |
| 16 | + |
| 17 | + public function beforeTestMethod($methodName) |
| 18 | + { |
| 19 | + $this->mockGenerator->orphanize('__construct'); |
| 20 | + $httpCallResult = $this->newMockInstance(HttpCallResult::class); |
| 21 | + $httpCallResult->getMockController()->getValue = json_encode([ |
| 22 | + 'a string node' => 'some string', |
| 23 | + 'another string node' => 'some other string', |
| 24 | + 'a null node' => null, |
| 25 | + 'a true node' => true, |
| 26 | + 'a false node' => false, |
| 27 | + 'a number node' => 3, |
| 28 | + 'an array node' => [ |
| 29 | + 'one', |
| 30 | + 'two', |
| 31 | + 'three', |
| 32 | + ], |
| 33 | + ]); |
| 34 | + |
| 35 | + $this->httpCallResultPool = $this->newMockInstance(HttpCallResultPool::class); |
| 36 | + $this->httpCallResultPool->getMockController()->getResult = $httpCallResult; |
| 37 | + } |
| 38 | + |
| 39 | + public function testTheJsonNodeShouldBeEqualTo() |
| 40 | + { |
| 41 | + $this |
| 42 | + ->given($this->newTestedInstance($this->httpCallResultPool)) |
| 43 | + ->then |
| 44 | + |
| 45 | + ->if($this->testedInstance->theJsonNodeShouldBeEqualTo('a string node', 'some string')) |
| 46 | + |
| 47 | + ->exception(function () { |
| 48 | + $this->testedInstance->theJsonNodeShouldBeEqualTo('a string node', 'expectedstring'); |
| 49 | + }) |
| 50 | + ->hasMessage("The node 'a string node' value is '\"some string\"', 'expectedstring' expected") |
| 51 | + ; |
| 52 | + } |
| 53 | + |
| 54 | + public function testTheJsonNodesShouldBeEqualTo() |
| 55 | + { |
| 56 | + $this |
| 57 | + ->given($this->newTestedInstance($this->httpCallResultPool)) |
| 58 | + ->and($validTableNode = new TableNode([ |
| 59 | + 1 => ['a string node', 'some string'], |
| 60 | + 2 => ['another string node', 'some other string'], |
| 61 | + ])) |
| 62 | + ->then |
| 63 | + |
| 64 | + ->if($this->testedInstance->theJsonNodesShouldBeEqualTo($validTableNode)) |
| 65 | + |
| 66 | + ->exception(function () { |
| 67 | + $invalidTableNode = new TableNode([ |
| 68 | + 1 => ['a string node', 'may the force'], |
| 69 | + 2 => ['another string node', 'be with you'], |
| 70 | + ]); |
| 71 | + $this->testedInstance->theJsonNodesShouldBeEqualTo($invalidTableNode); |
| 72 | + }) |
| 73 | + ->hasMessage("The node 'a string node' value is '\"some string\"', 'may the force' expected\nThe node 'another string node' value is '\"some other string\"', 'be with you' expected") |
| 74 | + ; |
| 75 | + } |
| 76 | + public function testTheJsonNodeShouldMatch() |
| 77 | + { |
| 78 | + $this |
| 79 | + ->given($this->newTestedInstance($this->httpCallResultPool)) |
| 80 | + ->then |
| 81 | + |
| 82 | + ->if($this->testedInstance->theJsonNodeShouldMatch('a string node', '/some/')) |
| 83 | + |
| 84 | + ->exception(function () { |
| 85 | + $this->testedInstance->theJsonNodeShouldMatch('a string node', '/nomatch/'); |
| 86 | + }) |
| 87 | + ->hasMessage("The node 'a string node' value is '\"some string\"', '/nomatch/' pattern expected") |
| 88 | + ; |
| 89 | + } |
| 90 | + |
| 91 | + public function testTheJsonNodeShouldBeNull() |
| 92 | + { |
| 93 | + $this |
| 94 | + ->given($this->newTestedInstance($this->httpCallResultPool)) |
| 95 | + ->then |
| 96 | + |
| 97 | + ->if($this->testedInstance->theJsonNodeShouldBeNull('a null node')) |
| 98 | + |
| 99 | + ->exception(function () { |
| 100 | + $this->testedInstance->theJsonNodeShouldBeNull('a string node'); |
| 101 | + }) |
| 102 | + ->hasMessage("The node 'a string node' value is '\"some string\"', null expected") |
| 103 | + ; |
| 104 | + } |
| 105 | + |
| 106 | + public function testTheJsonNodeShouldNotBeNull() |
| 107 | + { |
| 108 | + $this |
| 109 | + ->given($this->newTestedInstance($this->httpCallResultPool)) |
| 110 | + ->then |
| 111 | + |
| 112 | + ->if($this->testedInstance->theJsonNodeShouldNotBeNull('a string node')) |
| 113 | + |
| 114 | + ->exception(function () { |
| 115 | + $this->testedInstance->theJsonNodeShouldNotBeNull('a null node'); |
| 116 | + }) |
| 117 | + ->hasMessage("The node 'a null node' value is null, non-null value expected") |
| 118 | + ; |
| 119 | + } |
| 120 | + |
| 121 | + public function testTheJsonNodeShouldBeTrue() |
| 122 | + { |
| 123 | + $this |
| 124 | + ->given($this->newTestedInstance($this->httpCallResultPool)) |
| 125 | + ->then |
| 126 | + |
| 127 | + ->if($this->testedInstance->theJsonNodeShouldBeTrue('a true node')) |
| 128 | + |
| 129 | + ->exception(function () { |
| 130 | + $this->testedInstance->theJsonNodeShouldBeTrue('a false node'); |
| 131 | + }) |
| 132 | + ->hasMessage("The node 'a false node' value is 'false', 'true' expected") |
| 133 | + ; |
| 134 | + } |
| 135 | + |
| 136 | + public function testTheJsonNodeShouldBeFalse() |
| 137 | + { |
| 138 | + $this |
| 139 | + ->given($this->newTestedInstance($this->httpCallResultPool)) |
| 140 | + ->then |
| 141 | + |
| 142 | + ->if($this->testedInstance->theJsonNodeShouldBeFalse('a false node')) |
| 143 | + |
| 144 | + ->exception(function () { |
| 145 | + $this->testedInstance->theJsonNodeShouldBeFalse('a true node'); |
| 146 | + }) |
| 147 | + ->hasMessage("The node 'a true node' value is 'true', 'false' expected") |
| 148 | + ; |
| 149 | + } |
| 150 | + |
| 151 | + public function testTheJsonNodeShouldBeEqualToTheString() |
| 152 | + { |
| 153 | + $this |
| 154 | + ->given($this->newTestedInstance($this->httpCallResultPool)) |
| 155 | + ->then |
| 156 | + |
| 157 | + ->if($this->testedInstance->theJsonNodeShouldBeEqualToTheString('a string node', 'some string')) |
| 158 | + |
| 159 | + ->exception(function () { |
| 160 | + $this->testedInstance->theJsonNodeShouldBeEqualToTheString('a string node', 'expected'); |
| 161 | + }) |
| 162 | + ->hasMessage("The node 'a string node' value is '\"some string\"', string 'expected' expected") |
| 163 | + ; |
| 164 | + } |
| 165 | + |
| 166 | + public function testTheJsonNodeShouldBeEqualToTheNumber() |
| 167 | + { |
| 168 | + $this |
| 169 | + ->given($this->newTestedInstance($this->httpCallResultPool)) |
| 170 | + ->then |
| 171 | + |
| 172 | + ->if($this->testedInstance->theJsonNodeShouldBeEqualToTheNumber('a number node', 3)) |
| 173 | + |
| 174 | + ->exception(function () { |
| 175 | + $this->testedInstance->theJsonNodeShouldBeEqualToTheNumber('a number node', 2); |
| 176 | + }) |
| 177 | + ->hasMessage("The node 'a number node' value is '3', number '2' expected") |
| 178 | + ; |
| 179 | + } |
| 180 | + |
| 181 | + public function testTheJsonNodeShouldExist() |
| 182 | + { |
| 183 | + $this |
| 184 | + ->given($this->newTestedInstance($this->httpCallResultPool)) |
| 185 | + ->then |
| 186 | + |
| 187 | + ->if($this->testedInstance->theJsonNodeShouldExist('a string node')) |
| 188 | + |
| 189 | + ->exception(function () { |
| 190 | + $this->testedInstance->theJsonNodeShouldExist('invalid key'); |
| 191 | + }) |
| 192 | + ->hasMessage("The node 'invalid key' does not exist.") |
| 193 | + ; |
| 194 | + } |
| 195 | +} |
0 commit comments