-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[TesterBundle] New AutowireServiceMock
- Loading branch information
Showing
5 changed files
with
107 additions
and
13 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
59 changes: 59 additions & 0 deletions
59
packages/tester-bundle/PHPUnit/Extension/SetUpAutowire/AutowireServiceMock.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,59 @@ | ||
<?php | ||
|
||
namespace Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire; | ||
|
||
use Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowireMock; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use PHPUnit\Framework\TestCase; | ||
|
||
#[\Attribute(\Attribute::TARGET_PROPERTY)] | ||
class AutowireServiceMock extends AutowireMock | ||
{ | ||
use KernelTestCaseAutowireDependentTrait; | ||
|
||
public static function getPriority(): int | ||
{ | ||
return 0; | ||
} | ||
|
||
public function __construct(private ?string $serviceId = null) | ||
{ | ||
} | ||
|
||
public function autowire(TestCase $testCase, \ReflectionProperty $reflectionProperty): void | ||
{ | ||
parent::autowire($testCase, $reflectionProperty); | ||
|
||
$value = $reflectionProperty->getValue($testCase); | ||
|
||
$this->getContainer($testCase)->set( | ||
$this->getServiceId($reflectionProperty), | ||
$value | ||
); | ||
} | ||
|
||
private function getServiceId(\ReflectionProperty $reflectionProperty): string | ||
{ | ||
$serviceId = $this->serviceId; | ||
|
||
if ($serviceId) { | ||
return $serviceId; | ||
} | ||
|
||
$type = $reflectionProperty->getType(); | ||
|
||
\assert($type instanceof \ReflectionIntersectionType); | ||
|
||
foreach ($type->getTypes() as $type) { | ||
\assert($type instanceof \ReflectionNamedType); | ||
|
||
if (MockObject::class === $type->getName()) { | ||
continue; | ||
} | ||
|
||
return $type->getName(); | ||
} | ||
|
||
throw new \RuntimeException('Cannot load service id from property '.$reflectionProperty->getName().' of class '.$reflectionProperty->getDeclaringClass()->getName()); | ||
} | ||
} |
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 |
---|---|---|
|
@@ -108,11 +108,13 @@ namespace App\Tests; | |
use App\AServiceInterface; | ||
use App\Entity\User; | ||
use App\MyService; | ||
use App\MyOtherService; | ||
use Draw\Bundle\TesterBundle\Messenger\TransportTester; | ||
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireEntity; | ||
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireLoggerTester; | ||
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireParameter; | ||
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireService; | ||
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireServiceMock; | ||
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireTransportTester; | ||
use Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowiredInterface; | ||
use Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowireMock; | ||
|
@@ -148,6 +150,11 @@ class MyTest extends KernelTestCase implements AutowiredInterface | |
|
||
#[AutowireEntity(['email' => '[email protected]'])] | ||
private User $user; | ||
|
||
// Will create a mock object of MyOtherService and call container->set(MyOtherService::class, $mockObject) | ||
// You can also set the service id to use in the container as the first parameter of the attribute. | ||
#[AutowireServiceMock] | ||
private MyOtherService&MockObject $myOtherService; | ||
} | ||
``` | ||
|
||
|
40 changes: 40 additions & 0 deletions
40
tests/TesterBundle/PHPUnit/Extension/SetUpAutoWire/AutowireServiceMockTest.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,40 @@ | ||
<?php | ||
|
||
namespace App\Tests\TesterBundle\PHPUnit\Extension\SetUpAutoWire; | ||
|
||
use App\Entity\User; | ||
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireClient; | ||
use Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire\AutowireServiceMock; | ||
use Draw\Bundle\TesterBundle\WebTestCase; | ||
use Draw\Component\Tester\PHPUnit\Extension\SetUpAutowire\AutowiredInterface; | ||
use Draw\DoctrineExtra\ORM\EntityHandler; | ||
use PHPUnit\Framework\MockObject\MockObject; | ||
use Symfony\Bundle\FrameworkBundle\KernelBrowser; | ||
|
||
/** | ||
* @internal | ||
*/ | ||
class AutowireServiceMockTest extends WebTestCase implements AutowiredInterface | ||
{ | ||
#[AutowireClient] | ||
private KernelBrowser $client; | ||
|
||
#[AutowireServiceMock] | ||
private EntityHandler&MockObject $entityHandler; | ||
|
||
public function testUsersAction(): void | ||
{ | ||
$this->entityHandler | ||
->expects(static::once()) | ||
->method('findAll') | ||
->with(User::class) | ||
->willReturn([]) | ||
; | ||
|
||
$this->client | ||
->request('GET', '/api/users') | ||
; | ||
|
||
static::assertResponseIsSuccessful(); | ||
} | ||
} |