Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 85 additions & 13 deletions src/Illuminate/Foundation/Testing/Concerns/MakesHttpRequests.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
use BackedEnum;
use Illuminate\Contracts\Http\Kernel as HttpKernel;
use Illuminate\Cookie\CookieValuePrefix;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Http\Request;
use Illuminate\Support\Collection;
use Illuminate\Support\Uri;
Expand Down Expand Up @@ -365,7 +366,11 @@ public function get($uri, array $headers = [])
$server = $this->transformHeadersToServerVars($headers);
$cookies = $this->prepareCookiesForRequest();

return $this->call('GET', $uri, [], $cookies, [], $server);
$testResponse = $this->call('GET', $uri, [], $cookies, [], $server);

$this->resetDatabaseTransactionLevelToOne();

return $testResponse;
}

/**
Expand All @@ -378,7 +383,11 @@ public function get($uri, array $headers = [])
*/
public function getJson($uri, array $headers = [], $options = 0)
{
return $this->json('GET', $uri, [], $headers, $options);
$testResponse = $this->json('GET', $uri, [], $headers, $options);

$this->resetDatabaseTransactionLevelToOne();

return $testResponse;
}

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

return $this->call('POST', $uri, $data, $cookies, [], $server);
$testResponse = $this->call('POST', $uri, $data, $cookies, [], $server);

$this->resetDatabaseTransactionLevelToOne();

return $testResponse;
}

/**
Expand All @@ -408,7 +421,11 @@ public function post($uri, array $data = [], array $headers = [])
*/
public function postJson($uri, array $data = [], array $headers = [], $options = 0)
{
return $this->json('POST', $uri, $data, $headers, $options);
$testResponse = $this->json('POST', $uri, $data, $headers, $options);

$this->resetDatabaseTransactionLevelToOne();

return $testResponse;
}

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

return $this->call('PUT', $uri, $data, $cookies, [], $server);
$testResponse = $this->call('PUT', $uri, $data, $cookies, [], $server);

$this->resetDatabaseTransactionLevelToOne();

return $testResponse;
}

/**
Expand All @@ -438,7 +459,11 @@ public function put($uri, array $data = [], array $headers = [])
*/
public function putJson($uri, array $data = [], array $headers = [], $options = 0)
{
return $this->json('PUT', $uri, $data, $headers, $options);
$testResponse = $this->json('PUT', $uri, $data, $headers, $options);

$this->resetDatabaseTransactionLevelToOne();

return $testResponse;
}

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

return $this->call('PATCH', $uri, $data, $cookies, [], $server);
$testResponse = $this->call('PATCH', $uri, $data, $cookies, [], $server);

$this->resetDatabaseTransactionLevelToOne();

return $testResponse;
}

/**
Expand All @@ -468,7 +497,11 @@ public function patch($uri, array $data = [], array $headers = [])
*/
public function patchJson($uri, array $data = [], array $headers = [], $options = 0)
{
return $this->json('PATCH', $uri, $data, $headers, $options);
$testResponse = $this->json('PATCH', $uri, $data, $headers, $options);

$this->resetDatabaseTransactionLevelToOne();

return $testResponse;
}

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

return $this->call('DELETE', $uri, $data, $cookies, [], $server);
$testResponse = $this->call('DELETE', $uri, $data, $cookies, [], $server);

$this->resetDatabaseTransactionLevelToOne();

return $testResponse;
}

/**
Expand All @@ -498,7 +535,11 @@ public function delete($uri, array $data = [], array $headers = [])
*/
public function deleteJson($uri, array $data = [], array $headers = [], $options = 0)
{
return $this->json('DELETE', $uri, $data, $headers, $options);
$testResponse = $this->json('DELETE', $uri, $data, $headers, $options);

$this->resetDatabaseTransactionLevelToOne();

return $testResponse;
}

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

$cookies = $this->prepareCookiesForRequest();

return $this->call('OPTIONS', $uri, $data, $cookies, [], $server);
$testResponse = $this->call('OPTIONS', $uri, $data, $cookies, [], $server);

$this->resetDatabaseTransactionLevelToOne();

return $testResponse;
}

/**
Expand All @@ -529,7 +574,11 @@ public function options($uri, array $data = [], array $headers = [])
*/
public function optionsJson($uri, array $data = [], array $headers = [], $options = 0)
{
return $this->json('OPTIONS', $uri, $data, $headers, $options);
$testResponse = $this->json('OPTIONS', $uri, $data, $headers, $options);

$this->resetDatabaseTransactionLevelToOne();

return $testResponse;
}

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

$cookies = $this->prepareCookiesForRequest();

return $this->call('HEAD', $uri, [], $cookies, [], $server);
$testResponse = $this->call('HEAD', $uri, [], $cookies, [], $server);

$this->resetDatabaseTransactionLevelToOne();

return $testResponse;
}

/**
Expand Down Expand Up @@ -763,4 +816,23 @@ protected function createTestResponse($response, $request)
);
});
}

protected function resetDatabaseTransactionLevelToOne()
{
$uses = $this->traitsUsedByTest ?? array_flip(class_uses_recursive(static::class));

if (! isset($uses[DatabaseTransactions::class])) {
return;
}

$databaseManager = $this->app['db'];

$connections = $this->connectionsToTransact();

foreach ($connections as $connectionName) {
if ($databaseManager->connection($connectionName)->transactionLevel() > 1) {
$databaseManager->connection($connectionName)->rollBack(1);
}
}
}
}
Loading
Loading