Skip to content

Commit

Permalink
Front End Testing
Browse files Browse the repository at this point in the history
- test
- controller
- template
  • Loading branch information
wesolowski committed Apr 1, 2021
1 parent 86addf1 commit b9511dc
Show file tree
Hide file tree
Showing 7 changed files with 306 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,4 @@
###< symfony/phpunit-bridge ###

.idea
/codecoverage
3 changes: 3 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
<filter>
<whitelist processUncoveredFilesFromWhitelist="true">
<directory suffix=".php">src</directory>
<exclude>
<file>src/Kernel.php</file>
</exclude>
</whitelist>
</filter>

Expand Down
75 changes: 75 additions & 0 deletions src/Controller/CarController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
<?php declare(strict_types=1);

namespace App\Controller;

use App\Entity\Car;
use App\Entity\CarMake;
use App\Repository\CarMakeRepository;
use App\Repository\CarRepository;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
use Symfony\Component\Routing\Annotation\Route;

class CarController extends AbstractController
{
private CarMakeRepository $carMakeRepository;

private CarRepository $carRepository;

/**
* @param \App\Repository\CarMakeRepository $carMakeRepository
* @param \App\Repository\CarRepository $carRepository
*/
public function __construct(
CarMakeRepository $carMakeRepository,
CarRepository $carRepository
)
{
$this->carMakeRepository = $carMakeRepository;
$this->carRepository = $carRepository;
}

/**
* @Route("/", name="home")
*/
public function index(): Response
{
$makeList = $this->carMakeRepository->findAll();

return $this->render('home.html.twig', [
'makeList' => $makeList,
]);
}

/**
* @Route("/make/{makeId}", name="make")
*/
public function make(int $makeId): Response
{
$make = $this->carMakeRepository->find($makeId);

if (!$make instanceof CarMake) {
throw $this->createNotFoundException('The make does not exist');
}

return $this->render('make.html.twig', [
'make' => $make,
]);
}

/**
* @Route("/car/{carId}", name="car")
*/
public function car(int $carId): Response
{
$car = $this->carRepository->find($carId);
if (!$car instanceof Car) {
throw $this->createNotFoundException('The car does not exist');
}

return $this->render('car.html.twig', [
'car' => $car,
]);
}

}
11 changes: 11 additions & 0 deletions templates/car.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{% extends 'base.html.twig' %}

{% block title %}Detail{% endblock %}

{% block body %}

<h1>{{ car.model }}</h1>
<h2>{{ car.make.name }}</h2>
<h3>{{ car.description }}</h3>

{% endblock %}
15 changes: 15 additions & 0 deletions templates/home.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends 'base.html.twig' %}

{% block title %}Start{% endblock %}

{% block body %}

<h1>Car</h1>

<ul class="car">
{% for make in makeList %}
<li><a href="{{ url('make', {'makeId' : make.id}) }}">{{ make.name }}</a></li>
{% endfor %}
</ul>

{% endblock %}
15 changes: 15 additions & 0 deletions templates/make.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{% extends 'base.html.twig' %}

{% block title %}List{% endblock %}

{% block body %}

<h1>{{ make.name }}</h1>

<ul class="car">
{% for car in make.cars %}
<li><a href="{{ url('car', {'carId' : car.id}) }}">{{ car.model }}</a></li>
{% endfor %}
</ul>

{% endblock %}
186 changes: 186 additions & 0 deletions tests/Acceptance/Controller/CarControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
<?php declare(strict_types=1);

namespace App\Tests\Acceptance\Controller;

use App\Entity\Car;
use App\Entity\CarMake;
use Doctrine\Persistence\ObjectManager;
use Symfony\Bundle\FrameworkBundle\KernelBrowser;
use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;

