Subversion-Projekte lars-tiefland.laravel_shop

Revision

Details | Letzte Änderung | Log anzeigen | RSS feed

Revision Autor Zeilennr. Zeile
148 lars 1
<?php
2
 
3
namespace Illuminate\Testing\Constraints;
4
 
5
use Illuminate\Database\Connection;
6
use PHPUnit\Framework\Constraint\Constraint;
7
 
8
class SoftDeletedInDatabase extends 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
     * The name of the column that indicates soft deletion has occurred.
33
     *
34
     * @var string
35
     */
36
    protected $deletedAtColumn;
37
 
38
    /**
39
     * Create a new constraint instance.
40
     *
41
     * @param  \Illuminate\Database\Connection  $database
42
     * @param  array  $data
43
     * @param  string  $deletedAtColumn
44
     * @return void
45
     */
46
    public function __construct(Connection $database, array $data, string $deletedAtColumn)
47
    {
48
        $this->data = $data;
49
 
50
        $this->database = $database;
51
 
52
        $this->deletedAtColumn = $deletedAtColumn;
53
    }
54
 
55
    /**
56
     * Check if the data is found in the given table.
57
     *
58
     * @param  string  $table
59
     * @return bool
60
     */
61
    public function matches($table): bool
62
    {
63
        return $this->database->table($table)
64
                ->where($this->data)
65
                ->whereNotNull($this->deletedAtColumn)
66
                ->count() > 0;
67
    }
68
 
69
    /**
70
     * Get the description of the failure.
71
     *
72
     * @param  string  $table
73
     * @return string
74
     */
75
    public function failureDescription($table): string
76
    {
77
        return sprintf(
78
            "any soft deleted row in the table [%s] matches the attributes %s.\n\n%s",
79
            $table, $this->toString(), $this->getAdditionalInfo($table)
80
        );
81
    }
82
 
83
    /**
84
     * Get additional info about the records found in the database table.
85
     *
86
     * @param  string  $table
87
     * @return string
88
     */
89
    protected function getAdditionalInfo($table)
90
    {
91
        $query = $this->database->table($table);
92
 
93
        $results = $query->limit($this->show)->get();
94
 
95
        if ($results->isEmpty()) {
96
            return 'The table is empty';
97
        }
98
 
99
        $description = 'Found: '.json_encode($results, JSON_PRETTY_PRINT);
100
 
101
        if ($query->count() > $this->show) {
102
            $description .= sprintf(' and %s others', $query->count() - $this->show);
103
        }
104
 
105
        return $description;
106
    }
107
 
108
    /**
109
     * Get a string representation of the object.
110
     *
111
     * @return string
112
     */
113
    public function toString(): string
114
    {
115
        return json_encode($this->data);
116
    }
117
}