Skip to content

Commit 20815e6

Browse files
committed
Updated test.
- Tests GET on edit routes
1 parent e8bf724 commit 20815e6

File tree

1 file changed

+93
-18
lines changed

1 file changed

+93
-18
lines changed

app/tests/AdminTest.php

Lines changed: 93 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -9,12 +9,68 @@ class AdminTest extends WebTestCase
99
protected $pool;
1010
protected $router;
1111

12+
protected $verifiablePatterns = array(
13+
'/cmf/content/staticcontent/list',
14+
'/cmf/content/staticcontent/create',
15+
'/cmf/content/staticcontent/{id}/edit',
16+
'/cmf/content/staticcontent/{id}/delete',
17+
'/cmf/block/simpleblock/list',
18+
'/cmf/block/simpleblock/create',
19+
'/cmf/block/simpleblock/{id}/edit',
20+
'/cmf/block/simpleblock/{id}/delete',
21+
'/cmf/block/containerblock/list',
22+
'/cmf/block/containerblock/create',
23+
'/cmf/block/containerblock/{id}/edit',
24+
'/cmf/block/containerblock/{id}/delete',
25+
'/cmf/block/referenceblock/list',
26+
'/cmf/block/referenceblock/create',
27+
'/cmf/block/referenceblock/{id}/edit',
28+
'/cmf/block/referenceblock/{id}/delete',
29+
'/cmf/block/actionblock/list',
30+
'/cmf/block/actionblock/create',
31+
'/cmf/block/actionblock/{id}/edit',
32+
'/cmf/block/actionblock/{id}/delete',
33+
'/cmf/block/imagine/list',
34+
'/cmf/block/imagine/create',
35+
'/cmf/routing/route/list',
36+
'/cmf/routing/route/create',
37+
'/cmf/routing/route/{id}/edit',
38+
'/cmf/routing/route/{id}/delete',
39+
'/cmf/routing/redirectroute/list',
40+
'/cmf/routing/redirectroute/create',
41+
'/cmf/routing/redirectroute/{id}/edit',
42+
'/cmf/routing/redirectroute/{id}/delete',
43+
'/cmf/menu/menu/list',
44+
'/cmf/menu/menu/create',
45+
'/cmf/menu/menu/{id}/edit',
46+
'/cmf/menu/menu/{id}/delete',
47+
'/cmf/menu/menunode/list',
48+
'/cmf/menu/menunode/create',
49+
'/cmf/menu/menunode/{id}/edit',
50+
'/cmf/menu/menunode/{id}/delete',
51+
'/cmf/blog/blog/list',
52+
'/cmf/blog/blog/create',
53+
'/cmf/blog/blog/{id}/edit',
54+
'/cmf/blog/blog/{id}/delete',
55+
'/cmf/blog/post/list',
56+
'/cmf/blog/post/create',
57+
'/cmf/blog/post/{id}/edit',
58+
'/cmf/blog/post/{id}/delete',
59+
'/cmf/simplecms/page/list',
60+
'/cmf/simplecms/page/create',
61+
'/cmf/simplecms/page/{id}/edit',
62+
'/cmf/simplecms/page/{id}/delete',
63+
);
64+
65+
protected $testedPatterns = array();
66+
1267
public function setUp()
1368
{
1469
parent::setUp();
1570
$this->pool = $this->getContainer()->get('sonata.admin.pool');
1671
$this->router = $this->getContainer()->get('router');
1772
$this->client = $this->createClientAuthenticated();
73+
$this->dm = $this->getContainer()->get('doctrine_phpcr.odm.default_document_manager');
1874
}
1975

2076
public function testAdmin()
@@ -29,44 +85,63 @@ public function testAdmin()
2985
foreach ($admins as $admin) {
3086
$this->doTestReachableAdminRoutes($admin);
3187
}
88+
89+
// verify that we have tested everything we wanted to test.
90+
$this->assertEquals($this->verifiablePatterns, $this->testedPatterns);
3291
}
3392

3493
protected function doTestReachableAdminRoutes($admin)
3594
{
3695
$routeCollection = $admin->getRoutes();
96+
$class = $admin->getClass();
97+
$routeParams = array('_locale' => 'en');
3798

3899
foreach ($routeCollection->getElements() as $route) {
39-
try {
40-
$url = $this->router->generate($route->getDefault('_sonata_name'), array(
41-
'_locale' => 'en'
42-
));
43-
} catch (MissingMandatoryParametersException $e) {
44-
// do not try and load pages with parameters, e.g. edit, show, etc.
100+
$requirements = $route->getRequirements();
101+
102+
// fix this one later
103+
if (strpos($route->getPattern(), 'export')) {
45104
continue;
46105
}
47106

48-
$crawler = $this->client->request('GET', $url);
49-
$res = $this->client->getResponse();
50-
$statusCode = $res->getStatusCode();
51-
52-
// hack around apparently mal-defined routes
53-
if ($statusCode != 200) {
54-
$exceptionMessage = $crawler->filter('.text-exception h1')->html();
107+
// these don't all work atm
108+
if (strpos($route->getPattern(), 'show')) {
109+
continue;
110+
}
55111

56-
// we cannot determine if sonata routes need a POST
57-
if (false !== strpos($exceptionMessage, 'POST expected')) {
112+
// do not test POST routes
113+
if (isset($requirements['_method'])) {
114+
if ($requirements['_method'] != 'GET') {
58115
continue;
59116
}
117+
}
60118

61-
// the export route has no _format requirement
62-
if (false !== strpos($exceptionMessage, 'Export in format `` is not allowed')) {
63-
continue;
119+
// if an ID is required, try and find a document to test
120+
if (isset($requirements['id'])) {
121+
if ($document = $this->dm->getRepository($class)->findOneBy(array())) {
122+
$node = $this->dm->getNodeForDocument($document);
123+
$routeParams['id'] = $node->getPath();
124+
} else {
125+
// we should throw an exception here maybe and fix the missing fixtures
64126
}
65127
}
66128

129+
try {
130+
$url = $this->router->generate($route->getDefault('_sonata_name'), $routeParams);
131+
} catch (MissingMandatoryParametersException $e) {
132+
// do not try and load pages with parameters, e.g. edit, show, etc.
133+
continue;
134+
}
135+
136+
$crawler = $this->client->request('GET', $url);
137+
$res = $this->client->getResponse();
138+
$statusCode = $res->getStatusCode();
139+
67140
$this->assertEquals(200, $statusCode, sprintf(
68141
'URL %s returns 200 OK HTTP Code', $url
69142
));
143+
144+
$this->testedPatterns[] = $route->getPattern();
70145
}
71146
}
72147
}

0 commit comments

Comments
 (0)