Skip to content

Commit 9d5ae0d

Browse files
committed
feat(MakesHttpRequests): it improves transaction control in tests http calls
1 parent eb14349 commit 9d5ae0d

File tree

2 files changed

+704
-13
lines changed

2 files changed

+704
-13
lines changed

src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php

Lines changed: 85 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
use Illuminate\Testing\TestResponse;
1313
use Symfony\Component\HttpFoundation\File\UploadedFile as SymfonyUploadedFile;
1414
use Symfony\Component\HttpFoundation\Request as SymfonyRequest;
15+
use Illuminate\Foundation\Testing\DatabaseTransactions;
1516

1617
trait MakesHttpRequests
1718
{
@@ -365,7 +366,11 @@ public function get($uri, array $headers = [])
365366
$server = $this->transformHeadersToServerVars($headers);
366367
$cookies = $this->prepareCookiesForRequest();
367368

368-
return $this->call('GET', $uri, [], $cookies, [], $server);
369+
$testResponse = $this->call('GET', $uri, [], $cookies, [], $server);
370+
371+
$this->resetDatabaseTransactionLevelToOne();
372+
373+
return $testResponse;
369374
}
370375

371376
/**
@@ -378,7 +383,11 @@ public function get($uri, array $headers = [])
378383
*/
379384
public function getJson($uri, array $headers = [], $options = 0)
380385
{
381-
return $this->json('GET', $uri, [], $headers, $options);
386+
$testResponse = $this->json('GET', $uri, [], $headers, $options);
387+
388+
$this->resetDatabaseTransactionLevelToOne();
389+
390+
return $testResponse;
382391
}
383392

384393
/**
@@ -394,7 +403,11 @@ public function post($uri, array $data = [], array $headers = [])
394403
$server = $this->transformHeadersToServerVars($headers);
395404
$cookies = $this->prepareCookiesForRequest();
396405

397-
return $this->call('POST', $uri, $data, $cookies, [], $server);
406+
$testResponse = $this->call('POST', $uri, $data, $cookies, [], $server);
407+
408+
$this->resetDatabaseTransactionLevelToOne();
409+
410+
return $testResponse;
398411
}
399412

400413
/**
@@ -408,7 +421,11 @@ public function post($uri, array $data = [], array $headers = [])
408421
*/
409422
public function postJson($uri, array $data = [], array $headers = [], $options = 0)
410423
{
411-
return $this->json('POST', $uri, $data, $headers, $options);
424+
$testResponse = $this->json('POST', $uri, $data, $headers, $options);
425+
426+
$this->resetDatabaseTransactionLevelToOne();
427+
428+
return $testResponse;
412429
}
413430

414431
/**
@@ -424,7 +441,11 @@ public function put($uri, array $data = [], array $headers = [])
424441
$server = $this->transformHeadersToServerVars($headers);
425442
$cookies = $this->prepareCookiesForRequest();
426443

427-
return $this->call('PUT', $uri, $data, $cookies, [], $server);
444+
$testResponse = $this->call('PUT', $uri, $data, $cookies, [], $server);
445+
446+
$this->resetDatabaseTransactionLevelToOne();
447+
448+
return $testResponse;
428449
}
429450

430451
/**
@@ -438,7 +459,11 @@ public function put($uri, array $data = [], array $headers = [])
438459
*/
439460
public function putJson($uri, array $data = [], array $headers = [], $options = 0)
440461
{
441-
return $this->json('PUT', $uri, $data, $headers, $options);
462+
$testResponse = $this->json('PUT', $uri, $data, $headers, $options);
463+
464+
$this->resetDatabaseTransactionLevelToOne();
465+
466+
return $testResponse;
442467
}
443468

