Skip to content

Commit bb561b0

Browse files
mateuszsipnorberttech
authored andcommitted
NotContains expander (#127)
* NotContains expander * More descritpive ContainsTest methods names * documented existence of notContains expander
1 parent dcded7d commit bb561b0

File tree

4 files changed

+130
-6
lines changed

4 files changed

+130
-6
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ $matcher->getError(); // returns null or error message
7373
* ``startsWith($stringBeginning, $ignoreCase = false)``
7474
* ``endsWith($stringEnding, $ignoreCase = false)``
7575
* ``contains($string, $ignoreCase = false)``
76+
* ``notContains($string, $ignoreCase = false)``
7677
* ``isDateTime()``
7778
* ``isEmail()``
7879
* ``isUrl()``
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Coduo\PHPMatcher\Matcher\Pattern\Expander;
6+
7+
use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander;
8+
use Coduo\ToString\StringConverter;
9+
10+
final class NotContains implements PatternExpander
11+
{
12+
const NAME = 'notContains';
13+
14+
private $error;
15+
16+
private $string;
17+
18+
private $ignoreCase;
19+
20+
public static function is(string $name) : bool
21+
{
22+
return self::NAME === $name;
23+
}
24+
25+
public function __construct(string $string, $ignoreCase = false)
26+
{
27+
$this->string = $string;
28+
$this->ignoreCase = $ignoreCase;
29+
}
30+
31+
public function match($value) : bool
32+
{
33+
if (!\is_string($value)) {
34+
$this->error = \sprintf('Not contains expander require "string", got "%s".', new StringConverter($value));
35+
return false;
36+
}
37+
38+
$contains = $this->ignoreCase
39+
? \mb_stripos($value, $this->string)
40+
: \mb_strpos($value, $this->string);
41+
42+
if ($contains !== false) {
43+
$this->error = \sprintf("String \"%s\" contains \"%s\".", $value, $this->string);
44+
45+
return false;
46+
}
47+
48+
return true;
49+
}
50+
51+
public function getError()
52+
{
53+
return $this->error;
54+
}
55+
}

tests/Matcher/Pattern/Expander/ContainsTest.php

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,15 @@
1010
class ContainsTest extends TestCase
1111
{
1212
/**
13-
* @dataProvider examplesIgnoreCaseProvider
13+
* @dataProvider examplesCaseSensitiveProvider
1414
*/
15-
public function test_matching_values_ignore_case($needle, $haystack, $expectedResult)
15+
public function test_matching_values_case_sensitive($needle, $haystack, $expectedResult)
1616
{
1717
$expander = new Contains($needle);
1818
$this->assertEquals($expectedResult, $expander->match($haystack));
1919
}
2020

21-
public static function examplesIgnoreCaseProvider()
21+
public static function examplesCaseSensitiveProvider()
2222
{
2323
return [
2424
['ipsum', 'lorem ipsum', true],
@@ -29,15 +29,15 @@ public static function examplesIgnoreCaseProvider()
2929
}
3030

3131
/**
32-
* @dataProvider examplesProvider
32+
* @dataProvider examplesCaseInsensitiveProvider
3333
*/
34-
public function test_matching_values($needle, $haystack, $expectedResult)
34+
public function test_matching_values_case_insensitive($needle, $haystack, $expectedResult)
3535
{
3636
$expander = new Contains($needle, true);
3737
$this->assertEquals($expectedResult, $expander->match($haystack));
3838
}
3939

40-
public static function examplesProvider()
40+
public static function examplesCaseInsensitiveProvider()
4141
{
4242
return [
4343
['IpSum', 'lorem ipsum', true],
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace Coduo\PHPMatcher\Tests\Matcher\Pattern\Expander;
6+
7+
use Coduo\PHPMatcher\Matcher\Pattern\Expander\Contains;
8+
use Coduo\PHPMatcher\Matcher\Pattern\Expander\NotContains;
9+
use PHPUnit\Framework\TestCase;
10+
11+
class NotContainsTest extends TestCase
12+
{
13+
/**
14+
* @dataProvider examplesCaseSensitiveProvider
15+
*/
16+
public function test_matching_values_case_sensitive($needle, $haystack, $expectedResult)
17+
{
18+
$expander = new NotContains($needle);
19+
$this->assertEquals($expectedResult, $expander->match($haystack));
20+
}
21+
22+
public static function examplesCaseSensitiveProvider()
23+
{
24+
return [
25+
['ipsum', 'lorem ipsum', false],
26+
['wor', 'this is my hello world string', false],
27+
['lol', 'lorem ipsum', true],
28+
['NO', 'norbert', true]
29+
];
30+
}
31+
32+
/**
33+
* @dataProvider examplesCaseInsensitiveProvider
34+
*/
35+
public function test_matching_values_case_insensitive($needle, $haystack, $expectedResult)
36+
{
37+
$expander = new NotContains($needle, true);
38+
$this->assertEquals($expectedResult, $expander->match($haystack));
39+
}
40+
41+
public static function examplesCaseInsensitiveProvider()
42+
{
43+
return [
44+
['IpSum', 'lorem ipsum', false],
45+
['wor', 'this is my hello WORLD string', false],
46+
['lol', 'LOREM ipsum', true],
47+
['NO', 'NORBERT', false]
48+
];
49+
}
50+
51+
/**
52+
* @dataProvider invalidCasesProvider
53+
*/
54+
public function test_error_when_matching_fail($string, $value, $errorMessage)
55+
{
56+
$expander = new NotContains($string);
57+
$this->assertFalse($expander->match($value));
58+
$this->assertEquals($errorMessage, $expander->getError());
59+
}
60+
61+
public static function invalidCasesProvider()
62+
{
63+
return [
64+
['ipsum', 'lorem ipsum', "String \"lorem ipsum\" contains \"ipsum\"."],
65+
['lorem', new \DateTime(), 'Not contains expander require "string", got "\\DateTime".'],
66+
];
67+
}
68+
}

0 commit comments

Comments
 (0)