Commit 3d55dc4e by Lech Sawon

unit test example

parent ff4cf5b9
...@@ -6,18 +6,14 @@ namespace App\Service\Reservation; ...@@ -6,18 +6,14 @@ namespace App\Service\Reservation;
use App\Entity\Reservation; use App\Entity\Reservation;
use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\EntityManagerInterface;
use Psr\Log\LoggerInterface;
final class AddReservation final class AddReservation
{ {
private EntityManagerInterface $manager; private EntityManagerInterface $manager;
private LoggerInterface $logger; public function __construct(EntityManagerInterface $manager)
public function __construct(EntityManagerInterface $manager, LoggerInterface $logger)
{ {
$this->manager = $manager; $this->manager = $manager;
$this->logger = $logger;
} }
......
...@@ -64,7 +64,6 @@ final class ApiExceptionSubscriber implements EventSubscriberInterface ...@@ -64,7 +64,6 @@ final class ApiExceptionSubscriber implements EventSubscriberInterface
$event->setResponse($response); $event->setResponse($response);
} }
if ($exception instanceof NotFoundHttpException) { if ($exception instanceof NotFoundHttpException) {
$response = new JsonResponse( $response = new JsonResponse(
......
<?php
declare(strict_types=1);
namespace App\Tests;
use App\Entity\Reservation;
use App\Service\Reservation\AddReservation;
use PHPUnit\Framework\TestCase;
//uproszczony przykład testu jednstkowego
final class AddReservationTest extends TestCase
{
public function testTest(): void
{
//przygotwanie środowiska (given)
$emSpy = new EntityManagerSpy();
$service = new AddReservation(
$emSpy
);
$reservation = new Reservation();
//wykonanie akcji (then)
$service->add($reservation);
//sprawdzenie warunków
self::assertEquals(1, $emSpy->getFlushCount());
self::assertEquals($reservation, $emSpy->getPersisted()[0]);
self::assertSame($reservation, $emSpy->getPersisted()[0]);
}
}
<?php
declare(strict_types=1);
namespace App\Tests;
use Doctrine\ORM\EntityManagerInterface;
//Nie powinniśmy rubić zaślepki dla nie swojego interfacu
//lepiej by bylo funckje zapisu umieścić w repository
// i zaślepiać interface tego repository
final class EntityManagerSpy implements EntityManagerInterface
{
private array $persisted = [];
private int $flushCount = 0;
public function getPersisted(): array
{
return $this->persisted;
}
public function getFlushCount(): int
{
return $this->flushCount;
}
public function persist($object)
{
$this->persisted[] = $object;
}
public function flush()
{
$this->flushCount++;
}
public function getRepository($className)
{
// TODO: Implement getRepository() method.
}
public function getCache()
{
// TODO: Implement getCache() method.
}
public function getConnection()
{
// TODO: Implement getConnection() method.
}
public function getExpressionBuilder()
{
// TODO: Implement getExpressionBuilder() method.
}
public function beginTransaction()
{
// TODO: Implement beginTransaction() method.
}
public function transactional($func)
{
// TODO: Implement transactional() method.
}
public function commit()
{
// TODO: Implement commit() method.
}
public function rollback()
{
// TODO: Implement rollback() method.
}
public function createQuery($dql = '')
{
// TODO: Implement createQuery() method.
}
public function createNamedQuery($name)
{
// TODO: Implement createNamedQuery() method.
}
public function createNativeQuery($sql, \Doctrine\ORM\Query\ResultSetMapping $rsm)
{
// TODO: Implement createNativeQuery() method.
}
public function createNamedNativeQuery($name)
{
// TODO: Implement createNamedNativeQuery() method.
}
public function createQueryBuilder()
{
// TODO: Implement createQueryBuilder() method.
}
public function getReference($entityName, $id)
{
// TODO: Implement getReference() method.
}
public function getPartialReference($entityName, $identifier)
{
// TODO: Implement getPartialReference() method.
}
public function close()
{
// TODO: Implement close() method.
}
public function copy($entity, $deep = false)
{
// TODO: Implement copy() method.
}
public function lock($entity, $lockMode, $lockVersion = null)
{
// TODO: Implement lock() method.
}
public function getEventManager()
{
// TODO: Implement getEventManager() method.
}
public function getConfiguration()
{
// TODO: Implement getConfiguration() method.
}
public function isOpen()
{
// TODO: Implement isOpen() method.
}
public function getUnitOfWork()
{
// TODO: Implement getUnitOfWork() method.
}
public function getHydrator($hydrationMode)
{
// TODO: Implement getHydrator() method.
}
public function newHydrator($hydrationMode)
{
// TODO: Implement newHydrator() method.
}
public function getProxyFactory()
{
// TODO: Implement getProxyFactory() method.
}
public function getFilters()
{
// TODO: Implement getFilters() method.
}
public function isFiltersStateClean()
{
// TODO: Implement isFiltersStateClean() method.
}
public function hasFilters()
{
// TODO: Implement hasFilters() method.
}
public function getClassMetadata($className)
{
// TODO: Implement getClassMetadata() method.
}
public function find($className, $id)
{
// TODO: Implement find() method.
}
public function remove($object)
{
// TODO: Implement remove() method.
}
public function merge($object)
{
// TODO: Implement merge() method.
}
public function clear($objectName = null)
{
// TODO: Implement clear() method.
}
public function detach($object)
{
// TODO: Implement detach() method.
}
public function refresh($object)
{
// TODO: Implement refresh() method.
}
public function initializeObject($obj)
{
// TODO: Implement initializeObject() method.
}
public function contains($object)
{
// TODO: Implement contains() method.
}
public function __call($name, $arguments)
{
// TODO: Implement @method Mapping\ClassMetadataFactory getMetadataFactory()
// TODO: Implement @method mixed wrapInTransaction(callable $func)
}
public function getMetadataFactory(){}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment