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 NotSoftDeletedInDatabase 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->database = $database;
49
        $this->data = $data;
50
        $this->deletedAtColumn = $deletedAtColumn;
51
    }
52
 
53
    /**
54
     * Check if the data is found in the given table.
55
     *
56
     * @param  string  $table
57
     * @return bool
58
     */
59
    public function matches($table): bool
60
    {
61
        return $this->database->table($table)
62
                ->where($this->data)
63
                ->whereNull($this->deletedAtColumn)
64
                ->count() > 0;
65
    }
66
 
67
    /**
68
     * Get the description of the failure.
69
     *
70
     * @param  string  $table
71
     * @return string
72
     */
73
    public function failureDescription($table): string
74
    {
75
        return sprintf(
76
            "any existing row in the table [%s] matches the attributes %s.\n\n%s",
77
            $table, $this->toString(), $this->getAdditionalInfo($table)
78
        );
79
    }
80
 
81
    /**
82
     * Get additional info about the records found in the database table.
83
     *
84
     * @param  string  $table
85
     * @return string
86
     */
87
    protected function getAdditionalInfo($table)
88
    {
89
        $query = $this->database->table($table);
90
 
91
        $results = $query->limit($this->show)->get();
92
 
93
        if ($results->isEmpty()) {
94
            return 'The table is empty';
95
        }
96
 
97
        $description = 'Found: '.json_encode($results, JSON_PRETTY_PRINT);
98
 
99
        if ($query->count() > $this->show) {
100
            $description .= sprintf(' and %s others', $query->count() - $this->show);
101
        }
102
 
103
        return $description;
104
    }
105
 
106
    /**
107
     * Get a string representation of the object.
108
     *
109
     * @return string
110
     */
111
    public function toString(): string
112
    {
113
        return json_encode($this->data);
114
    }
115
}