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
use ReflectionClass;
8
 
9
class CountInDatabase extends Constraint
10
{
11
    /**
12
     * The database connection.
13
     *
14
     * @var \Illuminate\Database\Connection
15
     */
16
    protected $database;
17
 
18
    /**
19
     * The expected table entries count that will be checked against the actual count.
20
     *
21
     * @var int
22
     */
23
    protected $expectedCount;
24
 
25
    /**
26
     * The actual table entries count that will be checked against the expected count.
27
     *
28
     * @var int
29
     */
30
    protected $actualCount;
31
 
32
    /**
33
     * Create a new constraint instance.
34
     *
35
     * @param  \Illuminate\Database\Connection  $database
36
     * @param  int  $expectedCount
37
     * @return void
38
     */
39
    public function __construct(Connection $database, int $expectedCount)
40
    {
41
        $this->expectedCount = $expectedCount;
42
 
43
        $this->database = $database;
44
    }
45
 
46
    /**
47
     * Check if the expected and actual count are equal.
48
     *
49
     * @param  string  $table
50
     * @return bool
51
     */
52
    public function matches($table): bool
53
    {
54
        $this->actualCount = $this->database->table($table)->count();
55
 
56
        return $this->actualCount === $this->expectedCount;
57
    }
58
 
59
    /**
60
     * Get the description of the failure.
61
     *
62
     * @param  string  $table
63
     * @return string
64
     */
65
    public function failureDescription($table): string
66
    {
67
        return sprintf(
68
            "table [%s] matches expected entries count of %s. Entries found: %s.\n",
69
            $table, $this->expectedCount, $this->actualCount
70
        );
71
    }
72
 
73
    /**
74
     * Get a string representation of the object.
75
     *
76
     * @param  int  $options
77
     * @return string
78
     */
79
    public function toString($options = 0): string
80
    {
81
        return (new ReflectionClass($this))->name;
82
    }
83
}