Skip to content

Commit

Permalink
Feature: Add notStartsWith() & notEndsWith() (webmozarts#147) (webmoz…
Browse files Browse the repository at this point in the history
…arts#175)

* Assert::notStartsWith($value, $prefix, $message = '') (webmozarts#147)

    * Write expected tests for notStartsWith()

    * Implement notStartsWith()

    * Update README to include notStartsWith()

* Assert::notEndsWith($value, $prefix, $message = '') (webmozarts#147)

        * Write expected tests for notEndsWith()

        * Implement notEndsWith()

        * Update README to include notEndsWith()
  • Loading branch information
kevinlelo authored Apr 3, 2020
1 parent e32e7c7 commit a112873
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 0 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,10 @@ Method | Description
`contains($value, $subString, $message = '')` | Check that a string contains a substring
`notContains($value, $subString, $message = '')` | Check that a string does not contains a substring
`startsWith($value, $prefix, $message = '')` | Check that a string has a prefix
`notStartsWith($value, $prefix, $message = '')` | Check that a string does not have a prefix
`startsWithLetter($value, $message = '')` | Check that a string starts with a letter
`endsWith($value, $suffix, $message = '')` | Check that a string has a suffix
`notEndsWith($value, $suffix, $message = '')` | Check that a string does not have a suffix
`regex($value, $pattern, $message = '')` | Check that a string matches a regular expression
`notRegex($value, $pattern, $message = '')` | Check that a string does not match a regular expression
`unicodeLetters($value, $message = '')` | Check that a string contains Unicode letters only
Expand Down
44 changes: 44 additions & 0 deletions src/Assert.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@
* @method static void nullOrNotContains($value, $subString, $message = '')
* @method static void nullOrNotWhitespaceOnly($value, $message = '')
* @method static void nullOrStartsWith($value, $prefix, $message = '')
* @method static void nullOrNotStartsWith($value, $prefix, $message = '')
* @method static void nullOrStartsWithLetter($value, $message = '')
* @method static void nullOrEndsWith($value, $suffix, $message = '')
* @method static void nullOrNotEndsWith($value, $suffix, $message = '')
* @method static void nullOrRegex($value, $pattern, $message = '')
* @method static void nullOrNotRegex($value, $pattern, $message = '')
* @method static void nullOrUnicodeLetters($value, $message = '')
Expand Down Expand Up @@ -163,8 +165,10 @@
* @method static void allNotContains($values, $subString, $message = '')
* @method static void allNotWhitespaceOnly($values, $message = '')
* @method static void allStartsWith($values, $prefix, $message = '')
* @method static void allNotStartsWith($values, $prefix, $message = '')
* @method static void allStartsWithLetter($values, $message = '')
* @method static void allEndsWith($values, $suffix, $message = '')
* @method static void allNotEndsWith($values, $suffix, $message = '')
* @method static void allRegex($values, $pattern, $message = '')
* @method static void allNotRegex($values, $pattern, $message = '')
* @method static void allUnicodeLetters($values, $message = '')
Expand Down Expand Up @@ -1199,6 +1203,26 @@ public static function startsWith($value, $prefix, $message = '')
}
}

/**
* @psalm-pure
*
* @param string $value
* @param string $prefix
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function notStartsWith($value, $prefix, $message = '')
{
if (0 === \strpos($value, $prefix)) {
static::reportInvalidArgument(\sprintf(
$message ?: 'Expected a value not to start with %2$s. Got: %s',
static::valueToString($value),
static::valueToString($prefix)
));
}
}

/**
* @psalm-pure
*
Expand Down Expand Up @@ -1248,6 +1272,26 @@ public static function endsWith($value, $suffix, $message = '')
}
}

/**
* @psalm-pure
*
* @param string $value
* @param string $suffix
* @param string $message
*
* @throws InvalidArgumentException
*/
public static function notEndsWith($value, $suffix, $message = '')
{
if ($suffix === \substr($value, -\strlen($suffix))) {
static::reportInvalidArgument(\sprintf(
$message ?: 'Expected a value not to end with %2$s. Got: %s',
static::valueToString($value),
static::valueToString($suffix)
));
}
}

/**
* @psalm-pure
*
Expand Down
24 changes: 24 additions & 0 deletions tests/AssertTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -295,6 +295,18 @@ public function getTests()
array('startsWithLetter', array(''), false),
array('startsWithLetter', array(null), false),
array('startsWithLetter', array(66), false),
array('notStartsWith', array('abcd', 'ab'), false),
array('notStartsWith', array('abcd', 'bc'), true),
array('notStartsWith', array('', 'bc'), true),
array('notStartsWith', array('äþçð', 'äþ'), false),
array('notStartsWith', array('äþçð', 'þç'), true),
array('notStartsWith', array('', 'þç'), true),
array('notStartsWith', array('あいうえ', 'あい'), false),
array('notStartsWith', array('あいうえ', 'いう'), true),
array('notStartsWith', array('', 'いう'), true),
array('notStartsWith', array('😄😑☹️', '😄'), false),
array('notStartsWith', array('😄😑☹️', '😑'), true),
array('notStartsWith', array('', '😑'), true),
array('endsWith', array('abcd', 'cd'), true),
array('endsWith', array('abcd', 'bc'), false),
array('endsWith', array('', 'bc'), false),
Expand All @@ -307,6 +319,18 @@ public function getTests()
array('endsWith', array('😄😑☹️', '☹️'), true),
array('endsWith', array('😄😑☹️', '😑'), false),
array('endsWith', array('', '😑'), false),
array('notEndsWith', array('abcd', 'cd'), false),
array('notEndsWith', array('abcd', 'bc'), true),
array('notEndsWith', array('', 'bc'), true),
array('notEndsWith', array('äþçð', 'çð'), false),
array('notEndsWith', array('äþçð', 'þç'), true),
array('notEndsWith', array('', 'þç'), true),
array('notEndsWith', array('あいうえ', 'うえ'), false),
array('notEndsWith', array('あいうえ', 'いう'), true),
array('notEndsWith', array('', 'いう'), true),
array('notEndsWith', array('😄😑☹️', '☹️'), false),
array('notEndsWith', array('😄😑☹️', '😑'), true),
array('notEndsWith', array('', '😑'), true),
array('regex', array('abcd', '~^ab~'), true),
array('regex', array('abcd', '~^bc~'), false),
array('regex', array('', '~^bc~'), false),
Expand Down

0 comments on commit a112873

Please sign in to comment.