Answer the question
In order to leave comments, you need to log in
How to test handler?
Hello. How is the handler tested?
There is this one:
public function __construct(
private CityRepository $cityRepository
)
{
}
public function __invoke(CommandInterface $command): void
{
$city = new City(
Id::generate(),
$command->getName(),
$command->getLocation(),
new DateTimeImmutable()
);
$this->cityRepository->add($city);
}
public function setUp(): void
{
BypassFinals::enable();
parent::setUp();
$this->mock = $this->getMockBuilder(CreateCityHandler::class)
->disableOriginalConstructor()
->getMock();
}
public function testCreate() {
$this->mock->__invoke(new CreateCityCommand(
$name = 'Изумрудный город',
1
));
}
Answer the question
In order to leave comments, you need to log in
If you are writing a Unit test, instead of CityRepository, use a mock that will check that exactly the object that you expect came into it.
If we are not talking about pure unit tests (they are confusing), but functional testing (based on the same phpunit functionality), then:
I can't check what got into the database, because I don't know the Id by which the values will get there
/**
* @ORM\Entity(repositoryClass=PostRepository::class)
* @ORM\HasLifecycleCallbacks()
*/
class Post
{
use IdTrait;
use CreatedAtTrait;
// ...
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question