class CarControllerTest extends WebTestCase
{
private ?ObjectManager $entityManager;

private KernelBrowser $client;

protected function setUp(): void
{
parent::setUp();
$this->client = self::createClient();

$this->entityManager = $this->client->getContainer()
->get('doctrine')
->getManager();
}

protected function tearDown(): void
{
parent::tearDown();

$connection = $this->entityManager->getConnection();

$connection->executeUpdate('DELETE FROM car');
$connection->executeUpdate('ALTER TABLE car AUTO_INCREMENT=0');
$connection->executeUpdate('DELETE FROM car_make');
$connection->executeUpdate('ALTER TABLE car_make AUTO_INCREMENT=0');

$this->entityManager = null;
}

public function testHomePage()
{
$this->createData();
$crawler = $this->client->request(
'GET',
'/'
);
self::assertResponseStatusCodeSame(200);
self::assertSelectorTextContains('h1', 'Car');

$makeList = $crawler->filter('ul.car > li > a');

self::assertCount(2, $makeList);

$bmwInfo = $makeList->getNode(0);
self::assertSame('BMW', $bmwInfo->nodeValue);
self::assertSame('http://localhost/make/1', $bmwInfo->attributes->item(0)->nodeValue);

$audiInfo = $makeList->getNode(1);
self::assertSame('Audi', $audiInfo->nodeValue);
self::assertSame('http://localhost/make/2', $audiInfo->attributes->item(0)->nodeValue);
}

public function testMakeBmwPage()
{
$this->createData();
$crawler = $this->client->request(
'GET',
'/make/1'
);
self::assertResponseStatusCodeSame(200);
self::assertSelectorTextContains('h1', 'BMW');

$makeList = $crawler->filter('ul.car > li > a');

self::assertCount(1, $makeList);

$bmwInfo = $makeList->getNode(0);
self::assertSame('6 Series', $bmwInfo->nodeValue);
self::assertSame('http://localhost/car/1', $bmwInfo->attributes->item(0)->nodeValue);
}

public function testMakeAudiPage()
{
$this->createData();
$crawler = $this->client->request(
'GET',
'/make/2'
);
self::assertResponseStatusCodeSame(200);
self::assertSelectorTextContains('h1', 'Audi');

$makeList = $crawler->filter('ul.car > li > a');

self::assertCount(3, $makeList);

$bmwInfo = $makeList->getNode(0);
self::assertSame('TT', $bmwInfo->nodeValue);
self::assertSame('http://localhost/car/2', $bmwInfo->attributes->item(0)->nodeValue);

$bmwInfo = $makeList->getNode(1);
self::assertSame('A8', $bmwInfo->nodeValue);
self::assertSame('http://localhost/car/3', $bmwInfo->attributes->item(0)->nodeValue);

$bmwInfo = $makeList->getNode(2);
self::assertSame('S4', $bmwInfo->nodeValue);
self::assertSame('http://localhost/car/4', $bmwInfo->attributes->item(0)->nodeValue);
}

public function testMakeNotFound()
{
$this->client->request(
'GET',
'/make/99'
);
self::assertResponseStatusCodeSame(404);
}

public function testAudiTTCarPage()
{
$this->createData();
$this->client->request(
'GET',
'/car/2'
);
self::assertResponseStatusCodeSame(200);
self::assertSelectorTextContains('h1', 'TT');
self::assertSelectorTextContains('h2', 'Audi');
self::assertSelectorTextContains('h3', 'id luctus nec molestie sed justo');
}

public function testCarPageWhenCarNotFound()
{
$this->client->request(
'GET',
'/car/99'
);
self::assertResponseStatusCodeSame(404);
}


private function createData()
{
$data = [
'BMW' =>
[
[
'model' => '6 Series',
'desc' => 'id luctus nec molestie sed justo ',
],
],
'Audi' =>
[
[
'model' => 'TT',
'desc' => 'id luctus nec molestie sed justo',
],
[
'model' => 'A8',
'desc' => 'dictumst morbi vestibulum velit id pretium iaculis diam erat fermentum',
],
[
'model' => 'S4',
'desc' => 'volutpat dui maecenas tristique est',
],
],
];

foreach ($data as $make => $modelList) {
$carMake = new CarMake();
$carMake->setName($make);
foreach ($modelList as $model) {
$car = new Car();
$car->setModel($model['model']);
$car->setDescription($model['desc']);

$carMake->addCar($car);
$this->entityManager->persist($car);
}
$this->entityManager->persist($carMake);
}

$this->entityManager->flush();
}

}

0 comments on commit b9511dc

Please sign in to comment.