Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Prokyonn committed Jul 29, 2021
1 parent 01da991 commit 29bf9d0
Show file tree
Hide file tree
Showing 6 changed files with 624 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ContentBundle\Tests\Functional\Content\Infrastructure\Doctrine;

use Sulu\Bundle\ContentBundle\Content\Domain\Factory\ContactFactoryInterface;
use Sulu\Bundle\TestBundle\Testing\SuluTestCase;

class ContactFactoryTest extends SuluTestCase
{
protected function setUp(): void
{
self::bootKernel();
self::purgeDatabase();
}

public function createContactFactory(): ContactFactoryInterface
{
return self::$container->get('sulu_content.contact_factory');
}

/**
* @dataProvider dataProvider
*/
public function testCreate(?int $contactId): void
{
$contactFactory = $this->createContactFactory();

$result = $contactFactory->create($contactId);
$this->assertSame(
$contactId,
$result ? $result->getId() : $result
);
}

/**
* @return \Generator<mixed[]>
*/
public function dataProvider(): \Generator
{
yield [
null,
];

yield [
1,
];
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ContentBundle\Tests\Unit\Content\Application\ContentDataMapper\DataMapper;

use DateTimeImmutable;
use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Sulu\Bundle\ContactBundle\Entity\ContactInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentDataMapper\DataMapper\AuthorDataMapper;
use Sulu\Bundle\ContentBundle\Content\Domain\Factory\ContactFactoryInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\AuthorInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentCollectionInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;

class AuthorDataMapperTest extends TestCase
{
protected function createAuthorDataMapperInstance(ContactFactoryInterface $contactFactory): AuthorDataMapper
{
return new AuthorDataMapper($contactFactory);
}

public function testMapNoAuthor(): void
{
$data = [
'author' => 1,
'authored' => '2020-05-08T00:00:00+00:00',
];

$unlocalizedDimensionContent = $this->prophesize(DimensionContentInterface::class);

$dimensionContentCollection = $this->prophesize(DimensionContentCollectionInterface::class);
$dimensionContentCollection->getDimensionAttributes()
->shouldBeCalled()
->willReturn(['stage' => 'draft', 'locale' => 'de']);
$dimensionContentCollection->getDimensionContent(['stage' => 'draft', 'locale' => null])
->shouldBeCalled()
->willReturn($unlocalizedDimensionContent);

$contactFactory = $this->prophesize(ContactFactoryInterface::class);

$authorMapper = $this->createAuthorDataMapperInstance($contactFactory->reveal());
$authorMapper->map($data, $dimensionContentCollection->reveal());
}

public function testMapLocalizedNoAuthor(): void
{
$data = [
'author' => 1,
'authored' => '2020-05-08T00:00:00+00:00',
];

$unlocalizedDimensionContent = $this->prophesize(DimensionContentInterface::class);
$unlocalizedDimensionContent->willImplement(AuthorInterface::class);

$localizedDimensionContent = $this->prophesize(DimensionContentInterface::class);

$dimensionContentCollection = $this->prophesize(DimensionContentCollectionInterface::class);
$dimensionContentCollection->getDimensionAttributes()
->shouldBeCalled()
->willReturn(['stage' => 'draft', 'locale' => 'de']);
$dimensionContentCollection->getDimensionContent(['stage' => 'draft', 'locale' => null])
->willReturn($unlocalizedDimensionContent);
$dimensionContentCollection->getDimensionContent(['stage' => 'draft', 'locale' => 'de'])
->willReturn($localizedDimensionContent->reveal());

$contactFactory = $this->prophesize(ContactFactoryInterface::class);

$this->expectException(\RuntimeException::class);
$this->expectExceptionMessage('Expected "$localizedObject" from type "Sulu\Bundle\ContentBundle\Content\Domain\Model\AuthorInterface" but "Double\DimensionContentInterface\P3" given.');

$authorMapper = $this->createAuthorDataMapperInstance($contactFactory->reveal());
$authorMapper->map($data, $dimensionContentCollection->reveal());
}

public function testMapUnlocalizedAuthor(): void
{
$data = [
'author' => 1,
'authored' => '2020-05-08T00:00:00+00:00',
];
$contact = $this->prophesize(ContactInterface::class);
$contactFactory = $this->prophesize(ContactFactoryInterface::class);
$contactFactory->create(1)
->shouldBeCalled()
->willReturn($contact->reveal());

$unlocalizedDimensionContent = $this->prophesize(DimensionContentInterface::class);
$unlocalizedDimensionContent->willImplement(AuthorInterface::class);

$unlocalizedDimensionContent->setAuthor($contact->reveal())
->shouldBeCalled();
$unlocalizedDimensionContent->setAuthored(Argument::that(
function (DateTimeImmutable $date) {
return $date->getTimestamp() === (new \DateTimeImmutable('2020-05-08T00:00:00+00:00'))->getTimestamp();
})
)->shouldBeCalled();

$dimensionContentCollection = $this->prophesize(DimensionContentCollectionInterface::class);
$dimensionContentCollection->getDimensionAttributes()
->shouldBeCalled()
->willReturn(['stage' => 'draft', 'locale' => 'de']);
$dimensionContentCollection->getDimensionContent(['stage' => 'draft', 'locale' => null])
->willReturn($unlocalizedDimensionContent->reveal());
$dimensionContentCollection->getDimensionContent(['stage' => 'draft', 'locale' => 'de'])
->willReturn(null);

$authorMapper = $this->createAuthorDataMapperInstance($contactFactory->reveal());
$authorMapper->map($data, $dimensionContentCollection->reveal());
}

public function testMapLocalizedAuthor(): void
{
$data = [
'author' => 1,
'authored' => '2020-05-08T00:00:00+00:00',
];
$contact = $this->prophesize(ContactInterface::class);
$contactFactory = $this->prophesize(ContactFactoryInterface::class);
$contactFactory->create(1)
->shouldBeCalled()
->willReturn($contact->reveal());

$unlocalizedDimensionContent = $this->prophesize(DimensionContentInterface::class);
$unlocalizedDimensionContent->willImplement(AuthorInterface::class);

$localizedDimensionContent = $this->prophesize(DimensionContentInterface::class);
$localizedDimensionContent->willImplement(AuthorInterface::class);

$localizedDimensionContent->setAuthor($contact->reveal())
->shouldBeCalled();
$localizedDimensionContent->setAuthored(Argument::that(
function (DateTimeImmutable $date) {
return $date->getTimestamp() === (new \DateTimeImmutable('2020-05-08T00:00:00+00:00'))->getTimestamp();
})
)->shouldBeCalled();

$dimensionContentCollection = $this->prophesize(DimensionContentCollectionInterface::class);
$dimensionContentCollection->getDimensionAttributes()
->shouldBeCalled()
->willReturn(['stage' => 'draft', 'locale' => 'de']);
$dimensionContentCollection->getDimensionContent(['stage' => 'draft', 'locale' => null])
->willReturn($unlocalizedDimensionContent->reveal());
$dimensionContentCollection->getDimensionContent(['stage' => 'draft', 'locale' => 'de'])
->willReturn($localizedDimensionContent->reveal());

$authorMapper = $this->createAuthorDataMapperInstance($contactFactory->reveal());
$authorMapper->map($data, $dimensionContentCollection->reveal());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
<?php

declare(strict_types=1);

/*
* This file is part of Sulu.
*
* (c) Sulu GmbH
*
* This source file is subject to the MIT license that is bundled
* with this source code in the file LICENSE.
*/

namespace Sulu\Bundle\ContentBundle\Tests\Unit\Content\Application\ContentMerger\Merger;

use PHPUnit\Framework\TestCase;
use Prophecy\Argument;
use Sulu\Bundle\ContactBundle\Entity\ContactInterface;
use Sulu\Bundle\ContentBundle\Content\Application\ContentMerger\Merger\AuthorMerger;
use Sulu\Bundle\ContentBundle\Content\Application\ContentMerger\Merger\MergerInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\AuthorInterface;
use Sulu\Bundle\ContentBundle\Content\Domain\Model\DimensionContentInterface;

class AuthorMergerTest extends TestCase
{
protected function getAuthorMergerInstance(): MergerInterface
{
return new AuthorMerger();
}

public function testMergeSourceNotImplementAuthorInterface(): void
{
$merger = $this->getAuthorMergerInstance();

$source = $this->prophesize(DimensionContentInterface::class);

$target = $this->prophesize(DimensionContentInterface::class);
$target->willImplement(AuthorInterface::class);
$target->setAuthor(Argument::any())->shouldNotBeCalled();

$merger->merge($target->reveal(), $source->reveal());
}

public function testMergeTargetNotImplementAuthorInterface(): void
{
$merger = $this->getAuthorMergerInstance();

$source = $this->prophesize(DimensionContentInterface::class);
$source->willImplement(AuthorInterface::class);
$source->getAuthor(Argument::any())->shouldNotBeCalled();

$target = $this->prophesize(DimensionContentInterface::class);

$merger->merge($target->reveal(), $source->reveal());
}

public function testMergeSet(): void
{
$merger = $this->getAuthorMergerInstance();

$contact = $this->prophesize(ContactInterface::class);
$authoredDate = new \DateTimeImmutable('2020-05-08T00:00:00+00:00');

$source = $this->prophesize(DimensionContentInterface::class);
$source->willImplement(AuthorInterface::class);
$source->getAuthor()->willReturn($contact->reveal())->shouldBeCalled();
$source->getAuthored()->willReturn($authoredDate)->shouldBeCalled();

$target = $this->prophesize(DimensionContentInterface::class);
$target->willImplement(AuthorInterface::class);
$target->setAuthor($contact->reveal())->shouldBeCalled();
$target->setAuthored($authoredDate)->shouldBeCalled();

$merger->merge($target->reveal(), $source->reveal());
}

public function testMergeNotSet(): void
{
$merger = $this->getAuthorMergerInstance();

$source = $this->prophesize(DimensionContentInterface::class);
$source->willImplement(AuthorInterface::class);
$source->getAuthor()->willReturn(null)->shouldBeCalled();
$source->getAuthored()->willReturn(null)->shouldBeCalled();

$target = $this->prophesize(DimensionContentInterface::class);
$target->willImplement(AuthorInterface::class);
$target->setAuthor(Argument::any())->shouldNotBeCalled();
$target->setAuthored(Argument::any())->shouldNotBeCalled();

$merger->merge($target->reveal(), $source->reveal());
}
}
Loading

0 comments on commit 29bf9d0

Please sign in to comment.