Commit ef323407 authored by Valentin Nivuahc's avatar Valentin Nivuahc

Avancement dans les tests

parent 30774a2d
Pipeline #42595 passed with stage
in 3 minutes 28 seconds
......@@ -3,6 +3,7 @@
namespace App\Tests\Service;
use App\Entity\Customer;
use App\Repository\CustomerRepository;
use App\Service\CustomerService;
use Symfony\Bridge\Doctrine\RegistryInterface;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
......@@ -55,10 +56,10 @@ class CustomerServiceTest extends WebTestCase
*/
public function testGetForm()
{
$vehiculeService = new CustomerService($this->doctrine, $this->formFactory);
$service = new CustomerService($this->doctrine, $this->formFactory);
$vehicule = new Customer();
$form = $vehiculeService->getForm($vehicule, 'edit-customer');
$form = $service->getForm($vehicule, 'edit-customer');
$this->assertEquals($form->getData(), $vehicule);
$this->assertEquals(['id' => 'edit-customer'], $form->getConfig()->getOption('attr'));
......@@ -78,4 +79,132 @@ class CustomerServiceTest extends WebTestCase
$this->assertInstanceOf(SubmitType::class, $elementConfig->getType()->getInnerType());
$this->assertEquals('button.save', $elementConfig->getOption('label'));
}
/**
* @dataProvider dataForSavedEntities
*
* @param string $lastname
* @param string $firstname
*
* @covers \App\Service\CrudService::save()
*/
public function testSave(string $lastname, string $firstname)
{
$service = new CustomerService($this->doctrine, $this->formFactory);
$repository = $this->doctrine->getManager()->getRepository(
Customer::class
);
$defaultCount = $repository->count([]);
$entity = new Customer($lastname, $firstname);
$entity = $service->save($entity);
$this->assertEquals($defaultCount + 1, $repository->count([]));
$this->assertEquals($lastname, $entity->getLastName());
$this->assertEquals($firstname, $entity->getFirstName());
}
/**
* @return array
*/
public function dataForSavedEntities()
{
return [
['', ''],
['Mylastname', ''],
['', 'Myfirstname'],
['Mylastname', 'Myfirstname'],
];
}
/**
* @covers \App\Service\CrudService::remove()
*/
public function testRemove()
{
$service = new CustomerService($this->doctrine, $this->formFactory);
$repository = $this->doctrine->getManager()->getRepository(
Customer::class
);
$defaultCount = $repository->count([]);
$entity = new Customer('Mylastname', 'Myfirstname');
$this->assertEmpty($entity->getId());
$entity = $service->save($entity);
$this->assertEquals($defaultCount + 1, $repository->count([]));
$this->assertNotEmpty($entity->getId());
$service->remove($entity);
$this->assertEquals($defaultCount, $repository->count([]));
$this->assertEmpty($entity->getId());
}
/**
* @covers \App\Service\CrudService::findAll()
* @covers \App\Service\CustomerService::find()
*/
public function testFind()
{
$formFactory = $this->createMock(FormFactoryInterface::class);
$doctrine = $this->getMockForAbstractClass(RegistryInterface::class);
$repository = $this->createMock(CustomerRepository::class);
$expectedCustomer = $this->createMock(Customer::class);
$doctrine
->expects($this->once())
->method('getRepository')
->willReturn($repository);
$repository
->expects($this->once())
->method('find')
->willReturn($expectedCustomer);
$expectedCustomer
->expects($this->once())
->method('getId')
->willReturn(654);
$service = new CustomerService($doctrine, $formFactory);
$customer = $service->find(654);
$this->assertEquals(654, $customer->getId());
}
/**
* @covers \App\Service\CrudService::findAll()
* @covers \App\Service\CustomerService::findAll()
*/
public function testFindAll()
{
$formFactory = $this->createMock(FormFactoryInterface::class);
$doctrine = $this->getMockForAbstractClass(RegistryInterface::class);
$repository = $this->createMock(CustomerRepository::class);
$customerIds = [1, 35, 654];
$expectedCustomers = [];
foreach ($customerIds as $customerId) {
$customer = $this->createMock(Customer::class);
$customer
->expects($this->once())
->method('getId')
->willReturn($customerId);
$expectedCustomers[] = $customer;
}
$doctrine
->expects($this->once())
->method('getRepository')
->willReturn($repository);
$repository
->expects($this->once())
->method('findAll')
->willReturn($expectedCustomers);
$service = new CustomerService($doctrine, $formFactory);
$customerList = $service->findAll();
foreach ($customerList as $index => $customer) {
$this->assertEquals($customerIds[$index], $customer->getId());
}
}
}
......@@ -58,10 +58,10 @@ class VehiculeServiceTest extends WebTestCase
*/
public function testGetForm()
{
$vehiculeService = new VehiculeService($this->doctrine, $this->formFactory);
$service = new VehiculeService($this->doctrine, $this->formFactory);
$vehicule = new Vehicule();
$form = $vehiculeService->getForm($vehicule, 'edit-vehicule');
$form = $service->getForm($vehicule, 'edit-vehicule');
$this->assertEquals($form->getData(), $vehicule);
$this->assertEquals(['id' => 'edit-vehicule'], $form->getConfig()->getOption('attr'));
......@@ -105,17 +105,19 @@ class VehiculeServiceTest extends WebTestCase
*/
public function testSave(array $data, array $expectedData)
{
$vehiculeService = new VehiculeService($this->doctrine, $this->formFactory);
$service = new VehiculeService($this->doctrine, $this->formFactory);
$repository = $this->doctrine->getManager()->getRepository(
Vehicule::class
);
$defaultCount = $repository->count([]);
$vehicule = new Vehicule($data[0], $data[1], $data[2], $data[3]);
$vehicule = $vehiculeService->save($vehicule);
$entity = new Vehicule($data[0], $data[1], $data[2], $data[3]);
$this->assertEmpty($entity->getId());
$entity = $service->save($entity);
$this->assertEquals($defaultCount + 1, $repository->count([]));
$this->assertEquals($expectedData['color'], $vehicule->getColor());
$this->assertNotEmpty($entity->getId());
$this->assertEquals($expectedData['color'], $entity->getColor());
}
/**
......@@ -166,10 +168,10 @@ class VehiculeServiceTest extends WebTestCase
->method('getNbAvailableVehiculesByRange')
->willReturn($expectedNb);
$vehiculeService = new VehiculeService($doctrine, $formFactory);
$service = new VehiculeService($doctrine, $formFactory);
$nb = $vehiculeService->getNbVehiculesAvailable($range);
$this->assertEquals($nb, $expectedNb);
$nb = $service->getNbVehiculesAvailable($range);
$this->assertEquals($expectedNb, $nb);
}
/**
......@@ -208,10 +210,10 @@ class VehiculeServiceTest extends WebTestCase
->method('getNbVehiculesByRange')
->willReturn($expectedNb);
$vehiculeService = new VehiculeService($doctrine, $formFactory);
$service = new VehiculeService($doctrine, $formFactory);
$nb = $vehiculeService->getNbVehiculesByRange($range);
$this->assertEquals($nb, $expectedNb);
$nb = $service->getNbVehiculesByRange($range);
$this->assertEquals($expectedNb, $nb);
}
/**
......
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