-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
306 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -16,3 +16,4 @@ | |
###< symfony/phpunit-bridge ### | ||
|
||
.idea | ||
/codecoverage |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
]); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 %} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
|
||
} |