Skip to content

Commit

Permalink
Add tests. (#46)
Browse files Browse the repository at this point in the history
Co-authored-by: Andrii Tomchyshyn <[email protected]>
  • Loading branch information
metalslave and Andrii Tomchyshyn authored Sep 11, 2023
1 parent 4a31b7e commit cfa4adb
Show file tree
Hide file tree
Showing 9 changed files with 428 additions and 3 deletions.
2 changes: 1 addition & 1 deletion Service/DependentEntity/DependentEntityService.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function setDependentEntities(DependentEntityInterface $entity): void
continue;
}

$propertyPath = $attributes[0]->getArguments()['propertypath'];
$propertyPath = $attributes[0]->getArguments()['propertyPath'];

$propertyPathValue = $this->propertyAccessor->getValue($entity, $propertyPath);

Expand Down
4 changes: 2 additions & 2 deletions Service/Repository/RepositoryService.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ public function getEntityById(string $id, string $class): mixed
$repository = $this->em->getRepository($class); // @phpstan-ignore-line

if (!$repository instanceof GettableOneByIdInterface) {
throw new LogicException(\sprintf('Repository %s should implements %s interface', $repository->getClassName(), GettableOneByIdInterface::class));
throw new LogicException(\sprintf('Repository %s should implements %s interface', $repository::class, GettableOneByIdInterface::class));
}

return $repository->getOneById($id);
Expand All @@ -50,7 +50,7 @@ public function findEntityById(string $id, string $class): mixed
$repository = $this->em->getRepository($class); // @phpstan-ignore-line

if (!$repository instanceof FindableByIdInterface) {
throw new LogicException(\sprintf('Repository %s should implements %s interface', $repository->getClassName(), FindableByIdInterface::class));
throw new LogicException(\sprintf('Repository %s should implements %s interface', $repository::class, FindableByIdInterface::class));
}

return $repository->findOneById($id);
Expand Down
39 changes: 39 additions & 0 deletions Tests/Attribute/DependentEntityTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php
/*
* This file is part of the StfalconApiBundle.
*
* (c) Stfalcon LLC <stfalcon.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace StfalconStudio\ApiBundle\Tests\Attribute;

use PHPUnit\Framework\TestCase;
use StfalconStudio\ApiBundle\Attribute\DependentEntity;
use StfalconStudio\ApiBundle\Exception\InvalidArgumentException;

/**
* DependedEntityTest.
*/
final class DependentEntityTest extends TestCase
{
public function testNotEmptyPropertyPath(): void
{
$this->expectException(InvalidArgumentException::class);
$this->expectExceptionMessage('The "propertyPath" parameter can not be empty.');

new DependentEntity(propertyPath: '');
}

public function testGetPropertyPath(): void
{
$propertyPath = 'someField';
$attribute = new DependentEntity(propertyPath: $propertyPath);

self::assertSame($propertyPath, $attribute->getPropertyPath());
}
}
126 changes: 126 additions & 0 deletions Tests/Service/DependentEntity/DependentEntityServiceTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,126 @@
<?php
/*
* This file is part of the StfalconApiBundle.
*
* (c) Stfalcon LLC <stfalcon.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace StfalconStudio\ApiBundle\Tests\Service\DependentEntity;

use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use StfalconStudio\ApiBundle\Service\DependentEntity\DependentEntityService;
use StfalconStudio\ApiBundle\Service\Repository\RepositoryService;
use Symfony\Component\PropertyAccess\PropertyAccessor;

final class DependentEntityServiceTest extends TestCase
{
/** @var PropertyAccessor|MockObject */
private PropertyAccessor|MockObject $propertyAccessor;

/** @var RepositoryService|MockObject */
private RepositoryService|MockObject $repositoryService;

private DependentEntityService $dependentEntityService;

protected function setUp(): void
{
$this->repositoryService = $this->createMock(RepositoryService::class);
$this->propertyAccessor = $this->createMock(PropertyAccessor::class);
$this->dependentEntityService = new DependentEntityService($this->repositoryService);
$this->dependentEntityService->setPropertyAccessor($this->propertyAccessor);
}

protected function tearDown(): void
{
unset(
$this->repositoryService,
$this->dependentEntityService,
$this->propertyAccessor,
);
}

