Passed
Push — master ( de7aef...b2203e )
by Julien
02:53
created

StringCheck::isEmpty()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 4
CRAP Score 1

Importance

Changes 2
Bugs 0 Features 0
Metric Value
eloc 3
c 2
b 0
f 0
dl 0
loc 5
ccs 4
cts 4
cp 1
rs 10
cc 1
nc 1
nop 0
crap 1
1
<?php declare(strict_types=1);
2
3
4
namespace Pitchart\Phlunit\Checks;
5
6
use PHPUnit\Framework\Assert;
7
use PHPUnit\Framework\Constraint\LogicalNot;
8
use Pitchart\Phlunit\Checks\Converter\ToDateTime;
9
use Pitchart\Phlunit\Checks\Converter\ToInteger;
10
use Pitchart\Phlunit\Checks\Converter\ToJson;
11
use Pitchart\Phlunit\Checks\Mixin\ConstraintCheck;
12
use Pitchart\Phlunit\Checks\Mixin\TypeCheck;
13
use Pitchart\Phlunit\Checks\Mixin\WithMessage;
14
use Pitchart\Phlunit\Constraint\String\EndsWith;
15
use Pitchart\Phlunit\Constraint\String\IsBlank;
16
use Pitchart\Phlunit\Constraint\String\IsDigits;
17
use Pitchart\Phlunit\Constraint\String\IsHexadecimal;
18
use Pitchart\Phlunit\Constraint\String\IsLetters;
19
use Pitchart\Phlunit\Constraint\String\StartsWith;
20
21
/**
22
 * Class StringCheck
23
 *
24
 * @package Pitchart\Phlunit\Checks
25
 *
26
 * @author Julien VITTE <[email protected]>
27
 *
28
 * @template-implements FluentCheck<string>
29
 */
