Skip to content

Commit

Permalink
[TesterBundle] New AutowireServiceMock
Browse files Browse the repository at this point in the history
  • Loading branch information
mpoiriert committed Nov 21, 2024
1 parent e5cf22f commit 13bb969
Show file tree
Hide file tree
Showing 5 changed files with 107 additions and 13 deletions.
12 changes: 0 additions & 12 deletions app/src/EntityMigration/UserSetCommentNullMigration.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,18 +38,6 @@ public function needMigration(MigrationTargetEntityInterface $entity): bool
return '' !== $entity->getComment();
}

public function countAllThatNeedMigration(): ?int
{
$manager = $this->managerRegistry->getManagerForClass(User::class);
\assert($manager instanceof EntityManagerInterface);

return (int) $manager
->createQuery('SELECT count(user) FROM '.User::class.' user WHERE user.comment != :comment')
->setParameter('comment', '')
->getSingleScalarResult()
;
}

public function createSelectIdQueryBuilder(): QueryBuilder
{
$manager = $this->managerRegistry->getManagerForClass(User::class);
Expand Down
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());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
namespace Draw\Bundle\TesterBundle\PHPUnit\Extension\SetUpAutowire;

use PHPUnit\Framework\TestCase;
use Psr\Container\ContainerInterface;
use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;
use Symfony\Component\DependencyInjection\ContainerInterface;

trait KernelTestCaseAutowireDependentTrait
{
Expand Down
7 changes: 7 additions & 0 deletions packages/tester-bundle/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
```

Expand Down
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();
}
}

0 comments on commit 13bb969

Please sign in to comment.