Skip to content

Commit

Permalink
Add KeyValidatorTrait::validateKeys
Browse files Browse the repository at this point in the history
  • Loading branch information
chadicus committed Sep 9, 2017
1 parent a52954f commit 4d6a7ec
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 1 deletion.
16 changes: 15 additions & 1 deletion src/KeyValidatorTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
trait KeyValidatorTrait
{
/**
* Verifies the the given cache key is a legal value.
* Verifies the given cache key is a legal value.
*
* @param mixed $key The cache key to validate.
*
Expand All @@ -26,4 +26,18 @@ protected function validateKey($key)
throw new InvalidArgumentException("Key '{$key}' contains unsupported characters");
}
}

/**
* Verifies all of the given cache keys are a legal values.
*
* @param array $keys The collection cache key to validate.
*
* @return void
*
* @throws InvalidArgumentException Thrown if the $key string is not a legal value.
*/
protected function validateKeys(array $keys)
{
array_walk($keys, [$this, 'validateKey']);
}
}
26 changes: 26 additions & 0 deletions tests/KeyValidatorTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,32 @@ public function validateKeyWithInvalidValue($key)
$this->validateKey($key);
}

/**
* @test
* @covers ::validateKeys
*
* @return void
*/
public function validateKeysWithString()
{
$this->assertNull($this->validateKeys(['a valid string', 'another valid string']));
}

/**
* @param mixed $key The key value which will throw an execption.
*
* @test
* @covers ::validateKeys
* @expectedException \Psr\SimpleCache\InvalidArgumentException
* @dataProvider provideInvalidKeys
*
* @return void
*/
public function validateKeysWithInvalidValue($key)
{
$this->validateKeys(['valid_key', $key, 'another_valid_key']);
}

/**
* Provides valid keys for testing.
*
Expand Down

0 comments on commit 4d6a7ec

Please sign in to comment.