30
class StringCheck implements FluentCheck
31
{
32 1
    use TypeCheck, WithMessage, ConstraintCheck,
33
        ToDateTime, ToInteger, ToJson;
34
35
    /**
36
     * @var string
37
     */
38
    private $value;
39
40
    /**
41
     * @var bool
42
     */
43
    private $ignoreCase = false;
44
45
    /**
46
     * GenericCheck constructor.
47
     *
48
     * @param $value
49
     */
50 45
    public function __construct(string $value)
51
    {
52 45
        $this->value = $value;
53 45
    }
54
55 3
    public function isEqualTo(string $expected): self
56
    {
57 3
        if ($this->ignoreCase) {
58 1
            Assert::assertEqualsIgnoringCase($expected, $this->value, $this->message);
59
        } else {
60 2
            Assert::assertSame($expected, $this->value, $this->message);
61
        }
62 3
        $this->resetMessage();
63 3
        return $this;
64
    }
65
66 2
    public function isNotEqualTo(string $expected): self
67
    {
68 2
        if ($this->ignoreCase) {
69
            Assert::assertNotEqualsIgnoringCase($expected, $this->value, $this->message);
70
        } else {
71 2
            Assert::assertNotEquals($expected, $this->value, $this->message);
72
        }
73 2
        $this->resetMessage();
74 2
        return $this;
75
    }
76
77 2
    public function isEmpty(): self
78
    {
79 2
        Assert::assertEmpty($this->value, $this->message);
80 2
        $this->resetMessage();
81 2
        return $this;
82
    }
83
84 2
    public function isNotEmpty(): self
85
    {
86 2
        Assert::assertNotEmpty($this->value, $this->message);
87 2
        $this->resetMessage();
88 2
        return $this;
89
    }
90
91 4
    public function isBlank(): self
92
    {
93 4
        Assert::assertThat($this->value, new IsBlank(), $this->message);
94 4
        $this->resetMessage();
95 4
        return $this;
96
    }
97
98 2
    public function isNotBlank(): self
99
    {
100 2
        Assert::assertThat($this->value, new LogicalNot(new IsBlank()), $this->message);
101 2
        $this->resetMessage();
102 2
        return $this;
103
    }
104
105 2
    public function isGreaterThan(string $expected): self
106
    {
107 2
        Assert::assertGreaterThan($expected, $this->value, $this->message);
108 2
        $this->resetMessage();
109 2
        return $this;
110
    }
111
112 2
    public function isGreaterThanOrEqualTo(string $expected): self
113
    {
114 2
        Assert::assertGreaterThanOrEqual($expected, $this->value, $this->message);
115 2
        $this->resetMessage();
116 2
        return $this;
117
    }
118
119 2
    public function isLessThan(string $expected): self
120
    {
121 2
        Assert::assertLessThan($expected, $this->value, $this->message);
122 2
        $this->resetMessage();
123 2
        return $this;
124
    }
125
126 2
    public function isLessThanOrEqualTo(string $expected): self
127
    {
128 2
        Assert::assertLessThanOrEqual($expected, $this->value, $this->message);
129 2
        $this->resetMessage();
130 2
        return $this;
131
    }
132
133 3
    public function contains(string $expected): self
134
    {
135 3
        if ($this->ignoreCase) {
136 1
            Assert::assertStringContainsStringIgnoringCase($expected, $this->value, $this->message);
137
        } else {
138 2
            Assert::assertStringContainsString($expected, $this->value, $this->message);
139
        }
140 3
        $this->resetMessage();
141 3
        return $this;
142
    }
143
144 4
    public function startsWith(string $expected): self
145
    {
146 4
        if ($this->ignoreCase) {
147 2
            Assert::assertThat($this->value, new StartsWith($expected), $this->message);
148
        } else {
149 3
            Assert::assertStringStartsWith($expected, $this->value, $this->message);
150
        }
151 4
        $this->resetMessage();
152 4
        return $this;
153
    }
154
155 3
    public function endsWith(string $expected): self
156
    {
157 3
        if ($this->ignoreCase) {
158 1
            Assert::assertThat($this->value, new EndsWith($expected), $this->message);
159
        } else {
160 2
            Assert::assertStringEndsWith($expected, $this->value, $this->message);
161
        }
162 3
        $this->resetMessage();
163 3
        return $this;
164
    }
165
166 2
    public function isDigits(): self
167
    {
168 2
        Assert::assertThat($this->value, new IsDigits(), $this->message);
169 2
        $this->resetMessage();
170 2
        return $this;
171
    }
172
173 2
    public function isLetters(): self
174
    {
175 2
        Assert::assertThat($this->value, new IsLetters(), $this->message);
176 2
        $this->resetMessage();
177 2
        return $this;
178
    }
179
180 2
    public function isWhitespaces(): self
181
    {
182 2
        return $this->isBlank();
183
    }
184
185 1
    public function isHexadecimal(): self
186
    {
187 1
        Assert::assertThat($this->value, new IsHexadecimal(), $this->message);
188 1
        $this->resetMessage();
189 1
        return $this;
190
    }
191
192 1
    public function matches(string $pattern): self
193
    {
194 1
        Assert::assertRegExp($pattern, $this->value, $this->message);
0 ignored issues
show
Deprecated Code introduced by
The function PHPUnit\Framework\Assert::assertRegExp() has been deprecated: https://github.com/sebastianbergmann/phpunit/issues/4086 ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-deprecated  annotation

194
        /** @scrutinizer ignore-deprecated */ Assert::assertRegExp($pattern, $this->value, $this->message);

This function has been deprecated. The supplier of the function has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the function will be removed and what other function to use instead.

Loading history...
195 1
        $this->resetMessage();
196 1
        return $this;
197
    }
198
199 3
    public function ignoringCase(): self
200
    {
201 3
        $this->ignoreCase = true;
202 3
        return $this;
203
    }
204
205 2
    public function respectingCase(): self
206
    {
207 2
        $this->ignoreCase = false;
208 2
        return $this;
209
    }
210
}
211