Skip to content

Commit 330c3ac

Browse files
committed
Merge pull request #276 from frne/sonata-extension-testing
Testing fixes and improvements
2 parents 63560f4 + ff2c76a commit 330c3ac

File tree

7 files changed

+86
-11
lines changed

7 files changed

+86
-11
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ before_script:
2323
- composer self-update
2424
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then echo "memory_limit = -1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini; fi;'
2525
- composer require symfony/symfony:${SYMFONY_VERSION} --prefer-source
26+
- vendor/symfony-cmf/testing/bin/travis/phpcr_odm_doctrine_dbal.sh
2627

2728
script: phpunit --coverage-text
2829

Admin/Extension/FrontendLinkExtension.php

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,13 @@
1414
use Knp\Menu\ItemInterface as MenuItemInterface;
1515
use Sonata\AdminBundle\Admin\AdminExtension;
1616
use Sonata\AdminBundle\Admin\AdminInterface;
17-
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
1817
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\PrefixInterface;
1918
use Symfony\Cmf\Component\Routing\RouteReferrersReadInterface;
2019
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
2120
use Symfony\Component\Routing\Exception\ExceptionInterface as RoutingExceptionInterface;
2221
use Symfony\Component\Routing\Route;
2322
use Symfony\Component\Routing\RouterInterface;
23+
use Symfony\Component\Translation\TranslatorInterface;
2424

2525
/**
2626
* Admin extension to add a frontend link to the edit tab implementing the
@@ -36,20 +36,24 @@ class FrontendLinkExtension extends AdminExtension
3636
private $router;
3737

3838
/**
39-
* @var Translator
39+
* @var TranslatorInterface
4040
*/
4141
private $translator;
4242

4343
/**
4444
* @param RouterInterface $router
45-
* @param Translator $translator
45+
* @param TranslatorInterface $translator
4646
*/
47-
public function __construct(RouterInterface $router, Translator $translator)
47+
public function __construct(RouterInterface $router, TranslatorInterface $translator)
4848
{
4949
$this->router = $router;
5050
$this->translator = $translator;
5151
}
5252

