I am testing a service which essentially is mostly serializing an object and sending it via a service to an external system.
If I create the typical unittest I would mock the response of the serializer and of the service, which contacts the external system. In fact there would be not much left to test except calling a bunch of setter Methods in my object.
The alternative would be using a KernelTestCase and creating a functional test, which would be fine except I don't want to contact the external system, but to use a mock only for this "external" service.
Is there any possibility to achieve this in Symfony 4?Or is there another approach to this?
What I am doing now is the following:
<?phpnamespace App\Tests\Service;use App\Service\MyClassService;use App\Service\ExternalClient\ExternalClient;use JMS\Serializer\Serializer;use JMS\Serializer\SerializerInterface;use Psr\Http\Message\RequestInterface;use Psr\Log\LoggerInterface;use Symfony\Bundle\FrameworkBundle\Test\KernelTestCase;use Symfony\Component\HttpFoundation\Request;class MyClassServiceTest extends KernelTestCase{ /** @var LoggerInterface */ private $logger; /** @var Serializer */ private $serializer; /** @var ExternalClient */ private $externalClient; /** @var RequestInterface */ private $request; /** @var MyClassService */ private $myClassService; public function setUp() { $kernel = self::bootKernel(); $this->logger = $kernel->getContainer()->get(LoggerInterface::class); $this->serializer = $kernel->getContainer()->get(SerializerInterface::class); $this->externalClient = $this->createMock(ExternalClient::class); } public function testPassRegistrationData() { $getParams = ['amount' => '21.56','product_id' => 867,'order_id' => '47t34g','order_item_id' => 2,'email' => 'kiki%40bubu.com', ]; $this->generateMyClassService($getParams); $userInformation = $this->myClassService->passRegistrationData(); var_dump($userInformation); } /** * generateMyClassService * * @param $getParams * * @return MyClass */ private function generateMyClassService($getParams) { $this->request = new Request($getParams, [], [], [], [], [], null); $this->myClassService = new MyClassService( $this->logger, $this->serializer, $this->externalClient, $this->request ); }}
give back this error:
Symfony\Component\DependencyInjection\Exception\RuntimeException: Cannot autowire service "App\Service\MyClassConfirmationService": argument "$request" of method "__construct()" references class "Symfony\Component\HttpFoundation\Request" but no such service exists.