|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
/* |
|
4
|
|
|
* This file is part of the Sylius package. |
|
5
|
|
|
* |
|
6
|
|
|
* (c) Paweł Jędrzejewski |
|
7
|
|
|
* |
|
8
|
|
|
* For the full copyright and license information, please view the LICENSE |
|
9
|
|
|
* file that was distributed with this source code. |
|
10
|
|
|
*/ |
|
11
|
|
|
|
|
12
|
|
|
namespace Sylius\Behat; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @author Arkadiusz Krakowiak <[email protected]> |
|
16
|
|
|
*/ |
|
17
|
|
|
class ResponseLoader implements ResponseLoaderInterface |
|
18
|
|
|
{ |
|
19
|
|
|
/** |
|
20
|
|
|
* {@inheritdoc} |
|
21
|
|
|
*/ |
|
22
|
|
|
public function getMockedResponse($source) |
|
23
|
|
|
{ |
|
24
|
|
|
$source = $this->getMockedResponsesFolder() . '/' . $source; |
|
25
|
|
|
|
|
26
|
|
|
return (array) json_decode($this->getFileContents($source)); |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
/** |
|
30
|
|
|
* {@inheritdoc} |
|
31
|
|
|
*/ |
|
32
|
|
|
public function getExpectedResponse($source) |
|
33
|
|
|
{ |
|
34
|
|
|
$source = $this->getExpectedResponsesFolder() . '/' . $source; |
|
35
|
|
|
|
|
36
|
|
|
return (array) json_decode($this->getFileContents($source)); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @return string |
|
41
|
|
|
*/ |
|
42
|
|
|
private function getResponsesFolder() |
|
43
|
|
|
{ |
|
44
|
|
|
return $this->getCalledClassFolder() . '/Responses'; |
|
45
|
|
|
} |
|
46
|
|
|
|
|
47
|
|
|
/** |
|
48
|
|
|
* @return string |
|
49
|
|
|
*/ |
|
50
|
|
|
private function getMockedResponsesFolder() |
|
51
|
|
|
{ |
|
52
|
|
|
return $this->getResponsesFolder() . '/Mocked'; |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
private function getExpectedResponsesFolder() |
|
59
|
|
|
{ |
|
60
|
|
|
return $this->getResponsesFolder() . '/Expected'; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
/** |
|
64
|
|
|
* @return string |
|
65
|
|
|
*/ |
|
66
|
|
|
private function getCalledClassFolder() |
|
67
|
|
|
{ |
|
68
|
|
|
$calledClass = get_called_class(); |
|
69
|
|
|
$calledClassFolder = dirname((new \ReflectionClass($calledClass))->getFileName()); |
|
70
|
|
|
|
|
71
|
|
|
return $calledClassFolder; |
|
72
|
|
|
} |
|
73
|
|
|
|
|
74
|
|
|
/** |
|
75
|
|
|
* @param string $source |
|
76
|
|
|
* |
|
77
|
|
|
* @throws \RuntimeException |
|
78
|
|
|
*/ |
|
79
|
|
|
private function assertSourceExists($source) |
|
80
|
|
|
{ |
|
81
|
|
|
if (!file_exists($source)) { |
|
82
|
|
|
throw new \RuntimeException(sprintf('File %s does not exist', $source)); |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
/** |
|
87
|
|
|
* @param string $source |
|
88
|
|
|
* @param mixed $content |
|
89
|
|
|
* |
|
90
|
|
|
* @throws \RuntimeException |
|
91
|
|
|
*/ |
|
92
|
|
|
private function assertContentIsNotEmpty($source, $content) |
|
93
|
|
|
{ |
|
94
|
|
|
if ('' === $content) { |
|
95
|
|
|
throw new \RuntimeException(sprintf('Something went wrong, file %s is empty', $source)); |
|
96
|
|
|
} |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @param string $source |
|
101
|
|
|
* @param mixed $content |
|
102
|
|
|
* |
|
103
|
|
|
* @throws \RuntimeException |
|
104
|
|
|
*/ |
|
105
|
|
|
private function assertContentIsProperLoaded($source, $content) |
|
106
|
|
|
{ |
|
107
|
|
|
if (false === $content) { |
|
108
|
|
|
throw new \RuntimeException(sprintf('Something went wrong, cannot open %s', $source)); |
|
109
|
|
|
} |
|
110
|
|
|
} |
|
111
|
|
|
|
|
112
|
|
|
/** |
|
113
|
|
|
* @param string $source |
|
114
|
|
|
* |
|
115
|
|
|
* @throws \RuntimeException |
|
116
|
|
|
*/ |
|
117
|
|
|
private function assertSourceIsNotFolder($source) |
|
118
|
|
|
{ |
|
119
|
|
|
if (true === is_dir($source)) { |
|
120
|
|
|
throw new \RuntimeException(sprintf('Given source %s is a folder!', $source)); |
|
121
|
|
|
} |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
/** |
|
125
|
|
|
* @param string $source |
|
126
|
|
|
* |
|
127
|
|
|
* @return string |
|
128
|
|
|
* |
|
129
|
|
|
* @throws \RuntimeException |
|
130
|
|
|
*/ |
|
131
|
|
|
private function getFileContents($source) |
|
132
|
|
|
{ |
|
133
|
|
|
$this->assertSourceExists($source); |
|
134
|
|
|
$this->assertSourceIsNotFolder($source); |
|
135
|
|
|
$content = file_get_contents($source, true); |
|
136
|
|
|
|
|
137
|
|
|
$this->assertContentIsProperLoaded($source, $content); |
|
138
|
|
|
$this->assertContentIsNotEmpty($source, $content); |
|
139
|
|
|
|
|
140
|
|
|
return $content; |
|
141
|
|
|
} |
|
142
|
|
|
} |
|
143
|
|
|
|