444469
/**
@@ -454,7 +479,11 @@ public function patch($uri, array $data = [], array $headers = [])
454479
$server = $this->transformHeadersToServerVars($headers);
455480
$cookies = $this->prepareCookiesForRequest();
456481

457-
return $this->call('PATCH', $uri, $data, $cookies, [], $server);
482+
$testResponse = $this->call('PATCH', $uri, $data, $cookies, [], $server);
483+
484+
$this->resetDatabaseTransactionLevelToOne();
485+
486+
return $testResponse;
458487
}
459488

460489
/**
@@ -468,7 +497,11 @@ public function patch($uri, array $data = [], array $headers = [])
468497
*/
469498
public function patchJson($uri, array $data = [], array $headers = [], $options = 0)
470499
{
471-
return $this->json('PATCH', $uri, $data, $headers, $options);
500+
$testResponse = $this->json('PATCH', $uri, $data, $headers, $options);
501+
502+
$this->resetDatabaseTransactionLevelToOne();
503+
504+
return $testResponse;
472505
}
473506

474507
/**
@@ -484,7 +517,11 @@ public function delete($uri, array $data = [], array $headers = [])
484517
$server = $this->transformHeadersToServerVars($headers);
485518
$cookies = $this->prepareCookiesForRequest();
486519

487-
return $this->call('DELETE', $uri, $data, $cookies, [], $server);
520+
$testResponse = $this->call('DELETE', $uri, $data, $cookies, [], $server);
521+
522+
$this->resetDatabaseTransactionLevelToOne();
523+
524+
return $testResponse;
488525
}
489526

490527
/**
@@ -498,7 +535,11 @@ public function delete($uri, array $data = [], array $headers = [])
498535
*/
499536
public function deleteJson($uri, array $data = [], array $headers = [], $options = 0)
500537
{
501-
return $this->json('DELETE', $uri, $data, $headers, $options);
538+
$testResponse = $this->json('DELETE', $uri, $data, $headers, $options);
539+
540+
$this->resetDatabaseTransactionLevelToOne();
541+
542+
return $testResponse;
502543
}
503544

504545
/**
@@ -515,7 +556,11 @@ public function options($uri, array $data = [], array $headers = [])
515556

516557
$cookies = $this->prepareCookiesForRequest();
517558

518-
return $this->call('OPTIONS', $uri, $data, $cookies, [], $server);
559+
$testResponse = $this->call('OPTIONS', $uri, $data, $cookies, [], $server);
560+
561+
$this->resetDatabaseTransactionLevelToOne();
562+
563+
return $testResponse;
519564
}
520565

521566
/**
@@ -529,7 +574,11 @@ public function options($uri, array $data = [], array $headers = [])
529574
*/
530575
public function optionsJson($uri, array $data = [], array $headers = [], $options = 0)
531576
{
532-
return $this->json('OPTIONS', $uri, $data, $headers, $options);
577+
$testResponse = $this->json('OPTIONS', $uri, $data, $headers, $options);
578+
579+
$this->resetDatabaseTransactionLevelToOne();
580+
581+
return $testResponse;
533582
}
534583

535584
/**
@@ -545,7 +594,11 @@ public function head($uri, array $headers = [])
545594

546595
$cookies = $this->prepareCookiesForRequest();
547596

548-
return $this->call('HEAD', $uri, [], $cookies, [], $server);
597+
$testResponse = $this->call('HEAD', $uri, [], $cookies, [], $server);
598+
599+
$this->resetDatabaseTransactionLevelToOne();
600+
601+
return $testResponse;
549602
}
550603

551604
/**
@@ -763,4 +816,23 @@ protected function createTestResponse($response, $request)
763816
);
764817
});
765818
}
819+
820+
protected function resetDatabaseTransactionLevelToOne()
821+
{
822+
$uses = $this->traitsUsedByTest ?? array_flip(class_uses_recursive(static::class));
823+
824+
if (! isset($uses[DatabaseTransactions::class])) {
825+
return;
826+
}
827+
828+
$databaseManager = $this->app['db'];
829+
830+
$connections = $this->connectionsToTransact();
831+
832+
foreach ($connections as $connectionName) {
833+
if ($databaseManager->connection($connectionName)->transactionLevel() > 1) {
834+
$databaseManager->connection($connectionName)->rollBack(1);
835+
}
836+
}
837+
}
766838
}

0 commit comments

Comments
 (0)