Skip to content

Commit

Permalink
Merge pull request #276 from frne/sonata-extension-testing
Browse files Browse the repository at this point in the history
Testing fixes and improvements
  • Loading branch information
dbu committed Nov 24, 2014
2 parents 63560f4 + ff2c76a commit 330c3ac
Show file tree
Hide file tree
Showing 7 changed files with 86 additions and 11 deletions.
1 change: 1 addition & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ before_script:
- composer self-update
- sh -c 'if [ "$TRAVIS_PHP_VERSION" != "hhvm" ]; then echo "memory_limit = -1" >> ~/.phpenv/versions/$(phpenv version-name)/etc/conf.d/travis.ini; fi;'
- composer require symfony/symfony:${SYMFONY_VERSION} --prefer-source
- vendor/symfony-cmf/testing/bin/travis/phpcr_odm_doctrine_dbal.sh

script: phpunit --coverage-text

Expand Down
14 changes: 9 additions & 5 deletions Admin/Extension/FrontendLinkExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,13 @@
use Knp\Menu\ItemInterface as MenuItemInterface;
use Sonata\AdminBundle\Admin\AdminExtension;
use Sonata\AdminBundle\Admin\AdminInterface;
use Symfony\Bundle\FrameworkBundle\Translation\Translator;
use Symfony\Cmf\Bundle\RoutingBundle\Doctrine\Phpcr\PrefixInterface;
use Symfony\Cmf\Component\Routing\RouteReferrersReadInterface;
use Symfony\Component\Config\Definition\Exception\InvalidConfigurationException;
use Symfony\Component\Routing\Exception\ExceptionInterface as RoutingExceptionInterface;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Translation\TranslatorInterface;

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

/**
* @var Translator
* @var TranslatorInterface
*/
private $translator;

/**
* @param RouterInterface $router
* @param Translator $translator
* @param TranslatorInterface $translator
*/
public function __construct(RouterInterface $router, Translator $translator)
public function __construct(RouterInterface $router, TranslatorInterface $translator)
{
$this->router = $router;
$this->translator = $translator;
}

/**
* @return void
* @throws InvalidConfigurationException
*/
public function configureTabMenu(
AdminInterface $admin,
MenuItemInterface $menu,
Expand All @@ -71,7 +75,7 @@ public function configureTabMenu(

if ($subject instanceof PrefixInterface && !is_string($subject->getId())) {
// we have an unpersisted dynamic route
return;
return;
}

try {
Expand Down
1 change: 1 addition & 0 deletions Tests/Resources/app/config/config_phpcr.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@
$loader->import(CMF_TEST_CONFIG_DIR.'/sonata_admin.php');
$loader->import(__DIR__.'/cmf_routing.yml');
$loader->import(__DIR__.'/cmf_routing.phpcr.yml');
$loader->import(__DIR__.'/sonata_admin.yml');
7 changes: 7 additions & 0 deletions Tests/Resources/app/config/sonata_admin.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
sonata_admin:
extensions:
cmf_routing.admin_extension.frontend_link:
implements:
- Symfony\Cmf\Component\Routing\RouteReferrersReadInterface
extends:
- Symfony\Component\Routing\Route
35 changes: 32 additions & 3 deletions Tests/WebTest/RedirectRouteAdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\WebTest;

use Symfony\Cmf\Component\Testing\Functional\BaseTestCase;
use Symfony\Component\DomCrawler\Crawler;

class RedirectRouteAdminTest extends BaseTestCase
{
/**
* @var \Symfony\Bundle\FrameworkBundle\Client
*/
private $client;

public function setUp()
{
$this->db('PHPCR')->loadFixtures(array(
Expand All @@ -37,11 +43,15 @@ public function testRedirectRouteEdit()
$res = $this->client->getResponse();
$this->assertEquals(200, $res->getStatusCode());
$this->assertCount(1, $crawler->filter('input[value="redirect-route-1"]'));

$this->assertFrontendLinkPresent($crawler);
}

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

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

$this->assertFrontendLinkNotPresent($crawler);

$button = $crawler->selectButton('Create');
$form = $button->form();
$node = $form->getFormNode();
$actionUrl = $node->getAttribute('action');
$uniqId = substr(strchr($actionUrl, '='), 1);

$form[$uniqId.'[parent]'] = '/test/routing';
$form[$uniqId.'[name]'] = 'foo-test';
$form[$uniqId . '[parent]'] = '/test/routing';
$form[$uniqId . '[name]'] = 'foo-test';

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

// If we have a 302 redirect, then all is well
$this->assertEquals(302, $res->getStatusCode());
}

/**
* @param Crawler $crawler
*/
private function assertFrontendLinkPresent(Crawler $crawler)
{
$this->assertCount(1, $link = $crawler->filter('a[class="sonata-admin-frontend-link"]'));
$this->assertEquals('/redirect-route-1', $link->attr('href'));
}

/**
* @param Crawler $crawler
*/
private function assertFrontendLinkNotPresent(Crawler $crawler)
{
$this->assertCount(0, $crawler->filter('a[class="sonata-admin-frontend-link"]'));
}
}
35 changes: 32 additions & 3 deletions Tests/WebTest/RouteAdminTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,15 @@
namespace Symfony\Cmf\Bundle\RoutingBundle\Tests\WebTest;

use Symfony\Cmf\Component\Testing\Functional\BaseTestCase;
use Symfony\Component\DomCrawler\Crawler;

class RouteAdminTest extends BaseTestCase
{
/**
* @var \Symfony\Bundle\FrameworkBundle\Client
*/
private $client;

public function setUp()
{
$this->db('PHPCR')->loadFixtures(array(
Expand All @@ -37,11 +43,15 @@ public function testRouteEdit()
$res = $this->client->getResponse();
$this->assertEquals(200, $res->getStatusCode());
$this->assertCount(1, $crawler->filter('input[value="route-1"]'));

$this->assertFrontendLinkPresent($crawler);
}

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

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

$this->assertFrontendLinkNotPresent($crawler);

$button = $crawler->selectButton('Create');
$form = $button->form();
$node = $form->getFormNode();
$actionUrl = $node->getAttribute('action');
$uniqId = substr(strchr($actionUrl, '='), 1);

$form[$uniqId.'[parent]'] = '/test/routing';
$form[$uniqId.'[name]'] = 'foo-test';
$form[$uniqId . '[parent]'] = '/test/routing';
$form[$uniqId . '[name]'] = 'foo-test';

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

// If we have a 302 redirect, then all is well
$this->assertEquals(302, $res->getStatusCode());
}

/**
* @param Crawler $crawler
*/
private function assertFrontendLinkPresent(Crawler $crawler)
{
$this->assertCount(1, $link = $crawler->filter('a[class="sonata-admin-frontend-link"]'));
$this->assertEquals('/route-1', $link->attr('href'));
}

/**
* @param Crawler $crawler
*/
private function assertFrontendLinkNotPresent(Crawler $crawler)
{
$this->assertCount(0, $crawler->filter('a[class="sonata-admin-frontend-link"]'));
}
}
4 changes: 4 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,10 @@
<directory>./Tests/Unit</directory>
</testsuite>

<testsuite name="web-test">
<directory>./Tests/WebTest</directory>
</testsuite>

<testsuite name="phpcr">
<directory>./Tests/Functional</directory>
<exclude>./Tests/Functional/Doctrine/Orm</exclude>
Expand Down

0 comments on commit 330c3ac

Please sign in to comment.