Skip to content

Commit db30634

Browse files
committed
added tests for crud testig
1 parent ad8a4fb commit db30634

File tree

2 files changed

+95
-9
lines changed

2 files changed

+95
-9
lines changed

tests/Maker/FunctionalTest.php

+42
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,8 @@ public function getCommandTests()
321321
'SweetFood',
322322
])
323323
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeCrud')
324+
// need for crud web tests
325+
->addExtraDependencies('symfony/css-selector')
324326
->addReplacement(
325327
'phpunit.xml.dist',
326328
'mysql://db_user:[email protected]:3306/db_name',
@@ -332,6 +334,25 @@ public function getCommandTests()
332334
'sqlite:///%kernel.project_dir%/var/app.db'
333335
)
334336
->addPreMakeCommand('php bin/console doctrine:schema:create --env=test')
337+
->assert(function(string $output, string $directory) {
338+
$this->assertFileExists($directory.'/src/Controller/SweetFoodController.php');
339+
$this->assertFileExists($directory.'/src/Form/SweetFoodType.php');
340+
$this->assertFileExists($directory.'/templates/sweet_food/_delete_form.html.twig');
341+
$this->assertFileExists($directory.'/templates/sweet_food/_form.html.twig');
342+
$this->assertFileExists($directory.'/templates/sweet_food/index.html.twig');
343+
$this->assertFileExists($directory.'/templates/sweet_food/new.html.twig');
344+
$this->assertFileExists($directory.'/templates/sweet_food/edit.html.twig');
345+
$this->assertFileExists($directory.'/templates/sweet_food/show.html.twig');
346+
347+
$this->assertContains('created: src/Controller/SweetFoodController.php', $output);
348+
$this->assertContains('created: src/Form/SweetFoodType.php', $output);
349+
$this->assertContains('created: templates/sweet_food/_delete_form.html.twig', $output);
350+
$this->assertContains('created: templates/sweet_food/_form.html.twig', $output);
351+
$this->assertContains('created: templates/sweet_food/index.html.twig', $output);
352+
$this->assertContains('created: templates/sweet_food/new.html.twig', $output);
353+
$this->assertContains('created: templates/sweet_food/edit.html.twig', $output);
354+
$this->assertContains('created: templates/sweet_food/show.html.twig', $output);
355+
})
335356
];
336357

337358
yield 'crud_with_no_base' => [MakerTestDetails::createTest(
@@ -341,6 +362,8 @@ public function getCommandTests()
341362
'SweetFood',
342363
])
343364
->setFixtureFilesPath(__DIR__.'/../fixtures/MakeCrud')
365+
// need for crud web tests
366+
->addExtraDependencies('symfony/css-selector')
344367
->addReplacement(
345368
'phpunit.xml.dist',
346369
'mysql://db_user:[email protected]:3306/db_name',
@@ -353,6 +376,25 @@ public function getCommandTests()
353376
)
354377
->addPreMakeCommand('php bin/console doctrine:schema:create --env=test')
355378
->addPreMakeCommand('rm templates/base.html.twig')
379+
->assert(function(string $output, string $directory) {
380+
$this->assertFileExists($directory.'/src/Controller/SweetFoodController.php');
381+
$this->assertFileExists($directory.'/src/Form/SweetFoodType.php');
382+
$this->assertFileExists($directory.'/templates/sweet_food/_delete_form.html.twig');
383+
$this->assertFileExists($directory.'/templates/sweet_food/_form.html.twig');
384+
$this->assertFileExists($directory.'/templates/sweet_food/index.html.twig');
385+
$this->assertFileExists($directory.'/templates/sweet_food/new.html.twig');
386+
$this->assertFileExists($directory.'/templates/sweet_food/edit.html.twig');
387+
$this->assertFileExists($directory.'/templates/sweet_food/show.html.twig');
388+
389+
$this->assertContains('created: src/Controller/SweetFoodController.php', $output);
390+
$this->assertContains('created: src/Form/SweetFoodType.php', $output);
391+
$this->assertContains('created: templates/sweet_food/_delete_form.html.twig', $output);
392+
$this->assertContains('created: templates/sweet_food/_form.html.twig', $output);
393+
$this->assertContains('created: templates/sweet_food/index.html.twig', $output);
394+
$this->assertContains('created: templates/sweet_food/new.html.twig', $output);
395+
$this->assertContains('created: templates/sweet_food/edit.html.twig', $output);
396+
$this->assertContains('created: templates/sweet_food/show.html.twig', $output);
397+
})
356398
];
357399
}
358400

