-
Notifications
You must be signed in to change notification settings - Fork 11.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[8.x] Add assertNotSoftDeleted Method (#38886)
* Add assertNotSoftDelete Method * add test Co-authored-by: Taylor Otwell <taylorotwell@gmail.com>
- Loading branch information
1 parent
195529f
commit cb72e7b
Showing
3 changed files
with
206 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115 changes: 115 additions & 0 deletions
115
src/Illuminate/Testing/Constraints/NotSoftDeletedInDatabase.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
<?php | ||
|
||
namespace Illuminate\Testing\Constraints; | ||
|
||
use Illuminate\Database\Connection; | ||
use PHPUnit\Framework\Constraint\Constraint; | ||
|
||
class NotSoftDeletedInDatabase extends Constraint | ||
{ | ||
/** | ||
* Number of records that will be shown in the console in case of failure. | ||
* | ||
* @var int | ||
*/ | ||
protected $show = 3; | ||
|
||
/** | ||
* The database connection. | ||
* | ||
* @var \Illuminate\Database\Connection | ||
*/ | ||
protected $database; | ||
|
||
/** | ||
* The data that will be used to narrow the search in the database table. | ||
* | ||
* @var array | ||
*/ | ||
protected $data; | ||
|
||
/** | ||
* The name of the column that indicates soft deletion has occurred. | ||
* | ||
* @var string | ||
*/ | ||
protected $deletedAtColumn; | ||
|
||
/** | ||
* Create a new constraint instance. | ||
* | ||
* @param \Illuminate\Database\Connection $database | ||
* @param array $data | ||
* @param string $deletedAtColumn | ||
* @return void | ||
*/ | ||
public function __construct(Connection $database, array $data, string $deletedAtColumn) | ||
{ | ||
$this->database = $database; | ||
$this->data = $data; | ||
$this->deletedAtColumn = $deletedAtColumn; | ||
} | ||
|
||
/** | ||
* Check if the data is found in the given table. | ||
* | ||
* @param string $table | ||
* @return bool | ||
*/ | ||
public function matches($table): bool | ||
{ | ||
return $this->database->table($table) | ||
->where($this->data) | ||
->whereNull($this->deletedAtColumn) | ||
->count() > 0; | ||
} | ||
|
||
/** | ||
* Get the description of the failure. | ||
* | ||
* @param string $table | ||
* @return string | ||
*/ | ||
public function failureDescription($table): string | ||
{ | ||
return sprintf( | ||
"any existing row in the table [%s] matches the attributes %s.\n\n%s", | ||
$table, $this->toString(), $this->getAdditionalInfo($table) | ||
); | ||
} | ||
|
||
/** | ||
* Get additional info about the records found in the database table. | ||
* | ||
* @param string $table | ||
* @return string | ||
*/ | ||
protected function getAdditionalInfo($table) | ||
{ | ||
$query = $this->database->table($table); | ||
|
||
$results = $query->limit($this->show)->get(); | ||
|
||
if ($results->isEmpty()) { | ||
return 'The table is empty'; | ||
} | ||
|
||
$description = 'Found: '.json_encode($results, JSON_PRETTY_PRINT); | ||
|
||
if ($query->count() > $this->show) { | ||
$description .= sprintf(' and %s others', $query->count() - $this->show); | ||
} | ||
|
||
return $description; | ||
} | ||
|
||
/** | ||
* Get a string representation of the object. | ||
* | ||
* @return string | ||
*/ | ||
public function toString(): string | ||
{ | ||
return json_encode($this->data); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters