1: <?php
2: 3: 4: 5: 6: 7: 8: 9: 10:
11:
12: namespace Crunchmail\PHPUnit;
13:
14: use Crunchmail\Client;
15:
16: use GuzzleHttp\Handler\MockHandler;
17: use GuzzleHttp\HandlerStack;
18: use GuzzleHttp\Psr7\Response;
19: use GuzzleHttp\Psr7\Request;
20: use GuzzleHttp\Middleware;
21:
22: 23: 24:
25: abstract class TestCase extends \PHPUnit_Framework_TestCase
26: {
27: private $container = [];
28: private $bodyHistory = [];
29:
30: 31: 32: 33: 34:
35: public function quickMock()
36: {
37: $handler = call_user_func_array([$this, 'mockHandler'], func_get_args());
38: return $this->mockClient($handler);
39: }
40:
41: 42: 43: 44: 45:
46: public function mockClient($handler)
47: {
48: $client = new Client([
49: 'base_uri' => '',
50: 'token_uri' => '',
51: 'auth' => ['api', 'key'],
52: 'handler' => $handler
53: ]);
54: return $client;
55: }
56:
57: 58: 59: 60: 61: 62: 63:
64: public function mockHandler()
65: {
66: $responses = [];
67:
68:
69: $this->bodyHistory = [];
70:
71: foreach (func_get_args() as $r)
72: {
73: list($tpl, $code) = $r;
74:
75: $body = $this->getTemplate($tpl);
76: $this->bodyHistory[] = $body;
77:
78: $responses[] = new MockHandler([ new Response($code, [], $body) ]);
79: }
80:
81:
82: $handler = new MockHandler($responses);
83: $stack = HandlerStack::create($handler);
84:
85:
86: $this->container = [];
87: $history = Middleware::history($this->container);
88:
89:
90: $stack->push($history);
91:
92: return $stack;
93: }
94:
95: public function getTemplate($tpl)
96: {
97: $dir = __DIR__ . '/../../tests/responses/';
98: $path = $dir . $tpl;
99:
100:
101: if (! preg_match('#\.[a-z]+$#', $tpl))
102: {
103: $path .= '.json';
104: }
105: return file_get_contents($path);
106: }
107:
108: public function getStdTemplate($tpl)
109: {
110: return json_decode($this->getTemplate($tpl));
111: }
112:
113: 114: 115: 116: 117: 118: 119:
120: public function getSentBody($ind)
121: {
122: return json_decode($this->bodyHistory[$ind]);
123: }
124:
125: public function getHistory()
126: {
127: return $this->container;
128: }
129:
130: public function methodsProvider()
131: {
132: $data = [];
133: foreach (Client::$methods as $m)
134: {
135: $data[] = [$m];
136: }
137: return $data;
138: }
139:
140: public function getHistoryRequest($ind)
141: {
142: $history = $this->getHistory();
143: return $history[$ind]['request'];
144: }
145:
146: 147: 148:
149: public function getHistoryContent($ind, $decode = true)
150: {
151: $req = $this->getHistoryRequest($ind);
152: $content = $req->getBody()->getContents();
153: return $decode ? json_decode($content) : $content;
154: }
155:
156: 157: 158:
159:
160:
161:
162: public static function assertGenericEntity($other)
163: {
164: self::assertThat($other, self::isGenericEntity());
165: }
166:
167: public static function assertGenericResource($other)
168: {
169: self::assertThat($other, self::isGenericResource());
170: }
171:
172: public static function assertGenericCollection($other)
173: {
174: self::assertThat($other, self::isGenericCollection());
175: }
176:
177: public static function assertEntity($type, $other)
178: {
179: self::assertThat($other, self::isEntity($type));
180: }
181:
182: public static function assertResource($type, $other)
183: {
184: self::assertThat($other, self::isResource($type));
185: }
186:
187: 188: 189:
190: public static function isGenericResource()
191: {
192: return new \Crunchmail\PHPUnit\IsGenericResourceConstraint();
193: }
194:
195: public static function isGenericCollection()
196: {
197: return new \Crunchmail\PHPUnit\IsGenericCollectionConstraint();
198: }
199:
200: public static function isGenericEntity()
201: {
202: return new \Crunchmail\PHPUnit\IsGenericEntityConstraint();
203: }
204:
205: public static function isEntity($type)
206: {
207: return new \Crunchmail\PHPUnit\IsEntityConstraint($type);
208: }
209:
210: public static function isResource($type)
211: {
212: return new \Crunchmail\PHPUnit\IsResourceConstraint($type);
213: }
214:
215:
216:
217: }
218: