Skip to content

Commit

Permalink
fix stuff based on infection testing
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Zogg committed May 1, 2019
1 parent 321a8ee commit 6b40d66
Show file tree
Hide file tree
Showing 14 changed files with 291 additions and 34 deletions.
5 changes: 5 additions & 0 deletions phpstan.neon
@@ -0,0 +1,5 @@
parameters:
level: 7
paths:
- src
ignoreErrors: []
7 changes: 5 additions & 2 deletions src/Accessor/PropertyAccessor.php
Expand Up @@ -42,7 +42,7 @@ function ($property, $value) {
$class
);

return $setter($this->property, $value);
$setter($this->property, $value);
}

/**
Expand Down Expand Up @@ -81,7 +81,10 @@ private function getClass($object): string
$object->__load();
}

return (new \ReflectionClass($object))->getParentClass()->name;
$reflectionParentClass = (new \ReflectionObject($object))->getParentClass();
if ($reflectionParentClass instanceof \ReflectionClass) {
return $reflectionParentClass->getName();
}
}

return get_class($object);
Expand Down
4 changes: 0 additions & 4 deletions src/Decoder/JsonxTypeDecoder.php
Expand Up @@ -53,10 +53,6 @@ private function decodeNode(\DOMNode $node)
{
$nodeName = $node->nodeName;

if (0 !== strpos($nodeName, 'json:')) {
throw DeserializerRuntimeException::createNotParsable($this->getContentType());
}

$nodeType = substr($nodeName, 5);

if (self::DATATYPE_OBJECT === $nodeType) {
Expand Down
4 changes: 1 addition & 3 deletions src/Decoder/UrlEncodedTypeDecoder.php
Expand Up @@ -43,9 +43,7 @@ public function decode(string $data): array
private function fixValues(array $rawData): array
{
$data = [];
foreach ($rawData as $rawKey => $value) {
$key = (string) (int) $rawKey === $rawKey ? (int) $rawKey : $rawKey;

foreach ($rawData as $key => $value) {
if (is_array($value)) {
$data[$key] = $this->fixValues($value);
} else {
Expand Down
4 changes: 0 additions & 4 deletions src/Denormalizer/ConvertTypeFieldDenormalizer.php
Expand Up @@ -112,10 +112,6 @@ private function convertType($value)
*/
private function convertBool($value)
{
if (is_float($value) || is_int($value)) {
return $value;
}

if ('true' === $value) {
return true;
}
Expand Down
8 changes: 4 additions & 4 deletions src/Denormalizer/Denormalizer.php
Expand Up @@ -207,14 +207,14 @@ private function isWithinGroup(
}

/**
* @param string $path
* @param string|int $name
* @param string $path
* @param string $name
*
* @return string
*/
private function getSubPathByName(string $path, $name): string
private function getSubPathByName(string $path, string $name): string
{
return '' === $path ? (string) $name : $path.'.'.$name;
return '' === $path ? $name : $path.'.'.$name;
}

/**
Expand Down
3 changes: 2 additions & 1 deletion src/Denormalizer/DenormalizerObjectMappingRegistry.php
Expand Up @@ -45,7 +45,8 @@ public function getObjectMapping(string $class): DenormalizationObjectMappingInt
$reflectionClass = new \ReflectionClass($class);

if (in_array('Doctrine\Common\Persistence\Proxy', $reflectionClass->getInterfaceNames(), true)) {
if (false !== $reflectionParentClass = $reflectionClass->getParentClass()) {
$reflectionParentClass = (new \ReflectionClass($class))->getParentClass();
if ($reflectionParentClass instanceof \ReflectionClass) {
$class = $reflectionParentClass->getName();
}
}
Expand Down
15 changes: 10 additions & 5 deletions src/Mapping/DenormalizationFieldMappingBuilder.php
Expand Up @@ -28,7 +28,7 @@ final class DenormalizationFieldMappingBuilder implements DenormalizationFieldMa
private $groups = [];

/**
* @var FieldDenormalizerInterface|null
* @var FieldDenormalizerInterface
*/
private $fieldDenormalizer;

Expand Down Expand Up @@ -71,8 +71,11 @@ public static function createCallback(string $name, callable $callback): Denorma
*
* @return DenormalizationFieldMappingBuilderInterface
*/
public static function createConvertType(string $name, string $type, bool $emptyToNull = false): DenormalizationFieldMappingBuilderInterface
{
public static function createConvertType(
string $name,
string $type,
bool $emptyToNull = false
): DenormalizationFieldMappingBuilderInterface {
$self = new self($name);
$self->fieldDenormalizer = new ConvertTypeFieldDenormalizer(new PropertyAccessor($name), $type, $emptyToNull);

Expand All @@ -84,8 +87,10 @@ public static function createConvertType(string $name, string $type, bool $empty
*
* @return DenormalizationFieldMappingBuilderInterface
*/
public static function createDateTime(string $name, bool $emptyToNull = false): DenormalizationFieldMappingBuilderInterface
{
public static function createDateTime(
string $name,
bool $emptyToNull = false
): DenormalizationFieldMappingBuilderInterface {
$self = new self($name);
$self->fieldDenormalizer = new DateTimeFieldDenormalizer(new PropertyAccessor($name), $emptyToNull);

Expand Down
26 changes: 23 additions & 3 deletions tests/Accessor/PropertyAccessorTest.php
Expand Up @@ -41,24 +41,34 @@ public function getName(): string
public function testSetValueCanAccessPrivatePropertyThroughDoctrineProxyClass()
{
$object = new class() extends AbstractManyModel implements Proxy {
/**
* @var bool
*/
private $initialized = false;

public function __load()
{
$this->initialized = true;
}

/**
* @return bool
*/
public function __isInitialized()
{
return false;
return $this->initialized;
}
};

$accessor = new PropertyAccessor('address');

self::assertFalse($object->__isInitialized());

$accessor->setValue($object, 'Address');

self::assertSame('Address', $accessor->getValue($object));
self::assertTrue($object->__isInitialized());

self::assertSame('Address', $object->getAddress());
}

public function testMissingSet()
Expand Down Expand Up @@ -99,24 +109,34 @@ public function setName(string $name)
public function testGetValueCanAccessPrivatePropertyThroughDoctrineProxyClass()
{
$object = new class() extends AbstractManyModel implements Proxy {
/**
* @var bool
*/
private $initialized = false;

public function __load()
{
$this->initialized = true;
}

/**
* @return bool
*/
public function __isInitialized()
{
return false;
return $this->initialized;
}
};

$object->setAddress('Address');

$accessor = new PropertyAccessor('address');

self::assertFalse($object->__isInitialized());

self::assertSame('Address', $accessor->getValue($object));

self::assertTrue($object->__isInitialized());
}

public function testMissingGet()
Expand Down
17 changes: 17 additions & 0 deletions tests/Denormalizer/ConvertTypeFieldDenormalizerTest.php
Expand Up @@ -397,6 +397,23 @@ public function testDenormalizeFieldWithEmptyToNullDisabled()
$fieldDenormalizer->denormalizeField('value', $object, '', $context);
}

public function testDenormalizeFieldWithObjectToString()
{
$object = new \stdClass();
$valueObject = new \stdClass();

/** @var AccessorInterface|MockObject $accessor */
$accessor = $this->getMockByCalls(AccessorInterface::class, [
Call::create('setValue')->with($object, $valueObject),
]);

/** @var DenormalizerContextInterface|MockObject $context */
$context = $this->getMockByCalls(DenormalizerContextInterface::class);

$fieldDenormalizer = new ConvertTypeFieldDenormalizer($accessor, ConvertTypeFieldDenormalizer::TYPE_STRING);
$fieldDenormalizer->denormalizeField('value', $object, $valueObject, $context);
}

public function testDenormalizeFieldWithEmptyToNullEnabled()
{
$object = new \stdClass();
Expand Down
17 changes: 15 additions & 2 deletions tests/Denormalizer/DateTimeFieldDenormalizerTest.php
Expand Up @@ -53,7 +53,6 @@ function ($date) {
),
]);


error_clear_last();

$fieldDenormalizer = new DateTimeFieldDenormalizer($fieldDenormalizer);
Expand All @@ -63,7 +62,14 @@ function ($date) {
self::assertNotNull($error);

self::assertSame(E_USER_DEPRECATED, $error['type']);
self::assertSame('Use "Chubbyphp\\Deserialization\\Accessor\\AccessorInterface" instead of "Chubbyphp\\Deserialization\\Denormalizer\\FieldDenormalizerInterface" as __construct argument', $error['message']);
self::assertSame(
sprintf(
'Use "%s" instead of "%s" as __construct argument',
AccessorInterface::class,
FieldDenormalizerInterface::class
),
$error['message']
);

$fieldDenormalizer->denormalizeField('date', $object, '2017-01-01', $context);
}
Expand Down Expand Up @@ -230,7 +236,14 @@ public function testDenormalizeZeroStringField()
$context = $this->getMockByCalls(DenormalizerContextInterface::class);

$fieldDenormalizer = new DateTimeFieldDenormalizer($accessor);

error_clear_last();

$fieldDenormalizer->denormalizeField('date', $object, '0', $context);

$error = error_get_last();

self::assertNull($error);
}

public function testDenormalizeArrayField()
Expand Down
54 changes: 54 additions & 0 deletions tests/Denormalizer/DenormalizerTest.php
Expand Up @@ -141,6 +141,60 @@ public function testDenormalizeWithNewAndType()
);
}

public function testDenormalizeWithNewAndTypeAndResetMissinFields()
{
$object = new \stdClass();

$factory = function () use ($object) {
return $object;
};

$context = DenormalizerContextBuilder::create()->setResetMissingFields(true)->getContext();

/** @var FieldDenormalizerInterface|MockObject $nameFieldDenormalizer */
$nameFieldDenormalizer = $this->getMockByCalls(FieldDenormalizerInterface::class, [
Call::create('denormalizeField')
->with('name', $object, 'name', $context, new ArgumentInstanceOf(DenormalizerInterface::class)),
]);

/** @var DenormalizationFieldMappingInterface|MockObject $denormalizationNameFieldMapping */
$denormalizationNameFieldMapping = $this->getMockByCalls(DenormalizationFieldMappingInterface::class, [
Call::create('getName')->with()->willReturn('name'),
Call::create('getFieldDenormalizer')->with()->willReturn($nameFieldDenormalizer),
]);

/** @var DenormalizationFieldMappingInterface|MockObject $denormalizationValueFieldMapping */
$denormalizationValueFieldMapping = $this->getMockByCalls(DenormalizationFieldMappingInterface::class, [
Call::create('getName')->with()->willReturn('value'),
]);

/** @var DenormalizationObjectMappingInterface|MockObject $objectMapping */
$objectMapping = $this->getMockByCalls(DenormalizationObjectMappingInterface::class, [
Call::create('getDenormalizationFactory')->with('', 'object')->willReturn($factory),
Call::create('getDenormalizationFieldMappings')->with('', 'object')->willReturn([
$denormalizationNameFieldMapping,
$denormalizationValueFieldMapping,
]),
]);

/** @var DenormalizerObjectMappingRegistryInterface|MockObject $registry */
$registry = $this->getMockByCalls(DenormalizerObjectMappingRegistryInterface::class, [
Call::create('getObjectMapping')->with(\stdClass::class)->willReturn($objectMapping),
]);

/** @var LoggerInterface|MockObject $logger */
$logger = $this->getMockByCalls(LoggerInterface::class, [
Call::create('info')->with('deserialize: path {path}', ['path' => 'name']),
]);

$denormalizer = new Denormalizer($registry, $logger);

self::assertSame(
$object,
$denormalizer->denormalize(\stdClass::class, ['name' => 'name', '_type' => 'object'], $context)
);
}

public function testDenormalizeWithExisting()
{
$object = new \stdClass();
Expand Down

0 comments on commit 6b40d66

Please sign in to comment.