Skip to content

Commit

Permalink
Merge pull request #1 from wimvds/unit-tests
Browse files Browse the repository at this point in the history
corrected / refactored / added unit tests
  • Loading branch information
Roderik van der Veer committed Apr 16, 2014
2 parents 551190e + fc8875d commit b79723b
Show file tree
Hide file tree
Showing 8 changed files with 428 additions and 37 deletions.
2 changes: 1 addition & 1 deletion Entity/Redirect.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
/**
* Redirect
*
* @ORM\Table(name="kunstmaan_redirectbundle_redirect")
* @ORM\Table(name="kuma_redirects")
* @ORM\Entity(repositoryClass="Kunstmaan\RedirectBundle\Repository\RedirectRepository")
*/
class Redirect extends AbstractEntity
Expand Down
10 changes: 9 additions & 1 deletion Resources/config/services.yml
Original file line number Diff line number Diff line change
@@ -1,14 +1,22 @@
parameters:
kunstmaan_redirect.menu.adaptor.class: 'Kunstmaan\RedirectBundle\Helper\Menu\RedirectMenuAdaptor'
kunstmaan_redirect.redirect_repository.class: 'Kunstmaan\RedirectBundle\Repository\RedirectRepository'
kunstmaan_redirect.redirect.class: 'Kunstmaan\RedirectBundle\Entity\Redirect'

services:
kunstmaan_redirect.menu.adaptor:
class: "%kunstmaan_redirect.menu.adaptor.class%"
tags:
- { name: 'kunstmaan_admin.menu.adaptor' }

kunstmaan_redirect.repositories.redirect:
class: "%kunstmaan_redirect.redirect_repository.class%"
factory_service: "doctrine.orm.entity_manager"
factory_method: getRepository
arguments: ["%kunstmaan_redirect.redirect.class%"]

kunstmaan_redirect.redirectrouter:
class: Kunstmaan\RedirectBundle\Router\RedirectRouter
arguments: ["@service_container"]
arguments: ["@kunstmaan_redirect.repositories.redirect"]
tags:
- { name: router, priority: 1 }
74 changes: 39 additions & 35 deletions Router/RedirectRouter.php
Original file line number Diff line number Diff line change
@@ -1,49 +1,38 @@
<?php
namespace Kunstmaan\RedirectBundle\Router;

use Doctrine\Common\Persistence\ObjectRepository;
use Kunstmaan\RedirectBundle\Entity\Redirect;
use Symfony\Bundle\FrameworkBundle\Routing\RedirectableUrlMatcher;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\Routing\Exception\RouteNotFoundException;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\RouterInterface;
use Symfony\Component\Routing\RequestContext;

class RedirectRouter implements RouterInterface {

class RedirectRouter implements RouterInterface
{
/** @var RequestContext */
private $context;

/** @var RouteCollection */
private $routeCollection;
private $routeCollection = null;

/** @var ObjectRepository */
private $redirectRepository;

/**
* The constructor for this service
*
* @param ContainerInterface $container
*/
public function __construct($container)
public function __construct(ObjectRepository $redirectRepository, RequestContext $context = null)
{
$this->container = $container;
$this->routeCollection = new RouteCollection();

$redirects = $this->container->get('doctrine')->getRepository('KunstmaanRedirectBundle:Redirect')->findAll();

foreach($redirects as $redirect){
/** @var Redirect $redirect */

$this->routeCollection->add(
'_redirect_route_'.$redirect->getId(),
new Route($redirect->getOrigin(), array(
'_controller' => 'FrameworkBundle:Redirect:urlRedirect',
'path' => $redirect->getTarget(),
'permanent' => $redirect->isPermanent(),
)));
}
$this->redirectRepository = $redirectRepository;
$this->context = $context ? : new RequestContext();
}


/**
* Sets the request context.
*
Expand All @@ -65,14 +54,6 @@ public function setContext(RequestContext $context)
*/
public function getContext()
{
if (!isset($this->context)) {
/* @var Request $request */
$request = $this->container->get('request');

$this->context = new RequestContext();
$this->context->fromRequest($request);
}

return $this->context;
}

Expand All @@ -83,6 +64,11 @@ public function getContext()
*/
public function getRouteCollection()
{
if (is_null($this->routeCollection)) {
$this->routeCollection = new RouteCollection();
$this->initRoutes();
}

return $this->routeCollection;
}

Expand All @@ -101,8 +87,8 @@ public function getRouteCollection()
*
* If there is no route with the given name, the generator must throw the RouteNotFoundException.
*
* @param string $name The name of the route
* @param mixed $parameters An array of parameters
* @param string $name The name of the route
* @param mixed $parameters An array of parameters
* @param Boolean|string $referenceType The type of reference to be generated (one of the constants)
*
* @return string The generated URL
Expand Down Expand Up @@ -136,9 +122,27 @@ public function generate($name, $parameters = array(), $referenceType = self::AB
*/
public function match($pathinfo)
{
$urlMatcher = new RedirectableUrlMatcher($this->routeCollection, $this->getContext());
$result = $urlMatcher->match($pathinfo);
$urlMatcher = new RedirectableUrlMatcher($this->getRouteCollection(), $this->getContext());
$result = $urlMatcher->match($pathinfo);

return $result;
}

}
private function initRoutes()
{
$redirects = $this->redirectRepository->findAll();

foreach ($redirects as $redirect) {
/** @var Redirect $redirect */

$this->routeCollection->add(
'_redirect_route_' . $redirect->getId(),
new Route($redirect->getOrigin(), array(
'_controller' => 'FrameworkBundle:Redirect:urlRedirect',
'path' => $redirect->getTarget(),
'permanent' => $redirect->isPermanent(),
))
);
}
}
}
105 changes: 105 additions & 0 deletions Tests/AdminList/RedirectAdminListConfiguratorTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
<?php

namespace Kunstmaan\RedirectBundle\Tests\Entity;

use Doctrine\ORM\EntityManager;
use Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper;
use Kunstmaan\RedirectBundle\AdminList\RedirectAdminListConfigurator;

/**
* Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2014-04-16 at 18:05:47.
*/
class RedirectAdminListConfiguratorTest extends \PHPUnit_Framework_TestCase
{
/**
* @var EntityManager
*/
protected $em;

/**
* @var AclHelper
*/
protected $aclHelper;

/**
* @var RedirectAdminListConfigurator
*/
protected $object;

/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->em = $this->getMockBuilder('Doctrine\ORM\EntityManager')
->disableOriginalConstructor()->getMock();
$this->aclHelper = $this->getMockBuilder('Kunstmaan\AdminBundle\Helper\Security\Acl\AclHelper')
->disableOriginalConstructor()->getMock();

$this->object = new RedirectAdminListConfigurator($this->em, $this->aclHelper);
}

/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}

