-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Co-authored-by: Andrii Tomchyshyn <andrij.tomchyshyn@stfalcon.com>
- Loading branch information
1 parent
4a31b7e
commit cfa4adb
Showing
9 changed files
with
428 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
126
Tests/Service/DependentEntity/DependentEntityServiceTest.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
43
Tests/Service/DependentEntity/DummyDependentEntityClass.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
41 changes: 41 additions & 0 deletions
41
Tests/Service/DependentEntity/DummyDependentEntityClassEmptyAttribute.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
17
Tests/Service/Repository/DummyRepositoryWithOutInterface.php
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
{ | ||
} |
Oops, something went wrong.