53+
/**
54+
* @return void
55+
* @throws InvalidConfigurationException
56+
*/
5357
public function configureTabMenu(
5458
AdminInterface $admin,
5559
MenuItemInterface $menu,
@@ -71,7 +75,7 @@ public function configureTabMenu(
7175

7276
if ($subject instanceof PrefixInterface && !is_string($subject->getId())) {
7377
// we have an unpersisted dynamic route
74-
return;
78+
return;
7579
}
7680

7781
try {

Tests/Resources/app/config/config_phpcr.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,4 @@
66
$loader->import(CMF_TEST_CONFIG_DIR.'/sonata_admin.php');
77
$loader->import(__DIR__.'/cmf_routing.yml');
88
$loader->import(__DIR__.'/cmf_routing.phpcr.yml');
9+
$loader->import(__DIR__.'/sonata_admin.yml');
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
sonata_admin:
2+
extensions:
3+
cmf_routing.admin_extension.frontend_link:
4+
implements:
5+
- Symfony\Cmf\Component\Routing\RouteReferrersReadInterface
6+
extends:
7+
- Symfony\Component\Routing\Route

Tests/WebTest/RedirectRouteAdminTest.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@
1212
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\WebTest;
1313

1414
use Symfony\Cmf\Component\Testing\Functional\BaseTestCase;
15+
use Symfony\Component\DomCrawler\Crawler;
1516

1617
class RedirectRouteAdminTest extends BaseTestCase
1718
{
19+
/**
20+
* @var \Symfony\Bundle\FrameworkBundle\Client
21+
*/
22+
private $client;
23+
1824
public function setUp()
1925
{
2026
$this->db('PHPCR')->loadFixtures(array(
@@ -37,11 +43,15 @@ public function testRedirectRouteEdit()
3743
$res = $this->client->getResponse();
3844
$this->assertEquals(200, $res->getStatusCode());
3945
$this->assertCount(1, $crawler->filter('input[value="redirect-route-1"]'));
46+
47+
$this->assertFrontendLinkPresent($crawler);
4048
}
4149

4250
public function testRedirectRouteShow()
4351
{
44-
$this->markTestSkipped('Not implemented yet.');
52+
$crawler = $this->client->request('GET', '/admin/cmf/routing/redirectroute/test/routing/redirect-route-1/show');
53+
$res = $this->client->getResponse();
54+
$this->assertEquals(200, $res->getStatusCode());
4555
}
4656

4757
public function testRedirectRouteCreate()
@@ -50,19 +60,38 @@ public function testRedirectRouteCreate()
5060
$res = $this->client->getResponse();
5161
$this->assertEquals(200, $res->getStatusCode());
5262

63+
$this->assertFrontendLinkNotPresent($crawler);
64+
5365
$button = $crawler->selectButton('Create');
5466
$form = $button->form();
5567
$node = $form->getFormNode();
5668
$actionUrl = $node->getAttribute('action');
5769
$uniqId = substr(strchr($actionUrl, '='), 1);
5870

59-
$form[$uniqId.'[parent]'] = '/test/routing';
60-
$form[$uniqId.'[name]'] = 'foo-test';
71+
$form[$uniqId . '[parent]'] = '/test/routing';
72+
$form[$uniqId . '[name]'] = 'foo-test';
6173

6274
$this->client->submit($form);
6375
$res = $this->client->getResponse();
6476

6577
// If we have a 302 redirect, then all is well
6678
$this->assertEquals(302, $res->getStatusCode());
6779
}
80+
81+
/**
82+
* @param Crawler $crawler
83+
*/
84+
private function assertFrontendLinkPresent(Crawler $crawler)
85+
{
86+
$this->assertCount(1, $link = $crawler->filter('a[class="sonata-admin-frontend-link"]'));
87+
$this->assertEquals('/redirect-route-1', $link->attr('href'));
88+
}
89+
90+
/**
91+
* @param Crawler $crawler
92+
*/
93+
private function assertFrontendLinkNotPresent(Crawler $crawler)
94+
{
95+
$this->assertCount(0, $crawler->filter('a[class="sonata-admin-frontend-link"]'));
96+
}
6897
}

Tests/WebTest/RouteAdminTest.php

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,15 @@
1212
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\WebTest;
1313

1414
use Symfony\Cmf\Component\Testing\Functional\BaseTestCase;
15+
use Symfony\Component\DomCrawler\Crawler;
1516

1617
class RouteAdminTest extends BaseTestCase
1718
{
19+
/**
20+
* @var \Symfony\Bundle\FrameworkBundle\Client
21+
*/
22+
private $client;
23+
1824
public function setUp()
1925
{
2026
$this->db('PHPCR')->loadFixtures(array(
@@ -37,11 +43,15 @@ public function testRouteEdit()
3743
$res = $this->client->getResponse();
3844
$this->assertEquals(200, $res->getStatusCode());
3945
$this->assertCount(1, $crawler->filter('input[value="route-1"]'));
46+
47+
$this->assertFrontendLinkPresent($crawler);
4048
}
4149

4250
public function testRouteShow()
4351
{
44-
$this->markTestSkipped('Not implemented yet.');
52+
$crawler = $this->client->request('GET', '/admin/cmf/routing/route/test/routing/route-1/show');
53+
$res = $this->client->getResponse();
54+
$this->assertEquals(200, $res->getStatusCode());
4555
}
4656

4757
public function testRouteCreate()
@@ -50,19 +60,38 @@ public function testRouteCreate()
5060
$res = $this->client->getResponse();
5161
$this->assertEquals(200, $res->getStatusCode());
5262

63+
$this->assertFrontendLinkNotPresent($crawler);
64+
5365
$button = $crawler->selectButton('Create');
5466
$form = $button->form();
5567
$node = $form->getFormNode();
5668
$actionUrl = $node->getAttribute('action');
5769
$uniqId = substr(strchr($actionUrl, '='), 1);
5870

59-
$form[$uniqId.'[parent]'] = '/test/routing';
60-
$form[$uniqId.'[name]'] = 'foo-test';
71+
$form[$uniqId . '[parent]'] = '/test/routing';
72+
$form[$uniqId . '[name]'] = 'foo-test';
6173

6274
$this->client->submit($form);
6375
$res = $this->client->getResponse();
6476

6577
// If we have a 302 redirect, then all is well
6678
$this->assertEquals(302, $res->getStatusCode());
6779
}
80+
81+
/**
82+
* @param Crawler $crawler
83+
*/
84+
private function assertFrontendLinkPresent(Crawler $crawler)
85+
{
86+
$this->assertCount(1, $link = $crawler->filter('a[class="sonata-admin-frontend-link"]'));
87+
$this->assertEquals('/route-1', $link->attr('href'));
88+
}
89+
90+
/**
91+
* @param Crawler $crawler
92+
*/
93+
private function assertFrontendLinkNotPresent(Crawler $crawler)
94+
{
95+
$this->assertCount(0, $crawler->filter('a[class="sonata-admin-frontend-link"]'));
96+
}
6897
}

phpunit.xml.dist

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@
1111
<directory>./Tests/Unit</directory>
1212
</testsuite>
1313

14+
<testsuite name="web-test">
15+
<directory>./Tests/WebTest</directory>
16+
</testsuite>
17+
1418
<testsuite name="phpcr">
1519
<directory>./Tests/Functional</directory>
1620
<exclude>./Tests/Functional/Doctrine/Orm</exclude>

0 commit comments

Comments
 (0)