Skip to content

Testing fixes and improvements #276

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 24, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

funny, for redirects we will link to the redirecting route, right? could confuse a not too savy user but makes sense to me. i can test if redirecting really works as expected.

}

/**
* @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