RepositoryAssertions::countLessThanOrEqual()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 1
Metric Value
eloc 3
c 1
b 0
f 1
dl 0
loc 7
rs 10
cc 1
nc 1
nop 2
1
<?php
2
3
namespace Zenstruck\Foundry;
4
5
use Zenstruck\Assert;
6
7
/**
8
 * @author Kevin Bond <[email protected]>
9
 */
10
final class RepositoryAssertions
11
{
12
    private $repository;
13
14
    public function __construct(RepositoryProxy $repository)
15
    {
16
        $this->repository = $repository;
17
    }
18
19
    public function empty(string $message = 'Expected {entity} repository to be empty but it has {actual} items.'): self
20
    {
21
        return $this->count(0, $message);
22
    }
23
24
    public function count(int $expectedCount, string $message = 'Expected count of {entity} repository ({actual}) to be {expected}.'): self
25
    {
26
        Assert::that($this->repository)
27
            ->hasCount($expectedCount, $message, ['entity' => $this->repository->getClassName()])
28
        ;
29
30
        return $this;
31
    }
32
33
    public function countGreaterThan(int $expected, string $message = 'Expected count of {entity} repository ({actual}) to be greater than {expected}.'): self
34
    {
35
        Assert::that($this->repository->count())
36
            ->isGreaterThan($expected, $message, ['entity' => $this->repository->getClassName()])
37
        ;
38
39
        return $this;
40
    }
41
42
    public function countGreaterThanOrEqual(int $expected, string $message = 'Expected count of {entity} repository ({actual}) to be greater than or equal to {expected}.'): self
43
    {
44
        Assert::that($this->repository->count())
45
            ->isGreaterThanOrEqualTo($expected, $message, ['entity' => $this->repository->getClassName()])
46
        ;
47
48
        return $this;
49
    }
50
51
    public function countLessThan(int $expected, string $message = 'Expected count of {entity} repository ({actual}) to be less than {expected}.'): self
52
    {
53
        Assert::that($this->repository->count())
54
            ->isLessThan($expected, $message, ['entity' => $this->repository->getClassName()])
55
        ;
56
57
        return $this;
58
    }
59
60
    public function countLessThanOrEqual(int $expected, string $message = 'Expected count of {entity} repository ({actual}) to be less than or equal to {expected}.'): self
61
    {
62
        Assert::that($this->repository->count())
63
            ->isLessThanOrEqualTo($expected, $message, ['entity' => $this->repository->getClassName()])
64
        ;
65
66
        return $this;
67
    }
68
69
    /**
70
     * @param object|array|mixed $criteria
71
     */
72
    public function exists($criteria, string $message = 'Expected {entity} to exist but it does not.'): self
73
    {
74
        Assert::that($this->repository->find($criteria))->isNotEmpty($message, [
75
            'entity' => $this->repository->getClassName(),
76
            'criteria' => $criteria,
77
        ]);
78
79
        return $this;
80
    }
81
82
    /**
83
     * @param object|array|mixed $criteria
84
     */
85
    public function notExists($criteria, string $message = 'Expected {entity} to not exist but it does.'): self
86
    {
87
        Assert::that($this->repository->find($criteria))->isEmpty($message, [
88
            'entity' => $this->repository->getClassName(),
89
            'criteria' => $criteria,
90
        ]);
91
92
        return $this;
93
    }
94
}
95