Skip to content

Instantly share code, notes, and snippets.

@svenluijten
Last active March 28, 2018 13:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save svenluijten/b43fbbd77b82ffd8860a7d0c0d69592d to your computer and use it in GitHub Desktop.
Save svenluijten/b43fbbd77b82ffd8860a7d0c0d69592d to your computer and use it in GitHub Desktop.
extra assertions against arrays
<?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