tests/fixtures/MakeCrud/tests/GeneratedCrudControllerTest.php

+53-9
Original file line numberDiff line numberDiff line change
@@ -9,20 +9,64 @@ class GeneratedCrudControllerTest extends WebTestCase
99
public function testIndexAction()
1010
{
1111
$client = self::createClient();
12-
$client->request('GET', '/sweet/food/');
13-
14-
$this->assertEquals(200, $client->getResponse()->getStatusCode());
12+
$crawler = $client->request('GET', '/sweet/food/');
13+
$this->assertTrue($client->getResponse()->isSuccessful());
1514
$this->assertContains('<!DOCTYPE html>', $client->getResponse()->getContent());
1615
$this->assertContains('SweetFood index', $client->getResponse()->getContent());
17-
}
1816

19-
public function testNewAction()
20-
{
21-
$client = self::createClient();
22-
$client->request('GET', '/sweet/food/new');
17+
$newLink = $crawler->filter('a:contains("Create new")')->eq(0)->link();
2318

24-
$this->assertEquals(200, $client->getResponse()->getStatusCode());
19+
$crawler = $client->click($newLink);
20+
$this->assertTrue($client->getResponse()->isSuccessful());
2521
$this->assertContains('<!DOCTYPE html>', $client->getResponse()->getContent());
2622
$this->assertContains('New SweetFood', $client->getResponse()->getContent());
23+
24+
$newForm = $crawler->selectButton('Save')->form();
25+
$client->submit($newForm, ['sweet_food[title]' => 'Candy']);
26+
$this->assertTrue($client->getResponse()->isRedirect());
27+
28+
$crawler = $client->followRedirect();
29+
$this->assertTrue($client->getResponse()->isSuccessful());
30+
$this->assertContains('<!DOCTYPE html>', $client->getResponse()->getContent());
31+
$this->assertContains('Edit SweetFood', $client->getResponse()->getContent());
32+
$this->assertGreaterThan(0, $crawler->filter('input[type=text]')->count());
33+
$this->assertEquals('Candy', $crawler->filter('input[type=text]')->attr('value'));
34+
35+
$editForm = $crawler->selectButton('Edit')->form();
36+
$client->submit($editForm, ['sweet_food[title]' => 'Candy edited']);
37+
$this->assertTrue($client->getResponse()->isRedirect());
38+
39+
$crawler = $client->followRedirect();
40+
$this->assertTrue($client->getResponse()->isSuccessful());
41+
$this->assertContains('<!DOCTYPE html>', $client->getResponse()->getContent());
42+
$this->assertContains('Edit SweetFood', $client->getResponse()->getContent());
43+
$this->assertGreaterThan(0, $crawler->filter('input[type=text]')->count());
44+
$this->assertEquals('Candy edited', $crawler->filter('input[type=text]')->attr('value'));
45+
46+
$backTolistLink = $crawler->filter('a:contains("back to list")')->eq(0)->link();
47+
48+
$crawler = $client->click($backTolistLink);
49+
$this->assertTrue($client->getResponse()->isSuccessful());
50+
$this->assertContains('<!DOCTYPE html>', $client->getResponse()->getContent());
51+
$this->assertContains('SweetFood index', $client->getResponse()->getContent());
52+
$this->assertContains('Candy edited', $client->getResponse()->getContent());
53+
54+
$showLink = $crawler->filter('a:contains("show")')->eq(0)->link();
55+
56+
$crawler = $client->click($showLink);
57+
$this->assertTrue($client->getResponse()->isSuccessful());
58+
$this->assertContains('<!DOCTYPE html>', $client->getResponse()->getContent());
59+
$this->assertContains('SweetFood', $client->getResponse()->getContent());
60+
$this->assertContains('Candy edited', $client->getResponse()->getContent());
61+
62+
$deleteForm = $crawler->selectButton('Delete')->form();
63+
$client->submit($deleteForm);
64+
$this->assertTrue($client->getResponse()->isRedirect());
65+
66+
$client->followRedirect();
67+
$this->assertTrue($client->getResponse()->isSuccessful());
68+
$this->assertContains('<!DOCTYPE html>', $client->getResponse()->getContent());
69+
$this->assertContains('SweetFood index', $client->getResponse()->getContent());
70+
$this->assertContains('no records found', $client->getResponse()->getContent());
2771
}
2872
}

0 commit comments

Comments
 (0)