Witam
Robię serwis odpowiedzialny za rejestrowanie użytkownika ale mam problem z testowaniem serwisu, gdy próbuje testować serwis to id użytkownika to null, domyślam się że nie może go dodać do bazy ale nie wiem gdzie leży błąd, gdy robię taką samą klasę w serwisie i wykonuje go to wszystko działa i użytkownik dodaje się do bazy, id w assert jest ustawione celowo.
<?php
namespace App\Tests\Services;
use App\Entity\User;
use App\Services\SecurityService;
use Doctrine\ORM\EntityManagerInterface;
use PHPUnit\Framework\TestCase;
class SecurityServiceTest extends TestCase
{
/**
* @var SecurityService
*/
private $securityService;
/**
* @var EntityManagerInterface|\PHPUnit_Framework_MockObject_MockObject
*/
private $entityManagerInterfaceMock;
protected function setUp()
{
$this->entityManagerInterfaceMock = $this->getMockBuilder(EntityManagerInterface::class)
->disableOriginalConstructor()
->getMock();
$this->securityService = new SecurityService(
$this->entityManagerInterfaceMock
);
}
protected function tearDown()
{
$this->securityService = null;
$this->entityManagerInterfaceMock = null;
}
public function testRegister()
{
$user = new User();
$user->setUsername("ExampleUsername");
$user->setPassword("ExamplePassword");
$user->setEmail("ExampleEmail@example.com");
$user->setCreatedAt(new \DateTime());
$this->entityManagerInterfaceMock
->expects($this->once())
->method('persist')
->with($user);
$this->entityManagerInterfaceMock
->expects($this->once())
->method('flush');
$this->assertEquals($user->getId(), 5);
}
}