Skip to content

Commit

Permalink
Add missing tests (nelmio#739)
Browse files Browse the repository at this point in the history
  • Loading branch information
theofidry authored May 21, 2017
1 parent 2747dc4 commit 26691c0
Show file tree
Hide file tree
Showing 74 changed files with 1,071 additions and 261 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
<?php

/*
* This file is part of the Alice package.
*
* (c) Nelmio <[email protected]>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace Nelmio\Alice\Throwable\Exception\FileLocator;

final class ChildFileNotFoundException extends FileNotFoundException
{
}
2 changes: 2 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@
<directory>src</directory>
<exclude>
<directory>src/Bridge</directory>

<file>src/deep_clone.php</file>
</exclude>
</whitelist>
</filter>
Expand Down
5 changes: 0 additions & 5 deletions src/Generator/Resolver/UniqueValuesPool.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
namespace Nelmio\Alice\Generator\Resolver;

use Nelmio\Alice\Definition\Value\UniqueValue;
use Nelmio\Alice\Throwable\Exception\InvalidArgumentExceptionFactory;

/**
* Class storing all the unique values.
Expand Down Expand Up @@ -59,10 +58,6 @@ private function isIdentical($val1, $val2): bool
return $val1 === $val2;
}

if (false === is_array($val1)) {
throw InvalidArgumentExceptionFactory::createForUnsupportedTypeForIdenticalValuesCheck($val1);
}

foreach ($val1 as $key => $item) {
if (is_string($key)) {
if (false === $this->isIdentical($item, $val2[$key])) {
Expand Down
7 changes: 5 additions & 2 deletions src/ObjectBag.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@

namespace Nelmio\Alice;

use ArrayIterator;
use Countable;
use IteratorAggregate;
use Nelmio\Alice\Definition\Object\CompleteObject;
use Nelmio\Alice\Definition\Object\SimpleObject;
use Nelmio\Alice\Throwable\Exception\InvalidArgumentExceptionFactory;
Expand All @@ -22,7 +25,7 @@
/**
* Value object containing a list of objects.
*/
final class ObjectBag implements \IteratorAggregate, \Countable
final class ObjectBag implements IteratorAggregate, Countable
{
/**
* @var ObjectInterface[]
Expand Down Expand Up @@ -140,7 +143,7 @@ public function count()
*/
public function getIterator()
{
return new \ArrayIterator($this->objects);
return new ArrayIterator($this->objects);
}

public function toArray(): array
Expand Down
27 changes: 16 additions & 11 deletions src/PropertyAccess/ReflectionPropertyAccessor.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

namespace Nelmio\Alice\PropertyAccess;

use Closure;
use Nelmio\Alice\IsAServiceTrait;
use ReflectionClass;
use Symfony\Component\PropertyAccess\Exception\NoSuchPropertyException;
use Symfony\Component\PropertyAccess\PropertyAccessorInterface;

Expand Down Expand Up @@ -41,12 +43,12 @@ public function setValue(&$objectOrArray, $propertyPath, $value)
{
try {
$this->decoratedPropertyAccessor->setValue($objectOrArray, $propertyPath, $value);
} catch (NoSuchPropertyException $e) {
if (!$this->propertyExists($objectOrArray, $propertyPath)) {
throw $e;
} catch (NoSuchPropertyException $exception) {
if (false === $this->propertyExists($objectOrArray, $propertyPath)) {
throw $exception;
}

$setPropertyClosure = \Closure::bind(
$setPropertyClosure = Closure::bind(
function ($object) use ($propertyPath, $value) {
$object->{$propertyPath} = $value;
},
Expand All @@ -65,12 +67,12 @@ public function getValue($objectOrArray, $propertyPath)
{
try {
return $this->decoratedPropertyAccessor->getValue($objectOrArray, $propertyPath);
} catch (NoSuchPropertyException $e) {
if (!$this->propertyExists($objectOrArray, $propertyPath)) {
throw $e;
} catch (NoSuchPropertyException $exception) {
if (false === $this->propertyExists($objectOrArray, $propertyPath)) {
throw $exception;
}

$getPropertyClosure = \Closure::bind(
$getPropertyClosure = Closure::bind(
function ($object) use ($propertyPath) {
return $object->{$propertyPath};
},
Expand Down Expand Up @@ -106,12 +108,15 @@ public function isReadable($objectOrArray, $propertyPath)
*/
private function propertyExists($objectOrArray, $propertyPath)
{
if (!is_object($objectOrArray)) {
if (false === is_object($objectOrArray)) {
return false;
}

$reflectionClass = (new \ReflectionClass(get_class($objectOrArray)));
$reflectionClass = (new ReflectionClass(get_class($objectOrArray)));

return $reflectionClass->hasProperty($propertyPath) && !$reflectionClass->getProperty($propertyPath)->isStatic();
return (
$reflectionClass->hasProperty($propertyPath)
&& false === $reflectionClass->getProperty($propertyPath)->isStatic()
);
}
}
81 changes: 43 additions & 38 deletions src/Throwable/Error/TypeErrorFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,47 +16,48 @@
use Nelmio\Alice\Definition\ValueInterface;
use Nelmio\Alice\FixtureBuilder\Denormalizer\Fixture\ChainableFixtureDenormalizerInterface;
use Nelmio\Alice\Generator\Resolver\ParameterResolverInterface;
use TypeError;

/**
* @private
*/
final class TypeErrorFactory
{
public static function createForObjectArgument($instance): \TypeError
public static function createForObjectArgument($instance): TypeError
{
return new \TypeError(
return new TypeError(
sprintf(
'Expected instance argument to be an object, got "%s" instead.',
'Expected instance argument to be an object. Got "%s" instead.',
gettype($instance)
)
);
}

public static function createForDynamicArrayQuantifier($quantifier): \TypeError
public static function createForDynamicArrayQuantifier($quantifier): TypeError
{
return new \TypeError(
return new TypeError(
sprintf(
'Expected quantifier to be either an integer or a "%s" object. Got "%s" instead.',
'Expected quantifier to be either an integer or a "%s". Got "%s" instead.',
ValueInterface::class,
is_object($quantifier) ? get_class($quantifier) : gettype($quantifier)
)
);
}

public static function createForDynamicArrayElement($element): \TypeError
public static function createForDynamicArrayElement($element): TypeError
{
return new \TypeError(
return new TypeError(
sprintf(
'Expected element to be either string, an array or a "%s" object. Got "%s" instead.',
'Expected element to be either string, an array or a "%s". Got "%s" instead.',
ValueInterface::class,
is_object($element) ? get_class($element) : gettype($element)
)
);
}

public static function createForOptionalValueQuantifier($quantifier): \TypeError
public static function createForOptionalValueQuantifier($quantifier): TypeError
{
return new \TypeError(
return new TypeError(
sprintf(
'Expected quantifier to be either a scalar value or an instance of "%s". Got "%s" instead.',
ValueInterface::class,
Expand All @@ -65,9 +66,9 @@ public static function createForOptionalValueQuantifier($quantifier): \TypeError
);
}

public static function createForOptionalValueFirstMember($firstMember): \TypeError
public static function createForOptionalValueFirstMember($firstMember): TypeError
{
return new \TypeError(
return new TypeError(
sprintf(
'Expected first member to be either a string or an instance of "%s". Got "%s" instead.',
ValueInterface::class,
Expand All @@ -76,9 +77,9 @@ public static function createForOptionalValueFirstMember($firstMember): \TypeErr
);
}

public static function createForOptionalValueSecondMember($secondMember): \TypeError
public static function createForOptionalValueSecondMember($secondMember): TypeError
{
return new \TypeError(
return new TypeError(
sprintf(
'Expected second member to be either null, a string or an instance of "%s". Got "%s" instead.',
ValueInterface::class,
Expand All @@ -87,9 +88,9 @@ public static function createForOptionalValueSecondMember($secondMember): \TypeE
);
}

public static function createForInvalidParameterKey($parameterKey): \TypeError
public static function createForInvalidParameterKey($parameterKey): TypeError
{
return new \TypeError(
return new TypeError(
sprintf(
'Expected parameter key to be either a string or an instance of "%s". Got "%s" instead.',
ValueInterface::class,
Expand All @@ -98,63 +99,63 @@ public static function createForInvalidParameterKey($parameterKey): \TypeError
);
}

public static function createForInvalidDenormalizerType(int $index, $denormalizer): \TypeError
public static function createForInvalidDenormalizerType(int $index, $denormalizer): TypeError
{
return new \TypeError(
return new TypeError(
sprintf(
'Expected denormalizer %d to be a "%s", got "%s" instead.',
'Expected denormalizer %d to be a "%s". Got "%s" instead.',
$index,
ChainableFixtureDenormalizerInterface::class,
is_object($denormalizer) ? get_class($denormalizer) : gettype($denormalizer)
)
);
}

public static function createForInvalidSpecificationBagMethodCall($methodCall): \TypeError
public static function createForInvalidSpecificationBagMethodCall($methodCall): TypeError
{
return new \TypeError(
return new TypeError(
sprintf(
'Expected method call value to be an array, got "%s" instead.',
'Expected method call value to be an array. Got "%s" instead.',
gettype($methodCall)
)
);
}

public static function createForInvalidSpecificationBagMethodCallName($unparsedMethod): \TypeError
public static function createForInvalidSpecificationBagMethodCallName($unparsedMethod): TypeError
{
return new \TypeError(
return new TypeError(
sprintf(
'Expected method name, got "%s" instead.',
'Expected method name. Got "%s" instead.',
gettype($unparsedMethod)
)
);
}

public static function createForInvalidFixtureBagParameters($fixturesParameters): \TypeError
public static function createForInvalidFixtureBagParameters($fixturesParameters): TypeError
{
return new \TypeError(
return new TypeError(
sprintf(
'Expected parameters to be an array. Got "%s" instead.',
is_object($fixturesParameters) ? get_class($fixturesParameters) : gettype($fixturesParameters)
)
);
}

public static function createForInvalidIncludeStatementInData($include, string $file): \TypeError
public static function createForInvalidIncludeStatementInData($include, string $file): TypeError
{
return new \TypeError(
return new TypeError(
sprintf(
'Expected include statement to be either null or an array of files to include. Got %s instead in '
.'file "%s".',
'Expected include statement to be either null or an array of files to include. Got "%s" instead '
.'in file "%s".',
gettype($include),
$file
)
);
}

public static function createForInvalidIncludedFilesInData($includeFile, string $file): \TypeError
public static function createForInvalidIncludedFilesInData($includeFile, string $file): TypeError
{
return new \TypeError(
return new TypeError(
sprintf(
'Expected elements of include statement to be file names. Got "%s" instead in file "%s".',
gettype($includeFile),
Expand All @@ -163,24 +164,28 @@ public static function createForInvalidIncludedFilesInData($includeFile, string
);
}

public static function createForInvalidFixtureFileReturnedData(string $file): \TypeError
public static function createForInvalidFixtureFileReturnedData(string $file): TypeError
{
return new \TypeError(
return new TypeError(
sprintf(
'The file "%s" must return a PHP array.',
$file
)
);
}

public static function createForInvalidChainableParameterResolver($resolver): \TypeError
public static function createForInvalidChainableParameterResolver($resolver): TypeError
{
return new \TypeError(
return new TypeError(
sprintf(
'Expected resolvers to be "%s" objects. Got "%s" instead.',
ParameterResolverInterface::class,
is_object($resolver)? get_class($resolver) : $resolver
)
);
}

private function __construct()
{
}
}
10 changes: 8 additions & 2 deletions src/Throwable/Exception/BadMethodCallExceptionFactory.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,18 +13,24 @@

namespace Nelmio\Alice\Throwable\Exception;

use BadMethodCallException;

/**
* @private
*/
final class BadMethodCallExceptionFactory
{
public static function createForUnknownMethod(string $method): \BadMethodCallException
public static function createForUnknownMethod(string $method): BadMethodCallException
{
return new \BadMethodCallException(
return new BadMethodCallException(
sprintf(
'Unknown method "%s".',
$method
)
);
}

private function __construct()
{
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@

namespace Nelmio\Alice\Throwable\Exception\FileLocator;

class FileNotFoundException extends \UnexpectedValueException
use UnexpectedValueException;

class FileNotFoundException extends UnexpectedValueException
{
public static function createForEmptyFile(): self
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,4 +66,8 @@ public static function createForInvalidScopeForUniqueValue(): InvalidScopeExcept
{
return new InvalidScopeException('Cannot bind a unique value scope to a temporary fixture.');
}

private function __construct()
{
}
}
Loading

0 comments on commit 26691c0

Please sign in to comment.