Passed
Push — master ( f8574f...0e1b55 )
by Julien
08:17 queued 05:08
created

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

195
        /** @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...
196 1
        $this->resetMessage();
197 1
        return $this;
198
    }
199
200 3
    public function ignoringCase(): self
201
    {
202 3
        $this->ignoreCase = true;
203 3
        return $this;
204
    }
205
206 2
    public function respectingCase(): self
207
    {
208 2
        $this->ignoreCase = false;
209 2
        return $this;
210
    }
211
}
212