Skip to content
This repository has been archived by the owner on Mar 28, 2022. It is now read-only.

Commit

Permalink
Add exception tests
Browse files Browse the repository at this point in the history
  • Loading branch information
DASPRiD committed May 23, 2016
1 parent d1fb388 commit c6fa8fa
Show file tree
Hide file tree
Showing 5 changed files with 85 additions and 1 deletion.
11 changes: 11 additions & 0 deletions src/Data.php
Expand Up @@ -20,6 +20,10 @@ private function __construct(array $data)
$this->data = $data;
}

/**
* @throws InvalidKey When a non-string key is encountered
* @throws InvalidValue When a non-string value is encountered
*/
public static function fromFlatArray(array $flatArray) : self
{
$originalCount = count($flatArray);
Expand Down Expand Up @@ -72,6 +76,9 @@ public function hasKey(string $key) : bool
return array_key_exists($key, $this->data);
}

/**
* @throws NonExistentKey When the key cannot be found and no fallback is defined
*/
public function getValue(string $key, string $fallback = null) : string
{
if (array_key_exists($key, $this->data)) {
Expand Down Expand Up @@ -107,6 +114,10 @@ public function isEmpty() : bool
return empty($this->data);
}

/**
* @throws InvalidKey When an invalid key is encountered
* @throws InvalidValue When an invalid value is encountered
*/
private static function flattenNestedArray(array $nestedArray, string $prefix = '') : array
{
$flatArray = [];
Expand Down
2 changes: 1 addition & 1 deletion src/Exception/InvalidValue.php
Expand Up @@ -14,6 +14,6 @@ public static function fromArrayWithNonStringValues(array $array)

public static function fromNonNestedValue($value)
{
return new self(sprintf('Expected string or array value, but "%s" provided', gettype($value)));
return new self(sprintf('Expected string or array value, but "%s" was provided', gettype($value)));
}
}
26 changes: 26 additions & 0 deletions test/Exception/InvalidKeyTest.php
@@ -0,0 +1,26 @@
<?php
declare(strict_types = 1);

namespace DASPRiD\FormidableTest\FormError;

use DASPRiD\Formidable\Exception\InvalidKey;
use PHPUnit_Framework_TestCase as TestCase;

/**
* @covers DASPRiD\Formidable\Exception\InvalidKey
*/
class InvalidKeyTest extends TestCase
{
public function testFromArrayWithNonStringKeys()
{
$this->assertSame('Non-string key in array found', InvalidKey::fromArrayWithNonStringKeys([])->getMessage());
}

public function testFromNonNestedKey()
{
$this->assertSame(
'Expected string or nested integer key, but "boolean" was provided',
InvalidKey::fromNonNestedKey(true)->getMessage()
);
}
}
26 changes: 26 additions & 0 deletions test/Exception/InvalidValueTest.php
@@ -0,0 +1,26 @@
<?php
declare(strict_types = 1);

namespace DASPRiD\FormidableTest\FormError;

use DASPRiD\Formidable\Exception\InvalidValue;
use PHPUnit_Framework_TestCase as TestCase;

/**
* @covers DASPRiD\Formidable\Exception\InvalidValue
*/
class InvalidValueTest extends TestCase
{
public function testFromArrayWithNonStringKeys()
{
$this->assertSame('Non-string value in array found', InvalidValue::fromArrayWithNonStringValues([])->getMessage());
}

public function testFromNonNestedKey()
{
$this->assertSame(
'Expected string or array value, but "boolean" was provided',
InvalidValue::fromNonNestedValue(true)->getMessage()
);
}
}
21 changes: 21 additions & 0 deletions test/Exception/NonExistentKeyTest.php
@@ -0,0 +1,21 @@
<?php
declare(strict_types = 1);

namespace DASPRiD\FormidableTest\FormError;

use DASPRiD\Formidable\Exception\NonExistentKey;
use PHPUnit_Framework_TestCase as TestCase;

/**
* @covers DASPRiD\Formidable\Exception\NonExistentKey
*/
class NonExistentKeyTest extends TestCase
{
public function testFromNonExistentKey()
{
$this->assertSame(
'Non-existent key "foo" provided',
NonExistentKey::fromNonExistentKey('foo')->getMessage()
);
}
}

0 comments on commit c6fa8fa

Please sign in to comment.