TestResponseTest.php 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688
  1. <?php
  2. namespace Illuminate\Tests\Testing;
  3. use Exception;
  4. use Illuminate\Container\Container;
  5. use Illuminate\Contracts\View\View;
  6. use Illuminate\Cookie\CookieValuePrefix;
  7. use Illuminate\Database\Eloquent\Model;
  8. use Illuminate\Encryption\Encrypter;
  9. use Illuminate\Filesystem\Filesystem;
  10. use Illuminate\Http\RedirectResponse;
  11. use Illuminate\Http\Response;
  12. use Illuminate\Session\ArraySessionHandler;
  13. use Illuminate\Session\Store;
  14. use Illuminate\Support\MessageBag;
  15. use Illuminate\Support\ViewErrorBag;
  16. use Illuminate\Testing\Fluent\AssertableJson;
  17. use Illuminate\Testing\TestResponse;
  18. use JsonSerializable;
  19. use Mockery as m;
  20. use PHPUnit\Framework\AssertionFailedError;
  21. use PHPUnit\Framework\ExpectationFailedException;
  22. use PHPUnit\Framework\TestCase;
  23. use Symfony\Component\HttpFoundation\BinaryFileResponse;
  24. use Symfony\Component\HttpFoundation\Cookie;
  25. class TestResponseTest extends TestCase
  26. {
  27. public function testAssertViewIs()
  28. {
  29. $response = $this->makeMockResponse([
  30. 'render' => 'hello world',
  31. 'getData' => ['foo' => 'bar'],
  32. 'name' => 'dir.my-view',
  33. ]);
  34. $response->assertViewIs('dir.my-view');
  35. }
  36. public function testAssertViewHas()
  37. {
  38. $response = $this->makeMockResponse([
  39. 'render' => 'hello world',
  40. 'gatherData' => ['foo' => 'bar'],
  41. ]);
  42. $response->assertViewHas('foo');
  43. }
  44. public function testAssertViewHasModel()
  45. {
  46. $model = new class extends Model
  47. {
  48. public function is($model)
  49. {
  50. return $this == $model;
  51. }
  52. };
  53. $response = $this->makeMockResponse([
  54. 'render' => 'hello world',
  55. 'gatherData' => ['foo' => $model],
  56. ]);
  57. $response->original->foo = $model;
  58. $response->assertViewHas('foo', $model);
  59. }
  60. public function testAssertViewHasWithClosure()
  61. {
  62. $response = $this->makeMockResponse([
  63. 'render' => 'hello world',
  64. 'gatherData' => ['foo' => 'bar'],
  65. ]);
  66. $response->assertViewHas('foo', function ($value) {
  67. return $value === 'bar';
  68. });
  69. }
  70. public function testAssertViewHasWithValue()
  71. {
  72. $response = $this->makeMockResponse([
  73. 'render' => 'hello world',
  74. 'gatherData' => ['foo' => 'bar'],
  75. ]);
  76. $response->assertViewHas('foo', 'bar');
  77. }
  78. public function testAssertViewHasNested()
  79. {
  80. $response = $this->makeMockResponse([
  81. 'render' => 'hello world',
  82. 'gatherData' => [
  83. 'foo' => [
  84. 'nested' => 'bar',
  85. ],
  86. ],
  87. ]);
  88. $response->assertViewHas('foo.nested');
  89. }
  90. public function testAssertViewHasWithNestedValue()
  91. {
  92. $response = $this->makeMockResponse([
  93. 'render' => 'hello world',
  94. 'gatherData' => [
  95. 'foo' => [
  96. 'nested' => 'bar',
  97. ],
  98. ],
  99. ]);
  100. $response->assertViewHas('foo.nested', 'bar');
  101. }
  102. public function testAssertViewMissing()
  103. {
  104. $response = $this->makeMockResponse([
  105. 'render' => 'hello world',
  106. 'gatherData' => ['foo' => 'bar'],
  107. ]);
  108. $response->assertViewMissing('baz');
  109. }
  110. public function testAssertViewMissingNested()
  111. {
  112. $response = $this->makeMockResponse([
  113. 'render' => 'hello world',
  114. 'gatherData' => [
  115. 'foo' => [
  116. 'nested' => 'bar',
  117. ],
  118. ],
  119. ]);
  120. $response->assertViewMissing('foo.baz');
  121. }
  122. public function testAssertSee()
  123. {
  124. $response = $this->makeMockResponse([
  125. 'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>',
  126. ]);
  127. $response->assertSee('foo');
  128. $response->assertSee(['baz', 'bar']);
  129. }
  130. public function testAssertSeeCanFail()
  131. {
  132. $this->expectException(AssertionFailedError::class);
  133. $response = $this->makeMockResponse([
  134. 'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>',
  135. ]);
  136. $response->assertSee('item');
  137. $response->assertSee(['not', 'found']);
  138. }
  139. public function testAssertSeeEscaped()
  140. {
  141. $response = $this->makeMockResponse([
  142. 'render' => 'laravel &amp; php &amp; friends',
  143. ]);
  144. $response->assertSee('laravel & php');
  145. $response->assertSee(['php & friends', 'laravel & php']);
  146. }
  147. public function testAssertSeeEscapedCanFail()
  148. {
  149. $this->expectException(AssertionFailedError::class);
  150. $response = $this->makeMockResponse([
  151. 'render' => 'laravel &amp; php &amp; friends',
  152. ]);
  153. $response->assertSee('foo & bar');
  154. $response->assertSee(['bar & baz', 'baz & qux']);
  155. }
  156. public function testAssertSeeInOrder()
  157. {
  158. $response = $this->makeMockResponse([
  159. 'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>',
  160. ]);
  161. $response->assertSeeInOrder(['foo', 'bar', 'baz']);
  162. $response->assertSeeInOrder(['foo', 'bar', 'baz', 'foo']);
  163. }
  164. public function testAssertSeeInOrderCanFail()
  165. {
  166. $this->expectException(AssertionFailedError::class);
  167. $response = $this->makeMockResponse([
  168. 'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>',
  169. ]);
  170. $response->assertSeeInOrder(['baz', 'bar', 'foo']);
  171. }
  172. public function testAssertSeeInOrderCanFail2()
  173. {
  174. $this->expectException(AssertionFailedError::class);
  175. $response = $this->makeMockResponse([
  176. 'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>',
  177. ]);
  178. $response->assertSeeInOrder(['foo', 'qux', 'bar', 'baz']);
  179. }
  180. public function testAssertSeeText()
  181. {
  182. $response = $this->makeMockResponse([
  183. 'render' => 'foo<strong>bar</strong>baz<strong>qux</strong>',
  184. ]);
  185. $response->assertSeeText('foobar');
  186. $response->assertSeeText(['bazqux', 'foobar']);
  187. }
  188. public function testAssertSeeTextCanFail()
  189. {
  190. $this->expectException(AssertionFailedError::class);
  191. $response = $this->makeMockResponse([
  192. 'render' => 'foo<strong>bar</strong>',
  193. ]);
  194. $response->assertSeeText('bazfoo');
  195. $response->assertSeeText(['bazfoo', 'barqux']);
  196. }
  197. public function testAssertSeeTextEscaped()
  198. {
  199. $response = $this->makeMockResponse([
  200. 'render' => 'laravel &amp; php &amp; friends',
  201. ]);
  202. $response->assertSeeText('laravel & php');
  203. $response->assertSeeText(['php & friends', 'laravel & php']);
  204. }
  205. public function testAssertSeeTextEscapedCanFail()
  206. {
  207. $this->expectException(AssertionFailedError::class);
  208. $response = $this->makeMockResponse([
  209. 'render' => 'laravel &amp; php &amp; friends',
  210. ]);
  211. $response->assertSeeText('foo & bar');
  212. $response->assertSeeText(['foo & bar', 'bar & baz']);
  213. }
  214. public function testAssertSeeTextInOrder()
  215. {
  216. $response = $this->makeMockResponse([
  217. 'render' => 'foo<strong>bar</strong> baz <strong>foo</strong>',
  218. ]);
  219. $response->assertSeeTextInOrder(['foobar', 'baz']);
  220. $response->assertSeeTextInOrder(['foobar', 'baz', 'foo']);
  221. }
  222. public function testAssertSeeTextInOrderEscaped()
  223. {
  224. $response = $this->makeMockResponse([
  225. 'render' => '<strong>laravel &amp; php</strong> <i>phpstorm &gt; sublime</i>',
  226. ]);
  227. $response->assertSeeTextInOrder(['laravel & php', 'phpstorm > sublime']);
  228. }
  229. public function testAssertSeeTextInOrderCanFail()
  230. {
  231. $this->expectException(AssertionFailedError::class);
  232. $response = $this->makeMockResponse([
  233. 'render' => 'foo<strong>bar</strong> baz <strong>foo</strong>',
  234. ]);
  235. $response->assertSeeTextInOrder(['baz', 'foobar']);
  236. }
  237. public function testAssertSeeTextInOrderCanFail2()
  238. {
  239. $this->expectException(AssertionFailedError::class);
  240. $response = $this->makeMockResponse([
  241. 'render' => 'foo<strong>bar</strong> baz <strong>foo</strong>',
  242. ]);
  243. $response->assertSeeTextInOrder(['foobar', 'qux', 'baz']);
  244. }
  245. public function testAssertDontSee()
  246. {
  247. $response = $this->makeMockResponse([
  248. 'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>',
  249. ]);
  250. $response->assertDontSee('laravel');
  251. $response->assertDontSee(['php', 'friends']);
  252. }
  253. public function testAssertDontSeeCanFail()
  254. {
  255. $this->expectException(AssertionFailedError::class);
  256. $response = $this->makeMockResponse([
  257. 'render' => '<ul><li>foo</li><li>bar</li><li>baz</li><li>foo</li></ul>',
  258. ]);
  259. $response->assertDontSee('foo');
  260. $response->assertDontSee(['baz', 'bar']);
  261. }
  262. public function testAssertDontSeeEscaped()
  263. {
  264. $response = $this->makeMockResponse([
  265. 'render' => 'laravel &amp; php &amp; friends',
  266. ]);
  267. $response->assertDontSee('foo & bar');
  268. $response->assertDontSee(['bar & baz', 'foo & bar']);
  269. }
  270. public function testAssertDontSeeEscapedCanFail()
  271. {
  272. $this->expectException(AssertionFailedError::class);
  273. $response = $this->makeMockResponse([
  274. 'render' => 'laravel &amp; php &amp; friends',
  275. ]);
  276. $response->assertDontSee('laravel & php');
  277. $response->assertDontSee(['php & friends', 'laravel & php']);
  278. }
  279. public function testAssertDontSeeText()
  280. {
  281. $response = $this->makeMockResponse([
  282. 'render' => 'foo<strong>bar</strong>baz<strong>qux</strong>',
  283. ]);
  284. $response->assertDontSeeText('laravelphp');
  285. $response->assertDontSeeText(['phpfriends', 'laravelphp']);
  286. }
  287. public function testAssertDontSeeTextCanFail()
  288. {
  289. $this->expectException(AssertionFailedError::class);
  290. $response = $this->makeMockResponse([
  291. 'render' => 'foo<strong>bar</strong>baz<strong>qux</strong>',
  292. ]);
  293. $response->assertDontSeeText('foobar');
  294. $response->assertDontSeeText(['bazqux', 'foobar']);
  295. }
  296. public function testAssertDontSeeTextEscaped()
  297. {
  298. $response = $this->makeMockResponse([
  299. 'render' => 'laravel &amp; php &amp; friends',
  300. ]);
  301. $response->assertDontSeeText('foo & bar');
  302. $response->assertDontSeeText(['bar & baz', 'foo & bar']);
  303. }
  304. public function testAssertDontSeeTextEscapedCanFail()
  305. {
  306. $this->expectException(AssertionFailedError::class);
  307. $response = $this->makeMockResponse([
  308. 'render' => 'laravel &amp; php &amp; friends',
  309. ]);
  310. $response->assertDontSeeText('laravel & php');
  311. $response->assertDontSeeText(['php & friends', 'laravel & php']);
  312. }
  313. public function testAssertOk()
  314. {
  315. $statusCode = 500;
  316. $this->expectException(AssertionFailedError::class);
  317. $this->expectExceptionMessage('Expected response status code');
  318. $baseResponse = tap(new Response, function ($response) use ($statusCode) {
  319. $response->setStatusCode($statusCode);
  320. });
  321. $response = TestResponse::fromBaseResponse($baseResponse);
  322. $response->assertOk();
  323. }
  324. public function testAssertCreated()
  325. {
  326. $statusCode = 500;
  327. $this->expectException(AssertionFailedError::class);
  328. $this->expectExceptionMessage('Expected response status code');
  329. $baseResponse = tap(new Response, function ($response) use ($statusCode) {
  330. $response->setStatusCode($statusCode);
  331. });
  332. $response = TestResponse::fromBaseResponse($baseResponse);
  333. $response->assertCreated();
  334. }
  335. public function testAssertNotFound()
  336. {
  337. $statusCode = 500;
  338. $this->expectException(AssertionFailedError::class);
  339. $this->expectExceptionMessage('Expected response status code');
  340. $baseResponse = tap(new Response, function ($response) use ($statusCode) {
  341. $response->setStatusCode($statusCode);
  342. });
  343. $response = TestResponse::fromBaseResponse($baseResponse);
  344. $response->assertNotFound();
  345. }
  346. public function testAssertForbidden()
  347. {
  348. $statusCode = 500;
  349. $this->expectException(AssertionFailedError::class);
  350. $this->expectExceptionMessage('Expected response status code');
  351. $baseResponse = tap(new Response, function ($response) use ($statusCode) {
  352. $response->setStatusCode($statusCode);
  353. });
  354. $response = TestResponse::fromBaseResponse($baseResponse);
  355. $response->assertForbidden();
  356. }
  357. public function testAssertUnauthorized()
  358. {
  359. $statusCode = 500;
  360. $this->expectException(AssertionFailedError::class);
  361. $this->expectExceptionMessage('Expected response status code');
  362. $baseResponse = tap(new Response, function ($response) use ($statusCode) {
  363. $response->setStatusCode($statusCode);
  364. });
  365. $response = TestResponse::fromBaseResponse($baseResponse);
  366. $response->assertUnauthorized();
  367. }
  368. public function testAssertUnprocessable()
  369. {
  370. $statusCode = 500;
  371. $this->expectException(AssertionFailedError::class);
  372. $this->expectExceptionMessage('Expected response status code');
  373. $baseResponse = tap(new Response, function ($response) use ($statusCode) {
  374. $response->setStatusCode($statusCode);
  375. });
  376. $response = TestResponse::fromBaseResponse($baseResponse);
  377. $response->assertUnprocessable();
  378. }
  379. public function testAssertNoContentAsserts204StatusCodeByDefault()
  380. {
  381. $statusCode = 500;
  382. $this->expectException(AssertionFailedError::class);
  383. $this->expectExceptionMessage('Expected response status code');
  384. $baseResponse = tap(new Response, function ($response) use ($statusCode) {
  385. $response->setStatusCode($statusCode);
  386. });
  387. $response = TestResponse::fromBaseResponse($baseResponse);
  388. $response->assertNoContent();
  389. }
  390. public function testAssertNoContentAssertsExpectedStatusCode()
  391. {
  392. $statusCode = 500;
  393. $expectedStatusCode = 418;
  394. $this->expectException(AssertionFailedError::class);
  395. $this->expectExceptionMessage('Expected response status code');
  396. $baseResponse = tap(new Response, function ($response) use ($statusCode) {
  397. $response->setStatusCode($statusCode);
  398. });
  399. $response = TestResponse::fromBaseResponse($baseResponse);
  400. $response->assertNoContent($expectedStatusCode);
  401. }
  402. public function testAssertNoContentAssertsEmptyContent()
  403. {
  404. $this->expectException(AssertionFailedError::class);
  405. $this->expectExceptionMessage('Response content is not empty');
  406. $baseResponse = tap(new Response, function ($response) {
  407. $response->setStatusCode(204);
  408. $response->setContent('non-empty-response-content');
  409. });
  410. $response = TestResponse::fromBaseResponse($baseResponse);
  411. $response->assertNoContent();
  412. }
  413. public function testAssertStatus()
  414. {
  415. $statusCode = 500;
  416. $expectedStatusCode = 401;
  417. $this->expectException(AssertionFailedError::class);
  418. $this->expectExceptionMessage('Expected response status code');
  419. $baseResponse = tap(new Response, function ($response) use ($statusCode) {
  420. $response->setStatusCode($statusCode);
  421. });
  422. $response = TestResponse::fromBaseResponse($baseResponse);
  423. $response->assertStatus($expectedStatusCode);
  424. }
  425. public function testAssertStatusShowsExceptionOnUnexpected500()
  426. {
  427. $statusCode = 500;
  428. $expectedStatusCode = 200;
  429. $this->expectException(AssertionFailedError::class);
  430. $this->expectExceptionMessage('Test exception message');
  431. $baseResponse = tap(new Response, function ($response) use ($statusCode) {
  432. $response->setStatusCode($statusCode);
  433. });
  434. $exceptions = collect([new Exception('Test exception message')]);
  435. $response = TestResponse::fromBaseResponse($baseResponse)
  436. ->withExceptions($exceptions);
  437. $response->assertStatus($expectedStatusCode);
  438. }
  439. public function testAssertStatusShowsErrorsOnUnexpectedErrorRedirect()
  440. {
  441. $statusCode = 302;
  442. $expectedStatusCode = 200;
  443. $this->expectException(AssertionFailedError::class);
  444. $this->expectExceptionMessage('The first name field is required.');
  445. $baseResponse = tap(new RedirectResponse('/', $statusCode), function ($response) {
  446. $response->setSession(new Store('test-session', new ArraySessionHandler(1)));
  447. $response->withErrors([
  448. 'first_name' => 'The first name field is required.',
  449. 'last_name' => 'The last name field is required.',
  450. ]);
  451. });
  452. $response = TestResponse::fromBaseResponse($baseResponse);
  453. $response->assertStatus($expectedStatusCode);
  454. }
  455. public function testAssertStatusShowsJsonErrorsOnUnexpected422()
  456. {
  457. $statusCode = 422;
  458. $expectedStatusCode = 200;
  459. $this->expectException(AssertionFailedError::class);
  460. $this->expectExceptionMessage('"The first name field is required."');
  461. $baseResponse = new Response(
  462. [
  463. 'message' => 'The given data was invalid.',
  464. 'errors' => [
  465. 'first_name' => 'The first name field is required.',
  466. 'last_name' => 'The last name field is required.',
  467. ],
  468. ],
  469. $statusCode
  470. );
  471. $response = TestResponse::fromBaseResponse($baseResponse);
  472. $response->assertStatus($expectedStatusCode);
  473. }
  474. public function testAssertStatusWhenJsonIsFalse()
  475. {
  476. $baseResponse = new Response('false', 200, ['Content-Type' => 'application/json']);
  477. $response = TestResponse::fromBaseResponse($baseResponse);
  478. $response->assertStatus(200);
  479. }
  480. public function testAssertStatusWhenJsonIsEncoded()
  481. {
  482. $baseResponse = tap(new Response, function ($response) {
  483. $response->header('Content-Type', 'application/json');
  484. $response->header('Content-Encoding', 'gzip');
  485. $response->setContent('b"x£½V*.I,)-V▓R╩¤V¬\x05\x00+ü\x059"');
  486. });
  487. $response = TestResponse::fromBaseResponse($baseResponse);
  488. $response->assertStatus(200);
  489. }
  490. public function testAssertHeader()
  491. {
  492. $this->expectException(AssertionFailedError::class);
  493. $baseResponse = tap(new Response, function ($response) {
  494. $response->header('Location', '/foo');
  495. });
  496. $response = TestResponse::fromBaseResponse($baseResponse);
  497. $response->assertHeader('Location', '/bar');
  498. }
  499. public function testAssertHeaderMissing()
  500. {
  501. $this->expectException(ExpectationFailedException::class);
  502. $this->expectExceptionMessage('Unexpected header [Location] is present on response.');
  503. $baseResponse = tap(new Response, function ($response) {
  504. $response->header('Location', '/foo');
  505. });
  506. $response = TestResponse::fromBaseResponse($baseResponse);
  507. $response->assertHeaderMissing('Location');
  508. }
  509. public function testAssertJsonWithArray()
  510. {
  511. $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceStub));
  512. $resource = new JsonSerializableSingleResourceStub;
  513. $response->assertJson($resource->jsonSerialize());
  514. }
  515. public function testAssertJsonWithNull()
  516. {
  517. $response = TestResponse::fromBaseResponse(new Response(null));
  518. $this->expectException(AssertionFailedError::class);
  519. $this->expectExceptionMessage('Invalid JSON was returned from the route.');
  520. $resource = new JsonSerializableSingleResourceStub;
  521. $response->assertJson($resource->jsonSerialize());
  522. }
  523. public function testAssertJsonWithFluent()
  524. {
  525. $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceStub));
  526. $response->assertJson(function (AssertableJson $json) {
  527. $json->where('0.foo', 'foo 0');
  528. });
  529. }
  530. public function testAssertJsonWithFluentFailsWhenNotInteractingWithAllProps()
  531. {
  532. $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub));
  533. $this->expectException(AssertionFailedError::class);
  534. $this->expectExceptionMessage('Unexpected properties were found on the root level.');
  535. $response->assertJson(function (AssertableJson $json) {
  536. $json->where('foo', 'bar');
  537. });
  538. }
  539. public function testAssertJsonWithFluentSkipsInteractionWhenTopLevelKeysNonAssociative()
  540. {
  541. $response = TestResponse::fromBaseResponse(new Response([
  542. ['foo' => 'bar'],
  543. ['foo' => 'baz'],
  544. ]));
  545. $response->assertJson(function (AssertableJson $json) {
  546. //
  547. });
  548. }
  549. public function testAssertJsonWithFluentHasAnyThrows()
  550. {
  551. $response = TestResponse::fromBaseResponse(new Response([]));
  552. $this->expectException(AssertionFailedError::class);
  553. $this->expectExceptionMessage('None of properties [data, errors, meta] exist.');
  554. $response->assertJson(function (AssertableJson $json) {
  555. $json->hasAny('data', 'errors', 'meta');
  556. });
  557. }
  558. public function testAssertJsonWithFluentHasAnyPasses()
  559. {
  560. $response = TestResponse::fromBaseResponse(new Response([
  561. 'data' => [],
  562. ]));
  563. $response->assertJson(function (AssertableJson $json) {
  564. $json->hasAny('data', 'errors', 'meta');
  565. });
  566. }
  567. public function testAssertSimilarJsonWithMixed()
  568. {
  569. $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub));
  570. $resource = new JsonSerializableMixedResourcesStub;
  571. $expected = $resource->jsonSerialize();
  572. $response->assertSimilarJson($expected);
  573. $expected['bars'][0] = ['bar' => 'foo 2', 'foo' => 'bar 2'];
  574. $expected['bars'][2] = ['bar' => 'foo 0', 'foo' => 'bar 0'];
  575. $response->assertSimilarJson($expected);
  576. }
  577. public function testAssertExactJsonWithMixedWhenDataIsExactlySame()
  578. {
  579. $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub));
  580. $resource = new JsonSerializableMixedResourcesStub;
  581. $expected = $resource->jsonSerialize();
  582. $response->assertExactJson($expected);
  583. }
  584. public function testAssertExactJsonWithMixedWhenDataIsSimilar()
  585. {
  586. $this->expectException(AssertionFailedError::class);
  587. $this->expectExceptionMessage('Failed asserting that two strings are equal.');
  588. $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub));
  589. $resource = new JsonSerializableMixedResourcesStub;
  590. $expected = $resource->jsonSerialize();
  591. $expected['bars'][0] = ['bar' => 'foo 2', 'foo' => 'bar 2'];
  592. $expected['bars'][2] = ['bar' => 'foo 0', 'foo' => 'bar 0'];
  593. $response->assertExactJson($expected);
  594. }
  595. public function testAssertJsonPath()
  596. {
  597. $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceStub));
  598. $response->assertJsonPath('0.foo', 'foo 0');
  599. $response->assertJsonPath('0.foo', 'foo 0');
  600. $response->assertJsonPath('0.bar', 'bar 0');
  601. $response->assertJsonPath('0.foobar', 'foobar 0');
  602. $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub));
  603. $response->assertJsonPath('foo', 'bar');
  604. $response->assertJsonPath('foobar.foobar_foo', 'foo');
  605. $response->assertJsonPath('foobar.foobar_bar', 'bar');
  606. $response->assertJsonPath('foobar.foobar_foo', 'foo')->assertJsonPath('foobar.foobar_bar', 'bar');
  607. $response->assertJsonPath('bars', [
  608. ['bar' => 'foo 0', 'foo' => 'bar 0'],
  609. ['bar' => 'foo 1', 'foo' => 'bar 1'],
  610. ['bar' => 'foo 2', 'foo' => 'bar 2'],
  611. ]);
  612. $response->assertJsonPath('bars.0', ['bar' => 'foo 0', 'foo' => 'bar 0']);
  613. $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceWithIntegersStub));
  614. $response->assertJsonPath('0.id', 10);
  615. $response->assertJsonPath('1.id', 20);
  616. $response->assertJsonPath('2.id', 30);
  617. }
  618. public function testAssertJsonPathCanFail()
  619. {
  620. $this->expectException(AssertionFailedError::class);
  621. $this->expectExceptionMessage('Failed asserting that 10 is identical to \'10\'.');
  622. $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceWithIntegersStub));
  623. $response->assertJsonPath('0.id', '10');
  624. }
  625. public function testAssertJsonFragment()
  626. {
  627. $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceStub));
  628. $response->assertJsonFragment(['foo' => 'foo 0']);
  629. $response->assertJsonFragment(['foo' => 'foo 0', 'bar' => 'bar 0', 'foobar' => 'foobar 0']);
  630. $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub));
  631. $response->assertJsonFragment(['foo' => 'bar']);
  632. $response->assertJsonFragment(['foobar_foo' => 'foo']);
  633. $response->assertJsonFragment(['foobar' => ['foobar_foo' => 'foo', 'foobar_bar' => 'bar']]);
  634. $response->assertJsonFragment(['foo' => 'bar 0', 'bar' => ['foo' => 'bar 0', 'bar' => 'foo 0']]);
  635. $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceWithIntegersStub));
  636. $response->assertJsonFragment(['id' => 10]);
  637. }
  638. public function testAssertJsonFragmentCanFail()
  639. {
  640. $this->expectException(AssertionFailedError::class);
  641. $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceWithIntegersStub));
  642. $response->assertJsonFragment(['id' => 1]);
  643. }
  644. public function testAssertJsonStructure()
  645. {
  646. $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub));
  647. // Without structure
  648. $response->assertJsonStructure();
  649. // At root
  650. $response->assertJsonStructure(['foo']);
  651. // Nested
  652. $response->assertJsonStructure(['foobar' => ['foobar_foo', 'foobar_bar']]);
  653. // Wildcard (repeating structure)
  654. $response->assertJsonStructure(['bars' => ['*' => ['bar', 'foo']]]);
  655. // Wildcard (numeric keys)
  656. $response->assertJsonStructure(['numeric_keys' => ['*' => ['bar', 'foo']]]);
  657. // Nested after wildcard
  658. $response->assertJsonStructure(['baz' => ['*' => ['foo', 'bar' => ['foo', 'bar']]]]);
  659. // Wildcard (repeating structure) at root
  660. $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceStub));
  661. $response->assertJsonStructure(['*' => ['foo', 'bar', 'foobar']]);
  662. }
  663. public function testAssertJsonCount()
  664. {
  665. $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub));
  666. // With falsey key
  667. $response->assertJsonCount(1, '0');
  668. // With simple key
  669. $response->assertJsonCount(3, 'bars');
  670. // With nested key
  671. $response->assertJsonCount(1, 'barfoo.0.bar');
  672. $response->assertJsonCount(3, 'barfoo.2.bar');
  673. // Without structure
  674. $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceStub));
  675. $response->assertJsonCount(4);
  676. }
  677. public function testAssertJsonMissing()
  678. {
  679. $this->expectException(AssertionFailedError::class);
  680. $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceWithIntegersStub));
  681. $response->assertJsonMissing(['id' => 20]);
  682. }
  683. public function testAssertJsonMissingExact()
  684. {
  685. $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceWithIntegersStub));
  686. $response->assertJsonMissingExact(['id' => 2]);
  687. // This is missing because bar has changed to baz
  688. $response->assertJsonMissingExact(['id' => 20, 'foo' => 'baz']);
  689. }
  690. public function testAssertJsonMissingExactCanFail()
  691. {
  692. $this->expectException(AssertionFailedError::class);
  693. $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceWithIntegersStub));
  694. $response->assertJsonMissingExact(['id' => 20]);
  695. }
  696. public function testAssertJsonMissingExactCanFail2()
  697. {
  698. $this->expectException(AssertionFailedError::class);
  699. $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableSingleResourceWithIntegersStub));
  700. $response->assertJsonMissingExact(['id' => 20, 'foo' => 'bar']);
  701. }
  702. public function testAssertJsonValidationErrors()
  703. {
  704. $data = [
  705. 'status' => 'ok',
  706. 'errors' => ['foo' => 'oops'],
  707. ];
  708. $testResponse = TestResponse::fromBaseResponse(
  709. (new Response)->setContent(json_encode($data))
  710. );
  711. $testResponse->assertJsonValidationErrors('foo');
  712. }
  713. public function testAssertJsonValidationErrorsUsingAssertInvalid()
  714. {
  715. $data = [
  716. 'status' => 'ok',
  717. 'errors' => ['foo' => 'oops'],
  718. ];
  719. $testResponse = TestResponse::fromBaseResponse(
  720. (new Response('', 200, ['Content-Type' => 'application/json']))->setContent(json_encode($data))
  721. );
  722. $testResponse->assertInvalid('foo');
  723. }
  724. public function testAssertSessionValidationErrorsUsingAssertInvalid()
  725. {
  726. app()->instance('session.store', $store = new Store('test-session', new ArraySessionHandler(1)));
  727. $store->put('errors', $errorBag = new ViewErrorBag);
  728. $errorBag->put('default', new MessageBag([
  729. 'first_name' => [
  730. 'Your first name is required',
  731. 'Your first name must be at least 1 character',
  732. ],
  733. ]));
  734. $testResponse = TestResponse::fromBaseResponse(new Response);
  735. $testResponse->assertValid('last_name');
  736. $testResponse->assertValid(['last_name']);
  737. $testResponse->assertInvalid();
  738. $testResponse->assertInvalid('first_name');
  739. $testResponse->assertInvalid(['first_name']);
  740. $testResponse->assertInvalid(['first_name' => 'required']);
  741. $testResponse->assertInvalid(['first_name' => 'character']);
  742. }
  743. public function testAssertSessionValidationErrorsUsingAssertValid()
  744. {
  745. app()->instance('session.store', $store = new Store('test-session', new ArraySessionHandler(1)));
  746. $store->put('errors', $errorBag = new ViewErrorBag);
  747. $errorBag->put('default', new MessageBag([
  748. ]));
  749. $testResponse = TestResponse::fromBaseResponse(new Response);
  750. $testResponse->assertValid();
  751. }
  752. public function testAssertJsonValidationErrorsCustomErrorsName()
  753. {
  754. $data = [
  755. 'status' => 'ok',
  756. 'data' => ['foo' => 'oops'],
  757. ];
  758. $testResponse = TestResponse::fromBaseResponse(
  759. (new Response)->setContent(json_encode($data))
  760. );
  761. $testResponse->assertJsonValidationErrors('foo', 'data');
  762. }
  763. public function testAssertJsonValidationErrorsCustomNestedErrorsName()
  764. {
  765. $data = [
  766. 'status' => 'ok',
  767. 'data' => ['errors' => ['foo' => 'oops']],
  768. ];
  769. $testResponse = TestResponse::fromBaseResponse(
  770. (new Response)->setContent(json_encode($data))
  771. );
  772. $testResponse->assertJsonValidationErrors('foo', 'data.errors');
  773. }
  774. public function testAssertJsonValidationErrorsCanFail()
  775. {
  776. $this->expectException(AssertionFailedError::class);
  777. $data = [
  778. 'status' => 'ok',
  779. 'errors' => ['foo' => 'oops'],
  780. ];
  781. $testResponse = TestResponse::fromBaseResponse(
  782. (new Response)->setContent(json_encode($data))
  783. );
  784. $testResponse->assertJsonValidationErrors('bar');
  785. }
  786. public function testAssertJsonValidationErrorsCanFailWhenThereAreNoErrors()
  787. {
  788. $this->expectException(AssertionFailedError::class);
  789. $data = ['status' => 'ok'];
  790. $testResponse = TestResponse::fromBaseResponse(
  791. (new Response)->setContent(json_encode($data))
  792. );
  793. $testResponse->assertJsonValidationErrors('bar');
  794. }
  795. public function testAssertJsonValidationErrorsFailsWhenGivenAnEmptyArray()
  796. {
  797. $this->expectException(AssertionFailedError::class);
  798. $testResponse = TestResponse::fromBaseResponse(
  799. (new Response)->setContent(json_encode(['errors' => ['foo' => 'oops']]))
  800. );
  801. $testResponse->assertJsonValidationErrors([]);
  802. }
  803. public function testAssertJsonValidationErrorsWithArray()
  804. {
  805. $data = [
  806. 'status' => 'ok',
  807. 'errors' => ['foo' => 'one', 'bar' => 'two'],
  808. ];
  809. $testResponse = TestResponse::fromBaseResponse(
  810. (new Response)->setContent(json_encode($data))
  811. );
  812. $testResponse->assertJsonValidationErrors(['foo', 'bar']);
  813. }
  814. public function testAssertJsonValidationErrorMessages()
  815. {
  816. $data = [
  817. 'status' => 'ok',
  818. 'errors' => ['key' => 'foo'],
  819. ];
  820. $testResponse = TestResponse::fromBaseResponse(
  821. (new Response)->setContent(json_encode($data))
  822. );
  823. $testResponse->assertJsonValidationErrors(['key' => 'foo']);
  824. }
  825. public function testAssertJsonValidationErrorContainsMessages()
  826. {
  827. $data = [
  828. 'status' => 'ok',
  829. 'errors' => ['key' => 'foo bar'],
  830. ];
  831. $testResponse = TestResponse::fromBaseResponse(
  832. (new Response)->setContent(json_encode($data))
  833. );
  834. $testResponse->assertJsonValidationErrors(['key' => 'foo']);
  835. }
  836. public function testAssertJsonValidationErrorMessagesCanFail()
  837. {
  838. $this->expectException(AssertionFailedError::class);
  839. $data = [
  840. 'status' => 'ok',
  841. 'errors' => ['key' => 'foo'],
  842. ];
  843. $testResponse = TestResponse::fromBaseResponse(
  844. (new Response)->setContent(json_encode($data))
  845. );
  846. $testResponse->assertJsonValidationErrors(['key' => 'bar']);
  847. }
  848. public function testAssertJsonValidationErrorMessageKeyCanFail()
  849. {
  850. $this->expectException(AssertionFailedError::class);
  851. $data = [
  852. 'status' => 'ok',
  853. 'errors' => ['foo' => 'value'],
  854. ];
  855. $testResponse = TestResponse::fromBaseResponse(
  856. (new Response)->setContent(json_encode($data))
  857. );
  858. $testResponse->assertJsonValidationErrors(['bar' => 'value']);
  859. }
  860. public function testAssertJsonValidationErrorMessagesMultipleMessages()
  861. {
  862. $data = [
  863. 'status' => 'ok',
  864. 'errors' => ['one' => 'foo', 'two' => 'bar'],
  865. ];
  866. $testResponse = TestResponse::fromBaseResponse(
  867. (new Response)->setContent(json_encode($data))
  868. );
  869. $testResponse->assertJsonValidationErrors(['one' => 'foo', 'two' => 'bar']);
  870. }
  871. public function testAssertJsonValidationErrorMessagesMultipleMessagesCanFail()
  872. {
  873. $this->expectException(AssertionFailedError::class);
  874. $data = [
  875. 'status' => 'ok',
  876. 'errors' => ['one' => 'foo', 'two' => 'bar'],
  877. ];
  878. $testResponse = TestResponse::fromBaseResponse(
  879. (new Response)->setContent(json_encode($data))
  880. );
  881. $testResponse->assertJsonValidationErrors(['one' => 'foo', 'three' => 'baz']);
  882. }
  883. public function testAssertJsonValidationErrorMessagesMixed()
  884. {
  885. $data = [
  886. 'status' => 'ok',
  887. 'errors' => ['one' => 'foo', 'two' => 'bar'],
  888. ];
  889. $testResponse = TestResponse::fromBaseResponse(
  890. (new Response)->setContent(json_encode($data))
  891. );
  892. $testResponse->assertJsonValidationErrors(['one' => 'foo', 'two']);
  893. }
  894. public function testAssertJsonValidationErrorMessagesMixedCanFail()
  895. {
  896. $this->expectException(AssertionFailedError::class);
  897. $data = [
  898. 'status' => 'ok',
  899. 'errors' => ['one' => 'foo', 'two' => 'bar'],
  900. ];
  901. $testResponse = TestResponse::fromBaseResponse(
  902. (new Response)->setContent(json_encode($data))
  903. );
  904. $testResponse->assertJsonValidationErrors(['one' => 'taylor', 'otwell']);
  905. }
  906. public function testAssertJsonValidationErrorMessagesMultipleErrors()
  907. {
  908. $data = [
  909. 'status' => 'ok',
  910. 'errors' => [
  911. 'one' => [
  912. 'First error message.',
  913. 'Second error message.',
  914. ],
  915. ],
  916. ];
  917. $testResponse = TestResponse::fromBaseResponse(
  918. (new Response)->setContent(json_encode($data))
  919. );
  920. $testResponse->assertJsonValidationErrors(['one' => ['First error message.', 'Second error message.']]);
  921. }
  922. public function testAssertJsonValidationErrorMessagesMultipleErrorsCanFail()
  923. {
  924. $this->expectException(AssertionFailedError::class);
  925. $data = [
  926. 'status' => 'ok',
  927. 'errors' => [
  928. 'one' => [
  929. 'First error message.',
  930. ],
  931. ],
  932. ];
  933. $testResponse = TestResponse::fromBaseResponse(
  934. (new Response)->setContent(json_encode($data))
  935. );
  936. $testResponse->assertJsonValidationErrors(['one' => ['First error message.', 'Second error message.']]);
  937. }
  938. public function testAssertJsonMissingValidationErrors()
  939. {
  940. $baseResponse = tap(new Response, function ($response) {
  941. $response->setContent(json_encode(['errors' => [
  942. 'foo' => [],
  943. 'bar' => ['one', 'two'],
  944. ]]));
  945. });
  946. $response = TestResponse::fromBaseResponse($baseResponse);
  947. $response->assertJsonMissingValidationErrors('baz');
  948. $baseResponse = tap(new Response, function ($response) {
  949. $response->setContent(json_encode(['foo' => 'bar']));
  950. });
  951. $response = TestResponse::fromBaseResponse($baseResponse);
  952. $response->assertJsonMissingValidationErrors('foo');
  953. }
  954. public function testAssertJsonMissingValidationErrorsCanFail()
  955. {
  956. $this->expectException(AssertionFailedError::class);
  957. $baseResponse = tap(new Response, function ($response) {
  958. $response->setContent(json_encode(['errors' => [
  959. 'foo' => [],
  960. 'bar' => ['one', 'two'],
  961. ]]));
  962. });
  963. $response = TestResponse::fromBaseResponse($baseResponse);
  964. $response->assertJsonMissingValidationErrors('foo');
  965. }
  966. public function testAssertJsonMissingValidationErrorsCanFail2()
  967. {
  968. $this->expectException(AssertionFailedError::class);
  969. $baseResponse = tap(new Response, function ($response) {
  970. $response->setContent(json_encode(['errors' => [
  971. 'foo' => [],
  972. 'bar' => ['one', 'two'],
  973. ]]));
  974. });
  975. $response = TestResponse::fromBaseResponse($baseResponse);
  976. $response->assertJsonMissingValidationErrors('bar');
  977. }
  978. public function testAssertJsonMissingValidationErrorsCanFail3()
  979. {
  980. $this->expectException(AssertionFailedError::class);
  981. $baseResponse = tap(new Response, function ($response) {
  982. $response->setContent(
  983. json_encode([
  984. 'data' => [
  985. 'errors' => [
  986. 'foo' => ['one'],
  987. ],
  988. ],
  989. ]),
  990. );
  991. });
  992. $response = TestResponse::fromBaseResponse($baseResponse);
  993. $response->assertJsonMissingValidationErrors('foo', 'data.errors');
  994. }
  995. public function testAssertJsonMissingValidationErrorsWithoutArgument()
  996. {
  997. $data = ['status' => 'ok'];
  998. $testResponse = TestResponse::fromBaseResponse(
  999. (new Response)->setContent(json_encode($data))
  1000. );
  1001. $testResponse->assertJsonMissingValidationErrors();
  1002. }
  1003. public function testAssertJsonMissingValidationErrorsWithoutArgumentWhenErrorsIsEmpty()
  1004. {
  1005. $data = ['status' => 'ok', 'errors' => []];
  1006. $testResponse = TestResponse::fromBaseResponse(
  1007. (new Response)->setContent(json_encode($data))
  1008. );
  1009. $testResponse->assertJsonMissingValidationErrors();
  1010. }
  1011. public function testAssertJsonMissingValidationErrorsWithoutArgumentCanFail()
  1012. {
  1013. $this->expectException(AssertionFailedError::class);
  1014. $data = ['errors' => ['foo' => []]];
  1015. $testResponse = TestResponse::fromBaseResponse(
  1016. (new Response)->setContent(json_encode($data))
  1017. );
  1018. $testResponse->assertJsonMissingValidationErrors();
  1019. }
  1020. public function testAssertJsonMissingValidationErrorsOnInvalidJson()
  1021. {
  1022. $this->expectException(AssertionFailedError::class);
  1023. $this->expectExceptionMessage('Invalid JSON was returned from the route.');
  1024. $invalidJsonResponse = TestResponse::fromBaseResponse(
  1025. (new Response)->setContent('~invalid json')
  1026. );
  1027. $invalidJsonResponse->assertJsonMissingValidationErrors();
  1028. }
  1029. public function testAssertJsonMissingValidationErrorsCustomErrorsName()
  1030. {
  1031. $data = [
  1032. 'status' => 'ok',
  1033. 'data' => ['foo' => 'oops'],
  1034. ];
  1035. $testResponse = TestResponse::fromBaseResponse(
  1036. (new Response)->setContent(json_encode($data))
  1037. );
  1038. $testResponse->assertJsonMissingValidationErrors('bar', 'data');
  1039. }
  1040. public function testAssertJsonMissingValidationErrorsNestedCustomErrorsName1()
  1041. {
  1042. $data = [
  1043. 'status' => 'ok',
  1044. 'data' => [
  1045. 'errors' => ['foo' => 'oops'],
  1046. ],
  1047. ];
  1048. $testResponse = TestResponse::fromBaseResponse(
  1049. (new Response)->setContent(json_encode($data))
  1050. );
  1051. $testResponse->assertJsonMissingValidationErrors('bar', 'data.errors');
  1052. }
  1053. public function testAssertJsonMissingValidationErrorsNestedCustomErrorsName2()
  1054. {
  1055. $testResponse = TestResponse::fromBaseResponse(
  1056. (new Response)->setContent(json_encode([]))
  1057. );
  1058. $testResponse->assertJsonMissingValidationErrors('bar', 'data.errors');
  1059. }
  1060. public function testAssertDownloadOffered()
  1061. {
  1062. $files = new Filesystem;
  1063. $tempDir = __DIR__.'/tmp';
  1064. $files->makeDirectory($tempDir, 0755, false, true);
  1065. $files->put($tempDir.'/file.txt', 'Hello World');
  1066. $testResponse = TestResponse::fromBaseResponse(new Response(
  1067. $files->get($tempDir.'/file.txt'), 200, [
  1068. 'Content-Disposition' => 'attachment; filename=file.txt',
  1069. ]
  1070. ));
  1071. $testResponse->assertDownload();
  1072. $files->deleteDirectory($tempDir);
  1073. }
  1074. public function testAssertDownloadOfferedWithAFileName()
  1075. {
  1076. $files = new Filesystem;
  1077. $tempDir = __DIR__.'/tmp';
  1078. $files->makeDirectory($tempDir, 0755, false, true);
  1079. $files->put($tempDir.'/file.txt', 'Hello World');
  1080. $testResponse = TestResponse::fromBaseResponse(new Response(
  1081. $files->get($tempDir.'/file.txt'), 200, [
  1082. 'Content-Disposition' => 'attachment; filename = file.txt',
  1083. ]
  1084. ));
  1085. $testResponse->assertDownload('file.txt');
  1086. $files->deleteDirectory($tempDir);
  1087. }
  1088. public function testAssertDownloadOfferedWorksWithBinaryFileResponse()
  1089. {
  1090. $files = new Filesystem;
  1091. $tempDir = __DIR__.'/tmp';
  1092. $files->makeDirectory($tempDir, 0755, false, true);
  1093. $files->put($tempDir.'/file.txt', 'Hello World');
  1094. $testResponse = TestResponse::fromBaseResponse(new BinaryFileResponse(
  1095. $tempDir.'/file.txt', 200, [], true, 'attachment'
  1096. ));
  1097. $testResponse->assertDownload('file.txt');
  1098. $files->deleteDirectory($tempDir);
  1099. }
  1100. public function testAssertDownloadOfferedFailsWithInlineContentDisposition()
  1101. {
  1102. $this->expectException(AssertionFailedError::class);
  1103. $files = new Filesystem;
  1104. $tempDir = __DIR__.'/tmp';
  1105. $files->makeDirectory($tempDir, 0755, false, true);
  1106. $files->put($tempDir.'/file.txt', 'Hello World');
  1107. $testResponse = TestResponse::fromBaseResponse(new BinaryFileResponse(
  1108. $tempDir.'/file.txt', 200, [], true, 'inline'
  1109. ));
  1110. $testResponse->assertDownload();
  1111. $files->deleteDirectory($tempDir);
  1112. }
  1113. public function testAssertDownloadOfferedWithAFileNameWithSpacesInIt()
  1114. {
  1115. $files = new Filesystem;
  1116. $tempDir = __DIR__.'/tmp';
  1117. $files->makeDirectory($tempDir, 0755, false, true);
  1118. $files->put($tempDir.'/file.txt', 'Hello World');
  1119. $testResponse = TestResponse::fromBaseResponse(new Response(
  1120. $files->get($tempDir.'/file.txt'), 200, [
  1121. 'Content-Disposition' => 'attachment; filename = "test file.txt"',
  1122. ]
  1123. ));
  1124. $testResponse->assertDownload('test file.txt');
  1125. $files->deleteDirectory($tempDir);
  1126. }
  1127. public function testMacroable()
  1128. {
  1129. TestResponse::macro('foo', function () {
  1130. return 'bar';
  1131. });
  1132. $response = TestResponse::fromBaseResponse(new Response);
  1133. $this->assertSame(
  1134. 'bar', $response->foo()
  1135. );
  1136. }
  1137. public function testCanBeCreatedFromBinaryFileResponses()
  1138. {
  1139. $files = new Filesystem;
  1140. $tempDir = __DIR__.'/tmp';
  1141. $files->makeDirectory($tempDir, 0755, false, true);
  1142. $files->put($tempDir.'/file.txt', 'Hello World');
  1143. $response = TestResponse::fromBaseResponse(new BinaryFileResponse($tempDir.'/file.txt'));
  1144. $this->assertEquals($tempDir.'/file.txt', $response->getFile()->getPathname());
  1145. $files->deleteDirectory($tempDir);
  1146. }
  1147. public function testJsonHelper()
  1148. {
  1149. $response = TestResponse::fromBaseResponse(new Response(new JsonSerializableMixedResourcesStub));
  1150. $this->assertSame('foo', $response->json('foobar.foobar_foo'));
  1151. $this->assertEquals(
  1152. json_decode($response->getContent(), true),
  1153. $response->json()
  1154. );
  1155. }
  1156. public function testItCanBeTapped()
  1157. {
  1158. $response = TestResponse::fromBaseResponse(
  1159. (new Response)->setContent('')->setStatusCode(418)
  1160. );
  1161. $response->tap(function ($response) {
  1162. $this->assertInstanceOf(TestResponse::class, $response);
  1163. })->assertStatus(418);
  1164. }
  1165. public function testAssertPlainCookie()
  1166. {
  1167. $response = TestResponse::fromBaseResponse(
  1168. (new Response)->withCookie(new Cookie('cookie-name', 'cookie-value'))
  1169. );
  1170. $response->assertPlainCookie('cookie-name', 'cookie-value');
  1171. }
  1172. public function testAssertCookie()
  1173. {
  1174. $container = Container::getInstance();
  1175. $encrypter = new Encrypter(str_repeat('a', 16));
  1176. $container->singleton('encrypter', function () use ($encrypter) {
  1177. return $encrypter;
  1178. });
  1179. $cookieName = 'cookie-name';
  1180. $cookieValue = 'cookie-value';
  1181. $encryptedValue = $encrypter->encrypt(CookieValuePrefix::create($cookieName, $encrypter->getKey()).$cookieValue, false);
  1182. $response = TestResponse::fromBaseResponse(
  1183. (new Response)->withCookie(new Cookie($cookieName, $encryptedValue))
  1184. );
  1185. $response->assertCookie($cookieName, $cookieValue);
  1186. }
  1187. public function testAssertCookieExpired()
  1188. {
  1189. $response = TestResponse::fromBaseResponse(
  1190. (new Response)->withCookie(new Cookie('cookie-name', 'cookie-value', time() - 5000))
  1191. );
  1192. $response->assertCookieExpired('cookie-name');
  1193. }
  1194. public function testAssertSessionCookieExpiredDoesNotTriggerOnSessionCookies()
  1195. {
  1196. $response = TestResponse::fromBaseResponse(
  1197. (new Response)->withCookie(new Cookie('cookie-name', 'cookie-value', 0))
  1198. );
  1199. $this->expectException(ExpectationFailedException::class);
  1200. $response->assertCookieExpired('cookie-name');
  1201. }
  1202. public function testAssertCookieNotExpired()
  1203. {
  1204. $response = TestResponse::fromBaseResponse(
  1205. (new Response)->withCookie(new Cookie('cookie-name', 'cookie-value', time() + 5000))
  1206. );
  1207. $response->assertCookieNotExpired('cookie-name');
  1208. }
  1209. public function testAssertSessionCookieNotExpired()
  1210. {
  1211. $response = TestResponse::fromBaseResponse(
  1212. (new Response)->withCookie(new Cookie('cookie-name', 'cookie-value', 0))
  1213. );
  1214. $response->assertCookieNotExpired('cookie-name');
  1215. }
  1216. public function testAssertCookieMissing()
  1217. {
  1218. $response = TestResponse::fromBaseResponse(new Response);
  1219. $response->assertCookieMissing('cookie-name');
  1220. }
  1221. public function testAssertRedirectContains()
  1222. {
  1223. $response = TestResponse::fromBaseResponse(
  1224. (new Response('', 302))->withHeaders(['Location' => 'https://url.com'])
  1225. );
  1226. $response->assertRedirectContains('url.com');
  1227. $this->expectException(ExpectationFailedException::class);
  1228. $response->assertRedirectContains('url.net');
  1229. }
  1230. private function makeMockResponse($content)
  1231. {
  1232. $baseResponse = tap(new Response, function ($response) use ($content) {
  1233. $response->setContent(m::mock(View::class, $content));
  1234. });
  1235. return TestResponse::fromBaseResponse($baseResponse);
  1236. }
  1237. }
  1238. class JsonSerializableMixedResourcesStub implements JsonSerializable
  1239. {
  1240. public function jsonSerialize(): array
  1241. {
  1242. return [
  1243. 'foo' => 'bar',
  1244. 'foobar' => [
  1245. 'foobar_foo' => 'foo',
  1246. 'foobar_bar' => 'bar',
  1247. ],
  1248. '0' => ['foo'],
  1249. 'bars' => [
  1250. ['bar' => 'foo 0', 'foo' => 'bar 0'],
  1251. ['bar' => 'foo 1', 'foo' => 'bar 1'],
  1252. ['bar' => 'foo 2', 'foo' => 'bar 2'],
  1253. ],
  1254. 'baz' => [
  1255. ['foo' => 'bar 0', 'bar' => ['foo' => 'bar 0', 'bar' => 'foo 0']],
  1256. ['foo' => 'bar 1', 'bar' => ['foo' => 'bar 1', 'bar' => 'foo 1']],
  1257. ],
  1258. 'barfoo' => [
  1259. ['bar' => ['bar' => 'foo 0']],
  1260. ['bar' => ['bar' => 'foo 0', 'foo' => 'foo 0']],
  1261. ['bar' => ['foo' => 'bar 0', 'bar' => 'foo 0', 'rab' => 'rab 0']],
  1262. ],
  1263. 'numeric_keys' => [
  1264. 2 => ['bar' => 'foo 0', 'foo' => 'bar 0'],
  1265. 3 => ['bar' => 'foo 1', 'foo' => 'bar 1'],
  1266. 4 => ['bar' => 'foo 2', 'foo' => 'bar 2'],
  1267. ],
  1268. ];
  1269. }
  1270. }
  1271. class JsonSerializableSingleResourceStub implements JsonSerializable
  1272. {
  1273. public function jsonSerialize(): array
  1274. {
  1275. return [
  1276. ['foo' => 'foo 0', 'bar' => 'bar 0', 'foobar' => 'foobar 0'],
  1277. ['foo' => 'foo 1', 'bar' => 'bar 1', 'foobar' => 'foobar 1'],
  1278. ['foo' => 'foo 2', 'bar' => 'bar 2', 'foobar' => 'foobar 2'],
  1279. ['foo' => 'foo 3', 'bar' => 'bar 3', 'foobar' => 'foobar 3'],
  1280. ];
  1281. }
  1282. }
  1283. class JsonSerializableSingleResourceWithIntegersStub implements JsonSerializable
  1284. {
  1285. public function jsonSerialize(): array
  1286. {
  1287. return [
  1288. ['id' => 10, 'foo' => 'bar'],
  1289. ['id' => 20, 'foo' => 'bar'],
  1290. ['id' => 30, 'foo' => 'bar'],
  1291. ];
  1292. }
  1293. }