PageRenderTime 28ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 0ms

/vendor/laravel/framework/src/Illuminate/Foundation/Testing/CrawlerTrait.php

https://gitlab.com/4gdevs/online-class-record-system
PHP | 520 lines | 224 code | 87 blank | 209 comment | 15 complexity | 5a035e45a5ed39d5d023ba2b1436886f MD5 | raw file
  1. <?php
  2. namespace Illuminate\Foundation\Testing;
  3. use Illuminate\Support\Arr;
  4. use Illuminate\Support\Str;
  5. use Illuminate\Http\Request;
  6. trait CrawlerTrait
  7. {
  8. use InteractsWithPages;
  9. /**
  10. * The last response returned by the application.
  11. *
  12. * @var \Illuminate\Http\Response
  13. */
  14. protected $response;
  15. /**
  16. * The current URL being viewed.
  17. *
  18. * @var string
  19. */
  20. protected $currentUri;
  21. /**
  22. * Additional server variables for the request.
  23. *
  24. * @var array
  25. */
  26. protected $serverVariables = [];
  27. /**
  28. * Visit the given URI with a JSON request.
  29. *
  30. * @param string $method
  31. * @param string $uri
  32. * @param array $data
  33. * @param array $headers
  34. * @return $this
  35. */
  36. public function json($method, $uri, array $data = [], array $headers = [])
  37. {
  38. $content = json_encode($data);
  39. $headers = array_merge([
  40. 'CONTENT_LENGTH' => mb_strlen($content, '8bit'),
  41. 'CONTENT_TYPE' => 'application/json',
  42. 'Accept' => 'application/json',
  43. ], $headers);
  44. $this->call(
  45. $method, $uri, [], [], [], $this->transformHeadersToServerVars($headers), $content
  46. );
  47. return $this;
  48. }
  49. /**
  50. * Visit the given URI with a GET request.
  51. *
  52. * @param string $uri
  53. * @param array $headers
  54. * @return $this
  55. */
  56. public function get($uri, array $headers = [])
  57. {
  58. $server = $this->transformHeadersToServerVars($headers);
  59. $this->call('GET', $uri, [], [], [], $server);
  60. return $this;
  61. }
  62. /**
  63. * Visit the given URI with a POST request.
  64. *
  65. * @param string $uri
  66. * @param array $data
  67. * @param array $headers
  68. * @return $this
  69. */
  70. public function post($uri, array $data = [], array $headers = [])
  71. {
  72. $server = $this->transformHeadersToServerVars($headers);
  73. $this->call('POST', $uri, $data, [], [], $server);
  74. return $this;
  75. }
  76. /**
  77. * Visit the given URI with a PUT request.
  78. *
  79. * @param string $uri
  80. * @param array $data
  81. * @param array $headers
  82. * @return $this
  83. */
  84. public function put($uri, array $data = [], array $headers = [])
  85. {
  86. $server = $this->transformHeadersToServerVars($headers);
  87. $this->call('PUT', $uri, $data, [], [], $server);
  88. return $this;
  89. }
  90. /**
  91. * Visit the given URI with a PATCH request.
  92. *
  93. * @param string $uri
  94. * @param array $data
  95. * @param array $headers
  96. * @return $this
  97. */
  98. public function patch($uri, array $data = [], array $headers = [])
  99. {
  100. $server = $this->transformHeadersToServerVars($headers);
  101. $this->call('PATCH', $uri, $data, [], [], $server);
  102. return $this;
  103. }
  104. /**
  105. * Visit the given URI with a DELETE request.
  106. *
  107. * @param string $uri
  108. * @param array $data
  109. * @param array $headers
  110. * @return $this
  111. */
  112. public function delete($uri, array $data = [], array $headers = [])
  113. {
  114. $server = $this->transformHeadersToServerVars($headers);
  115. $this->call('DELETE', $uri, $data, [], [], $server);
  116. return $this;
  117. }
  118. /**
  119. * Send the given request through the application.
  120. *
  121. * This method allows you to fully customize the entire Request object.
  122. *
  123. * @param \Illuminate\Http\Request $request
  124. * @return $this
  125. */
  126. public function handle(Request $request)
  127. {
  128. $this->currentUri = $request->fullUrl();
  129. $this->response = $this->app->prepareResponse($this->app->handle($request));
  130. return $this;
  131. }
  132. /**
  133. * Assert that the response contains JSON.
  134. *
  135. * @param array|null $data
  136. * @return $this
  137. */
  138. protected function shouldReturnJson(array $data = null)
  139. {
  140. return $this->receiveJson($data);
  141. }
  142. /**
  143. * Assert that the response contains JSON.
  144. *
  145. * @param array|null $data
  146. * @return $this|null
  147. */
  148. protected function receiveJson($data = null)
  149. {
  150. $this->seeJson();
  151. if (! is_null($data)) {
  152. return $this->seeJson($data);
  153. }
  154. }
  155. /**
  156. * Assert that the response contains an exact JSON array.
  157. *
  158. * @param array $data
  159. * @return $this
  160. */
  161. public function seeJsonEquals(array $data)
  162. {
  163. $actual = json_encode(Arr::sortRecursive(
  164. json_decode($this->response->getContent(), true)
  165. ));
  166. $this->assertEquals(json_encode(Arr::sortRecursive($data)), $actual);
  167. return $this;
  168. }
  169. /**
  170. * Assert that the response contains JSON.
  171. *
  172. * @param array|null $data
  173. * @param bool $negate
  174. * @return $this
  175. */
  176. public function seeJson(array $data = null, $negate = false)
  177. {
  178. if (is_null($data)) {
  179. $this->assertJson(
  180. $this->response->getContent(), "JSON was not returned from [{$this->currentUri}]."
  181. );
  182. return $this;
  183. }
  184. return $this->seeJsonContains($data, $negate);
  185. }
  186. /**
  187. * Assert that the response doesn't contain JSON.
  188. *
  189. * @param array|null $data
  190. * @return $this
  191. */
  192. public function dontSeeJson(array $data = null)
  193. {
  194. return $this->seeJson($data, true);
  195. }
  196. /**
  197. * Assert that the response contains the given JSON.
  198. *
  199. * @param array $data
  200. * @param bool $negate
  201. * @return $this
  202. */
  203. protected function seeJsonContains(array $data, $negate = false)
  204. {
  205. $method = $negate ? 'assertFalse' : 'assertTrue';
  206. $actual = json_decode($this->response->getContent(), true);
  207. if (is_null($actual) || $actual === false) {
  208. return $this->fail('Invalid JSON was returned from the route. Perhaps an exception was thrown?');
  209. }
  210. $actual = json_encode(Arr::sortRecursive(
  211. (array) $actual
  212. ));
  213. foreach (Arr::sortRecursive($data) as $key => $value) {
  214. $expected = $this->formatToExpectedJson($key, $value);
  215. $this->{$method}(
  216. Str::contains($actual, $expected),
  217. ($negate ? 'Found unexpected' : 'Unable to find')." JSON fragment [{$expected}] within [{$actual}]."
  218. );
  219. }
  220. return $this;
  221. }
  222. /**
  223. * Format the given key and value into a JSON string for expectation checks.
  224. *
  225. * @param string $key
  226. * @param mixed $value
  227. * @return string
  228. */
  229. protected function formatToExpectedJson($key, $value)
  230. {
  231. $expected = json_encode([$key => $value]);
  232. if (Str::startsWith($expected, '{')) {
  233. $expected = substr($expected, 1);
  234. }
  235. if (Str::endsWith($expected, '}')) {
  236. $expected = substr($expected, 0, -1);
  237. }
  238. return $expected;
  239. }
  240. /**
  241. * Asserts that the status code of the response matches the given code.
  242. *
  243. * @param int $status
  244. * @return $this
  245. */
  246. protected function seeStatusCode($status)
  247. {
  248. $this->assertEquals($status, $this->response->getStatusCode());
  249. return $this;
  250. }
  251. /**
  252. * Asserts that the response contains the given header and equals the optional value.
  253. *
  254. * @param string $headerName
  255. * @param mixed $value
  256. * @return $this
  257. */
  258. protected function seeHeader($headerName, $value = null)
  259. {
  260. $headers = $this->response->headers;
  261. $this->assertTrue($headers->has($headerName), "Header [{$headerName}] not present on response.");
  262. if (! is_null($value)) {
  263. $this->assertEquals(
  264. $headers->get($headerName), $value,
  265. "Header [{$headerName}] was found, but value [{$headers->get($headerName)}] does not match [{$value}]."
  266. );
  267. }
  268. return $this;
  269. }
  270. /**
  271. * Asserts that the response contains the given cookie and equals the optional value.
  272. *
  273. * @param string $cookieName
  274. * @param mixed $value
  275. * @return $this
  276. */
  277. protected function seeCookie($cookieName, $value = null)
  278. {
  279. $headers = $this->response->headers;
  280. $exist = false;
  281. foreach ($headers->getCookies() as $cookie) {
  282. if ($cookie->getName() === $cookieName) {
  283. $exist = true;
  284. break;
  285. }
  286. }
  287. $this->assertTrue($exist, "Cookie [{$cookieName}] not present on response.");
  288. if (! is_null($value)) {
  289. $this->assertEquals(
  290. $cookie->getValue(), $value,
  291. "Cookie [{$cookieName}] was found, but value [{$cookie->getValue()}] does not match [{$value}]."
  292. );
  293. }
  294. return $this;
  295. }
  296. /**
  297. * Define a set of server variables to be sent with the requests.
  298. *
  299. * @param array $server
  300. * @return $this
  301. */
  302. protected function withServerVariables(array $server)
  303. {
  304. $this->serverVariables = $server;
  305. return $this;
  306. }
  307. /**
  308. * Call the given URI and return the Response.
  309. *
  310. * @param string $method
  311. * @param string $uri
  312. * @param array $parameters
  313. * @param array $cookies
  314. * @param array $files
  315. * @param array $server
  316. * @param string $content
  317. * @return \Illuminate\Http\Response
  318. */
  319. public function call($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
  320. {
  321. $kernel = $this->app->make('Illuminate\Contracts\Http\Kernel');
  322. $this->currentUri = $this->prepareUrlForRequest($uri);
  323. $request = Request::create(
  324. $this->currentUri, $method, $parameters,
  325. $cookies, $files, array_replace($this->serverVariables, $server), $content
  326. );
  327. $response = $kernel->handle($request);
  328. $kernel->terminate($request, $response);
  329. return $this->response = $response;
  330. }
  331. /**
  332. * Call the given HTTPS URI and return the Response.
  333. *
  334. * @param string $method
  335. * @param string $uri
  336. * @param array $parameters
  337. * @param array $cookies
  338. * @param array $files
  339. * @param array $server
  340. * @param string $content
  341. * @return \Illuminate\Http\Response
  342. */
  343. public function callSecure($method, $uri, $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
  344. {
  345. $uri = $this->app['url']->secure(ltrim($uri, '/'));
  346. return $this->response = $this->call($method, $uri, $parameters, $cookies, $files, $server, $content);
  347. }
  348. /**
  349. * Call a controller action and return the Response.
  350. *
  351. * @param string $method
  352. * @param string $action
  353. * @param array $wildcards
  354. * @param array $parameters
  355. * @param array $cookies
  356. * @param array $files
  357. * @param array $server
  358. * @param string $content
  359. * @return \Illuminate\Http\Response
  360. */
  361. public function action($method, $action, $wildcards = [], $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
  362. {
  363. $uri = $this->app['url']->action($action, $wildcards, true);
  364. return $this->response = $this->call($method, $uri, $parameters, $cookies, $files, $server, $content);
  365. }
  366. /**
  367. * Call a named route and return the Response.
  368. *
  369. * @param string $method
  370. * @param string $name
  371. * @param array $routeParameters
  372. * @param array $parameters
  373. * @param array $cookies
  374. * @param array $files
  375. * @param array $server
  376. * @param string $content
  377. * @return \Illuminate\Http\Response
  378. */
  379. public function route($method, $name, $routeParameters = [], $parameters = [], $cookies = [], $files = [], $server = [], $content = null)
  380. {
  381. $uri = $this->app['url']->route($name, $routeParameters);
  382. return $this->response = $this->call($method, $uri, $parameters, $cookies, $files, $server, $content);
  383. }
  384. /**
  385. * Turn the given URI into a fully qualified URL.
  386. *
  387. * @param string $uri
  388. * @return string
  389. */
  390. protected function prepareUrlForRequest($uri)
  391. {
  392. if (Str::startsWith($uri, '/')) {
  393. $uri = substr($uri, 1);
  394. }
  395. if (! Str::startsWith($uri, 'http')) {
  396. $uri = $this->baseUrl.'/'.$uri;
  397. }
  398. return trim($uri, '/');
  399. }
  400. /**
  401. * Transform headers array to array of $_SERVER vars with HTTP_* format.
  402. *
  403. * @param array $headers
  404. * @return array
  405. */
  406. protected function transformHeadersToServerVars(array $headers)
  407. {
  408. $server = [];
  409. $prefix = 'HTTP_';
  410. foreach ($headers as $name => $value) {
  411. $name = strtr(strtoupper($name), '-', '_');
  412. if (! starts_with($name, $prefix) && $name != 'CONTENT_TYPE') {
  413. $name = $prefix.$name;
  414. }
  415. $server[$name] = $value;
  416. }
  417. return $server;
  418. }
  419. /**
  420. * Dump the content from the last response.
  421. *
  422. * @return void
  423. */
  424. public function dump()
  425. {
  426. $content = $this->response->getContent();
  427. $json = json_decode($content);
  428. if (json_last_error() === JSON_ERROR_NONE) {
  429. $content = $json;
  430. }
  431. dd($content);
  432. }
  433. }