Skip to content

Commit

Permalink
some progress on test rewrites
Browse files Browse the repository at this point in the history
  • Loading branch information
Dominik Zogg committed Mar 5, 2019
1 parent 0f1f0bf commit d4119b7
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 137 deletions.
6 changes: 3 additions & 3 deletions tests/Normalizer/CallbackLinkNormalizerTest.php
Expand Up @@ -22,7 +22,7 @@ class CallbackLinkNormalizerTest extends TestCase

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

/** @var LinkInterface|MockObject $link */
$link = $this->getMockByCalls(LinkInterface::class, [
Expand Down Expand Up @@ -62,7 +62,7 @@ function (

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

/** @var NormalizerContextInterface|MockObject $normalizerContext */
$normalizerContext = $this->getMockByCalls(NormalizerContextInterface::class);
Expand All @@ -88,7 +88,7 @@ public function testNormalizeLinkWithWrongDataType()
'The link normalizer callback needs to return a Psr\Link\LinkInterface|null, "string" given at path: "name"'
);

$object = new \stdClass;
$object = new \stdClass();

/** @var NormalizerContextInterface|MockObject $normalizerContext */
$normalizerContext = $this->getMockByCalls(NormalizerContextInterface::class);
Expand Down
4 changes: 2 additions & 2 deletions tests/Normalizer/DateTimeFieldNormalizerTest.php
Expand Up @@ -24,8 +24,8 @@ public function testNormalizeFieldWithInvalidConstructArgument()
$this->expectException(\TypeError::class);
$this->expectExceptionMessage(
'Chubbyphp\Serialization\Normalizer\DateTimeFieldNormalizer::__construct() expects parameter 1 to be '
. 'Chubbyphp\Serialization\Accessor\AccessorInterface|Chubbyphp\Serialization\Normalizer\\'
. 'FieldNormalizerInterface, DateTime given'
.'Chubbyphp\Serialization\Accessor\AccessorInterface|Chubbyphp\Serialization\Normalizer\\'
.'FieldNormalizerInterface, DateTime given'
);

new DateTimeFieldNormalizer(new \DateTime());
Expand Down
4 changes: 2 additions & 2 deletions tests/Normalizer/FieldNormalizerTest.php
Expand Up @@ -21,14 +21,14 @@ class FieldNormalizerTest extends TestCase

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

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

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

$fieldNormalizer = new FieldNormalizer($accessor);
Expand Down
164 changes: 34 additions & 130 deletions tests/Normalizer/Relation/ReferenceOneFieldNormalizerTest.php
Expand Up @@ -4,9 +4,10 @@

namespace Chubbyphp\Tests\Serialization\Normalizer;

use Chubbyphp\Mock\Call;
use Chubbyphp\Mock\MockByCallsTrait;
use Chubbyphp\Serialization\Accessor\AccessorInterface;
use Chubbyphp\Serialization\Normalizer\NormalizerContextInterface;
use Chubbyphp\Serialization\Normalizer\NormalizerInterface;
use Chubbyphp\Serialization\Normalizer\Relation\ReferenceOneFieldNormalizer;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
Expand All @@ -16,158 +17,61 @@
*/
class ReferenceOneFieldNormalizerTest extends TestCase
{
use MockByCallsTrait;

public function testNormalizeFieldWithNull()
{
$object = $this->getObject();

$fieldNormalizer = new ReferenceOneFieldNormalizer($this->getIdentifierAccessor(), $this->getAccessor());
$object = new \stdClass;

$data = $fieldNormalizer->normalizeField(
'relation',
$object,
$this->getNormalizerContext(),
$this->getNormalizer()
);
/** @var AccessorInterface|MockObject $identifierAccessor */
$identifierAccessor = $this->getMockByCalls(AccessorInterface::class);

self::assertSame(null, $data);
}
/** @var AccessorInterface|MockObject $accessor */
$accessor = $this->getMockByCalls(AccessorInterface::class, [
Call::create('getValue')->with($object)->willReturn(null),
]);

public function testNormalizeFieldWithObject()
{
$object = $this->getObject();
$object->setRelation($this->getRelation('id1'));
/** @var NormalizerContextInterface|MockObject $context */
$context = $this->getMockByCalls(NormalizerContextInterface::class);

$fieldNormalizer = new ReferenceOneFieldNormalizer($this->getIdentifierAccessor(), $this->getAccessor());
$fieldNormalizer = new ReferenceOneFieldNormalizer($identifierAccessor, $accessor);

$data = $fieldNormalizer->normalizeField(
'relation',
$object,
$this->getNormalizerContext(),
$this->getNormalizer()
$context
);

self::assertSame('id1', $data);
self::assertSame(null, $data);
}

/**
* @return AccessorInterface
*/
private function getAccessor(): AccessorInterface
public function testNormalizeFieldWithObject()
{
/** @var AccessorInterface|MockObject $accessor */
$accessor = $this->getMockBuilder(AccessorInterface::class)->getMockForAbstractClass();
$relation = new \stdClass;

$accessor->expects(self::any())->method('getValue')->willReturnCallback(function ($object) {
return $object->getRelation();
});
$object = new \stdClass;

return $accessor;
}
/** @var AccessorInterface|MockObject $identifierAccessor */
$identifierAccessor = $this->getMockByCalls(AccessorInterface::class, [
Call::create('getValue')->with($relation)->willReturn('id1'),
]);

/**
* @return AccessorInterface
*/
private function getIdentifierAccessor(): AccessorInterface
{
/** @var AccessorInterface|MockObject $accessor */
$accessor = $this->getMockBuilder(AccessorInterface::class)->getMockForAbstractClass();

$accessor->expects(self::any())->method('getValue')->willReturnCallback(function ($object) {
return $object->getId();
});
$accessor = $this->getMockByCalls(AccessorInterface::class, [
Call::create('getValue')->with($object)->willReturn($relation),
]);

return $accessor;
}

/**
* @return NormalizerInterface
*/
private function getNormalizer(): NormalizerInterface
{
/** @var NormalizerInterface|MockObject $normalizer */
$normalizer = $this->getMockBuilder(NormalizerInterface::class)->getMockForAbstractClass();

$normalizer->expects(self::any())->method('normalize')->willReturnCallback(
function ($object, NormalizerContextInterface $context = null, string $path = '') {
return ['name' => $object->getName()];
}
);

return $normalizer;
}

/**
* @return NormalizerContextInterface
*/
private function getNormalizerContext(): NormalizerContextInterface
{
/** @var NormalizerContextInterface|MockObject $context */
$context = $this->getMockBuilder(NormalizerContextInterface::class)->getMockForAbstractClass();
$context = $this->getMockByCalls(NormalizerContextInterface::class);

return $context;
}
$fieldNormalizer = new ReferenceOneFieldNormalizer($identifierAccessor, $accessor);

/**
* @return object
*/
private function getObject()
{
return new class() {
/**
* @var object
*/
private $relation;

/**
* @return object
*/
public function getRelation()
{
return $this->relation;
}

/**
* @param object $relation
*
* @return self
*/
public function setRelation($relation): self
{
$this->relation = $relation;

return $this;
}
};
}
$data = $fieldNormalizer->normalizeField(
'relation',
$object,
$context
);

/**
* @param string|null $id
*
* @return object
*/
private function getRelation(string $id)
{
return new class($id ?? uniqid()) {
/**
* @var string
*/
private $id;

/**
* @param string $id
*/
public function __construct(string $id)
{
$this->id = $id;
}

/**
* @return string
*/
public function getId(): string
{
return $this->id;
}
};
self::assertSame('id1', $data);
}
}

0 comments on commit d4119b7

Please sign in to comment.