|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Illuminate\Foundation\Testing\Constraints; |
| 4 | + |
| 5 | +use PHPUnit_Framework_Constraint; |
| 6 | +use Illuminate\Database\Connection; |
| 7 | + |
| 8 | +class SoftDeletedInDatabase extends PHPUnit_Framework_Constraint |
| 9 | +{ |
| 10 | + /** |
| 11 | + * Number of records that will be shown in the console in case of failure. |
| 12 | + * |
| 13 | + * @var int |
| 14 | + */ |
| 15 | + protected $show = 3; |
| 16 | + |
| 17 | + /** |
| 18 | + * The database connection. |
| 19 | + * |
| 20 | + * @var \Illuminate\Database\Connection |
| 21 | + */ |
| 22 | + protected $database; |
| 23 | + |
| 24 | + /** |
| 25 | + * The data that will be used to narrow the search in the database table. |
| 26 | + * |
| 27 | + * @var array |
| 28 | + */ |
| 29 | + protected $data; |
| 30 | + |
| 31 | + /** |
| 32 | + * Create a new constraint instance. |
| 33 | + * |
| 34 | + * @param \Illuminate\Database\Connection $database |
| 35 | + * @param array $data |
| 36 | + * @return void |
| 37 | + */ |
| 38 | + public function __construct(Connection $database, array $data) |
| 39 | + { |
| 40 | + $this->data = $data; |
| 41 | + |
| 42 | + $this->database = $database; |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Check if the data is found in the given table. |
| 47 | + * |
| 48 | + * @param string $table |
| 49 | + * @return bool |
| 50 | + */ |
| 51 | + public function matches($table) |
| 52 | + { |
| 53 | + return $this->database->table($table) |
| 54 | + ->where($this->data)->whereNotNull('deleted_at')->count() > 0; |
| 55 | + } |
| 56 | + |
| 57 | + /** |
| 58 | + * Get the description of the failure. |
| 59 | + * |
| 60 | + * @param string $table |
| 61 | + * @return string |
| 62 | + */ |
| 63 | + public function failureDescription($table) |
| 64 | + { |
| 65 | + return sprintf( |
| 66 | + "any soft deleted row in the table [%s] matches the attributes %s.\n\n%s", |
| 67 | + $table, $this->toString(), $this->getAdditionalInfo($table) |
| 68 | + ); |
| 69 | + } |
| 70 | + |
| 71 | + /** |
| 72 | + * Get additional info about the records found in the database table. |
| 73 | + * |
| 74 | + * @param string $table |
| 75 | + * @return string |
| 76 | + */ |
| 77 | + protected function getAdditionalInfo($table) |
| 78 | + { |
| 79 | + $results = $this->database->table($table)->get(); |
| 80 | + |
| 81 | + if ($results->isEmpty()) { |
| 82 | + return 'The table is empty'; |
| 83 | + } |
| 84 | + |
| 85 | + $description = 'Found: '.json_encode($results->take($this->show), JSON_PRETTY_PRINT); |
| 86 | + |
| 87 | + if ($results->count() > $this->show) { |
| 88 | + $description .= sprintf(' and %s others', $results->count() - $this->show); |
| 89 | + } |
| 90 | + |
| 91 | + return $description; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * Get a string representation of the object. |
| 96 | + * |
| 97 | + * @return string |
| 98 | + */ |
| 99 | + public function toString() |
| 100 | + { |
| 101 | + return json_encode($this->data); |
| 102 | + } |
| 103 | +} |
0 commit comments