Last active
March 28, 2018 13:07
-
-
Save svenluijten/b43fbbd77b82ffd8860a7d0c0d69592d to your computer and use it in GitHub Desktop.
extra assertions against arrays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace Tests; | |
use Illuminate\Support\Str; | |
use PHPUnit\Framework\TestCase as BaseTestCase; | |
class TestCase extends BaseTestCase | |
{ | |
/** | |
* Assert that any of the values in the array contains something. | |
* | |
* @param string|int $needle | |
* @param array $haystack | |
* @param string $msg | |
* | |
* @return void | |
*/ | |
public static function assertArrayContains($needle, array $haystack, string $msg = '') | |
{ | |
if ($msg === '') { | |
$msg = "Expected any value in the array to contain '$needle'."; | |
} | |
foreach ($haystack as $value) { | |
if (Str::contains($value, $needle)) { | |
return; | |
} | |
} | |
self::fail($msg); | |
} | |
/** | |
* Assert that none of the values in the array contain something. | |
* | |
* @param string|int $needle | |
* @param array $haystack | |
* @param string $msg | |
* | |
* @return void | |
*/ | |
public static function assertArrayNotContains($needle, array $haystack, string $msg = '') | |
{ | |
if ($msg === '') { | |
$msg = "Expected none of the values in the array to contain '$needle'."; | |
} | |
foreach ($haystack as $value) { | |
if (Str::contains($value, $needle)) { | |
self::fail($msg); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment