Skip to content

NotContains expander #127

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 15, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ $matcher->getError(); // returns null or error message
* ``startsWith($stringBeginning, $ignoreCase = false)``
* ``endsWith($stringEnding, $ignoreCase = false)``
* ``contains($string, $ignoreCase = false)``
* ``notContains($string, $ignoreCase = false)``
* ``isDateTime()``
* ``isEmail()``
* ``isUrl()``
Expand Down
55 changes: 55 additions & 0 deletions src/Matcher/Pattern/Expander/NotContains.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<?php

declare(strict_types=1);

namespace Coduo\PHPMatcher\Matcher\Pattern\Expander;

use Coduo\PHPMatcher\Matcher\Pattern\PatternExpander;
use Coduo\ToString\StringConverter;

final class NotContains implements PatternExpander
{
const NAME = 'notContains';

private $error;

private $string;

private $ignoreCase;

public static function is(string $name) : bool
{
return self::NAME === $name;
}

public function __construct(string $string, $ignoreCase = false)
{
$this->string = $string;
$this->ignoreCase = $ignoreCase;
}

public function match($value) : bool
{
if (!\is_string($value)) {
$this->error = \sprintf('Not contains expander require "string", got "%s".', new StringConverter($value));
return false;
}

$contains = $this->ignoreCase
? \mb_stripos($value, $this->string)
: \mb_strpos($value, $this->string);

if ($contains !== false) {
$this->error = \sprintf("String \"%s\" contains \"%s\".", $value, $this->string);

return false;
}

return true;
}

public function getError()
{
return $this->error;
}
}
12 changes: 6 additions & 6 deletions tests/Matcher/Pattern/Expander/ContainsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@
class ContainsTest extends TestCase
{
/**
* @dataProvider examplesIgnoreCaseProvider
* @dataProvider examplesCaseSensitiveProvider
*/
public function test_matching_values_ignore_case($needle, $haystack, $expectedResult)
public function test_matching_values_case_sensitive($needle, $haystack, $expectedResult)
{
$expander = new Contains($needle);
$this->assertEquals($expectedResult, $expander->match($haystack));
}

public static function examplesIgnoreCaseProvider()
public static function examplesCaseSensitiveProvider()
{
return [
['ipsum', 'lorem ipsum', true],
Expand All @@ -29,15 +29,15 @@ public static function examplesIgnoreCaseProvider()
}

/**
* @dataProvider examplesProvider
* @dataProvider examplesCaseInsensitiveProvider
*/
public function test_matching_values($needle, $haystack, $expectedResult)
public function test_matching_values_case_insensitive($needle, $haystack, $expectedResult)
{
$expander = new Contains($needle, true);
$this->assertEquals($expectedResult, $expander->match($haystack));
}

public static function examplesProvider()
public static function examplesCaseInsensitiveProvider()
{
return [
['IpSum', 'lorem ipsum', true],
Expand Down
68 changes: 68 additions & 0 deletions tests/Matcher/Pattern/Expander/NotContainsTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php

declare(strict_types=1);

namespace Coduo\PHPMatcher\Tests\Matcher\Pattern\Expander;

use Coduo\PHPMatcher\Matcher\Pattern\Expander\Contains;
use Coduo\PHPMatcher\Matcher\Pattern\Expander\NotContains;
use PHPUnit\Framework\TestCase;

class NotContainsTest extends TestCase
{
/**
* @dataProvider examplesCaseSensitiveProvider
*/
public function test_matching_values_case_sensitive($needle, $haystack, $expectedResult)
{
$expander = new NotContains($needle);
$this->assertEquals($expectedResult, $expander->match($haystack));
}

public static function examplesCaseSensitiveProvider()
{
return [
['ipsum', 'lorem ipsum', false],
['wor', 'this is my hello world string', false],
['lol', 'lorem ipsum', true],
['NO', 'norbert', true]
];
}

/**
* @dataProvider examplesCaseInsensitiveProvider
*/
public function test_matching_values_case_insensitive($needle, $haystack, $expectedResult)
{
$expander = new NotContains($needle, true);
$this->assertEquals($expectedResult, $expander->match($haystack));
}

public static function examplesCaseInsensitiveProvider()
{
return [
['IpSum', 'lorem ipsum', false],
['wor', 'this is my hello WORLD string', false],
['lol', 'LOREM ipsum', true],
['NO', 'NORBERT', false]
];
}

/**
* @dataProvider invalidCasesProvider
*/
public function test_error_when_matching_fail($string, $value, $errorMessage)
{
$expander = new NotContains($string);
$this->assertFalse($expander->match($value));
$this->assertEquals($errorMessage, $expander->getError());
}

public static function invalidCasesProvider()
{
return [
['ipsum', 'lorem ipsum', "String \"lorem ipsum\" contains \"ipsum\"."],
['lorem', new \DateTime(), 'Not contains expander require "string", got "\\DateTime".'],
];
}
}