public function testSetDependentEntities(): void
{
$entity = new DummyDependentEntityClass(name: 'some name');

$dependentEntity = $this->createMock(DummyDependentEntityClass::class);

$this->propertyAccessor
->expects(self::once())
->method('getValue')
->with($entity, 'name')
->willReturn('some name')
;

$this->repositoryService
->expects(self::once())
->method('getEntityById')
->with('some name', DummyDependentEntityClass::class)
->willReturn($dependentEntity)
;

$this->propertyAccessor
->expects(self::once())
->method('setValue')
->with($entity, 'dependentEntity', $dependentEntity)
;

$this->dependentEntityService->setDependentEntities($entity);
}

public function testSetNull(): void
{
$entity = new DummyDependentEntityClass(name: 'some name');

$this->propertyAccessor
->expects(self::once())
->method('getValue')
->with($entity, 'name')
->willReturn(null)
;

$this->repositoryService
->expects(self::never())
->method('getEntityById')
;

$this->propertyAccessor
->expects(self::once())
->method('setValue')
->with($entity, 'dependentEntity', null)
;

$this->dependentEntityService->setDependentEntities($entity);
}

public function testEmptyAttribute(): void
{
$entity = new DummyDependentEntityClassEmptyAttribute(name: 'some name');

$this->propertyAccessor
->expects(self::never())
->method('getValue')
->with($entity, 'name')
->willReturn(null)
;

$this->repositoryService
->expects(self::never())
->method('getEntityById')
;

$this->propertyAccessor
->expects(self::never())
->method('setValue')
->with($entity, 'dependentEntity', null)
;

$this->dependentEntityService->setDependentEntities($entity);
}
}
43 changes: 43 additions & 0 deletions Tests/Service/DependentEntity/DummyDependentEntityClass.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
<?php
/*
* This file is part of the StfalconApiBundle.
*
* (c) Stfalcon LLC <stfalcon.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace StfalconStudio\ApiBundle\Tests\Service\DependentEntity;

use StfalconStudio\ApiBundle\Attribute\DependentEntity;
use StfalconStudio\ApiBundle\Service\DependentEntity\DependentEntityInterface;

class DummyDependentEntityClass implements DependentEntityInterface
{
#[DependentEntity(propertyPath: 'name')]
private ?DummyDependentEntityClass $dependentEntity;

public function __construct(private readonly string $name)
{
}

public function getName(): string
{
return $this->name;
}

public function getDependentEntity(): ?DummyDependentEntityClass
{
return $this->dependentEntity;
}

public function setDependentEntity(?DummyDependentEntityClass $dependentEntity): self
{
$this->dependentEntity = $dependentEntity;

return $this;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php
/*
* This file is part of the StfalconApiBundle.
*
* (c) Stfalcon LLC <stfalcon.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace StfalconStudio\ApiBundle\Tests\Service\DependentEntity;

use StfalconStudio\ApiBundle\Service\DependentEntity\DependentEntityInterface;

final class DummyDependentEntityClassEmptyAttribute implements DependentEntityInterface
{
private ?DummyDependentEntityClassEmptyAttribute $dependentEntity;

public function __construct(private readonly string $name)
{
}

public function getName(): string
{
return $this->name;
}

public function getDependentEntity(): ?DummyDependentEntityClassEmptyAttribute
{
return $this->dependentEntity;
}

public function setDependentEntity(?DummyDependentEntityClassEmptyAttribute $dependentEntity): self
{
$this->dependentEntity = $dependentEntity;

return $this;
}
}
29 changes: 29 additions & 0 deletions Tests/Service/Repository/DummyRepository.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<?php
/*
* This file is part of the StfalconApiBundle.
*
* (c) Stfalcon LLC <stfalcon.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace StfalconStudio\ApiBundle\Tests\Service\Repository;

use StfalconStudio\ApiBundle\Service\Repository\FindableByIdInterface;
use StfalconStudio\ApiBundle\Service\Repository\GettableOneByIdInterface;

class DummyRepository implements GettableOneByIdInterface, FindableByIdInterface
{
public function findOneById(string $id): mixed
{
return null;
}

public function getOneById(string $id): mixed
{
return null;
}
}
17 changes: 17 additions & 0 deletions Tests/Service/Repository/DummyRepositoryWithOutInterface.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/*
* This file is part of the StfalconApiBundle.
*
* (c) Stfalcon LLC <stfalcon.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

declare(strict_types=1);

namespace StfalconStudio\ApiBundle\Tests\Service\Repository;

class DummyRepositoryWithOutInterface
{
}
Loading

0 comments on commit cfa4adb

Please sign in to comment.