/**
* @covers Kunstmaan\RedirectBundle\AdminList\RedirectAdminListConfigurator::buildFields
*/
public function testBuildFields()
{
$this->object->buildFields();
$fields = $this->object->getFields();
$this->assertEquals(3, count($fields));
$fieldNames = array_map(
function ($field) {
return $field->getName();
},
$fields
);
$this->assertEquals(array('origin', 'target', 'permanent'), $fieldNames);
}

/**
* @covers Kunstmaan\RedirectBundle\AdminList\RedirectAdminListConfigurator::buildFilters
*/
public function testBuildFilters()
{
$filterBuilder = $this->getMock('Kunstmaan\AdminListBundle\AdminList\FilterBuilder');
$filterBuilder
->expects($this->at(0))
->method('add')
->with('origin');
$filterBuilder
->expects($this->at(1))
->method('add')
->with('target');
$filterBuilder
->expects($this->at(2))
->method('add')
->with('permanent');
$this->object->setFilterBuilder($filterBuilder);
$this->object->buildFilters();
}

/**
* @covers Kunstmaan\RedirectBundle\AdminList\RedirectAdminListConfigurator::getBundleName
*/
public function testGetBundleName()
{
$this->assertEquals('KunstmaanRedirectBundle', $this->object->getBundleName());
}

/**
* @covers Kunstmaan\RedirectBundle\AdminList\RedirectAdminListConfigurator::getEntityName
*/
public function testGetEntityName()
{
$this->assertEquals('Redirect', $this->object->getEntityName());
}
}
64 changes: 64 additions & 0 deletions Tests/Entity/RedirectTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
<?php

namespace Kunstmaan\RedirectBundle\Tests\Entity;

use Kunstmaan\RedirectBundle\Entity\Redirect;

/**
* Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2014-04-16 at 17:26:20.
*/
class RedirectTest extends \PHPUnit_Framework_TestCase
{
/**
* @var Redirect
*/
protected $object;

/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->object = new Redirect;
}

/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}

/**
* @covers Kunstmaan\RedirectBundle\Entity\Redirect::getOrigin
* @covers Kunstmaan\RedirectBundle\Entity\Redirect::setOrigin
*/
public function testGetSetOrigin()
{
$this->object->setOrigin('origin');
$this->assertEquals('origin', $this->object->getOrigin());
}

/**
* @covers Kunstmaan\RedirectBundle\Entity\Redirect::getTarget
* @covers Kunstmaan\RedirectBundle\Entity\Redirect::setTarget
*/
public function testGetSetTarget()
{
$this->object->setTarget('target');
$this->assertEquals('target', $this->object->getTarget());
}

/**
* @covers Kunstmaan\RedirectBundle\Entity\Redirect::setPermanent
* @covers Kunstmaan\RedirectBundle\Entity\Redirect::isPermanent
*/
public function testGetSetPermanent()
{
$this->object->setPermanent(true);
$this->assertTrue($this->object->isPermanent());
}

}
63 changes: 63 additions & 0 deletions Tests/Form/RedirectAdminTypeTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
<?php

namespace Kunstmaan\RedirectBundle\Tests\Form;

use Kunstmaan\RedirectBundle\Form\RedirectAdminType;

/**
* Generated by PHPUnit_SkeletonGenerator 1.2.1 on 2014-04-16 at 17:32:30.
*/
class RedirectAdminTypeTest extends \PHPUnit_Framework_TestCase
{
/**
* @var RedirectAdminType
*/
protected $object;

/**
* Sets up the fixture, for example, opens a network connection.
* This method is called before a test is executed.
*/
protected function setUp()
{
$this->object = new RedirectAdminType();
}

/**
* Tears down the fixture, for example, closes a network connection.
* This method is called after a test is executed.
*/
protected function tearDown()
{
}

/**
* @covers Kunstmaan\RedirectBundle\Form\RedirectAdminType::buildForm
*/
public function testBuildForm()
{
$builder = $this->getMock('Symfony\Component\Form\Test\FormBuilderInterface');
$builder
->expects($this->at(0))
->method('add')
->with('origin');
$builder
->expects($this->at(1))
->method('add')
->with('target');
$builder
->expects($this->at(2))
->method('add')
->with('permanent');

$this->object->buildForm($builder, array());
}

/**
* @covers Kunstmaan\RedirectBundle\Form\RedirectAdminType::getName
*/
public function testGetName()
{
$this->assertEquals('redirect_form', $this->object->getName());
}
}
Loading

0 comments on commit b79723b

Please sign in to comment.