diff --git a/Entity/Redirect.php b/Entity/Redirect.php index b6be293..15c24b4 100644 --- a/Entity/Redirect.php +++ b/Entity/Redirect.php @@ -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 diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 8c39b47..ef18707 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -1,5 +1,7 @@ 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: @@ -7,8 +9,14 @@ services: 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 } diff --git a/Router/RedirectRouter.php b/Router/RedirectRouter.php index fbfc843..e96122c 100644 --- a/Router/RedirectRouter.php +++ b/Router/RedirectRouter.php @@ -1,49 +1,38 @@ 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. * @@ -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; } @@ -83,6 +64,11 @@ public function getContext() */ public function getRouteCollection() { + if (is_null($this->routeCollection)) { + $this->routeCollection = new RouteCollection(); + $this->initRoutes(); + } + return $this->routeCollection; } @@ -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 @@ -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(), + )) + ); + } + } +} \ No newline at end of file diff --git a/Tests/AdminList/RedirectAdminListConfiguratorTest.php b/Tests/AdminList/RedirectAdminListConfiguratorTest.php new file mode 100644 index 0000000..793b02a --- /dev/null +++ b/Tests/AdminList/RedirectAdminListConfiguratorTest.php @@ -0,0 +1,105 @@ +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()); + } +} diff --git a/Tests/Entity/RedirectTest.php b/Tests/Entity/RedirectTest.php new file mode 100644 index 0000000..a8a2796 --- /dev/null +++ b/Tests/Entity/RedirectTest.php @@ -0,0 +1,64 @@ +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()); + } + +} diff --git a/Tests/Form/RedirectAdminTypeTest.php b/Tests/Form/RedirectAdminTypeTest.php new file mode 100644 index 0000000..2246e7b --- /dev/null +++ b/Tests/Form/RedirectAdminTypeTest.php @@ -0,0 +1,63 @@ +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()); + } +} diff --git a/Tests/Router/RedirectRouterTest.php b/Tests/Router/RedirectRouterTest.php new file mode 100644 index 0000000..e747aaa --- /dev/null +++ b/Tests/Router/RedirectRouterTest.php @@ -0,0 +1,113 @@ +repository = $this->getRedirectRepository(); + $this->object = new RedirectRouter($this->repository); + } + + private function getRedirects() + { + if (!isset($this->redirects)) { + $this->redirects = array( + (new Redirect())->setOrigin('test')->setTarget('/target1')->setPermanent(false)->setId(1), + (new Redirect())->setOrigin('test2')->setTarget('/target2')->setPermanent(true)->setId(2) + ); + } + + return $this->redirects; + } + + protected function getRedirectRepository() + { + $repository = $this->getMock('Doctrine\Common\Persistence\ObjectRepository'); + $repository->expects($this->any())->method('findAll')->will($this->returnValue($this->getRedirects())); + + return $repository; + } + + /** + * 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\Router\RedirectRouter::getContext + * @covers Kunstmaan\RedirectBundle\Router\RedirectRouter::setContext + */ + public function testGetSetContext() + { + $context = new RequestContext(); + $this->object->setContext($context); + $this->assertEquals($context, $this->object->getContext()); + } + + /** + * @covers Kunstmaan\RedirectBundle\Router\RedirectRouter::getRouteCollection + */ + public function testGetRouteCollection() + { + $collection = $this->object->getRouteCollection(); + $this->assertEquals(2, $collection->count()); + } + + /** + * @covers Kunstmaan\RedirectBundle\Router\RedirectRouter::generate + * @expectedException \Symfony\Component\Routing\Exception\RouteNotFoundException + */ + public function testGenerate() + { + $this->object->generate('test'); + } + + /** + * @covers Kunstmaan\RedirectBundle\Router\RedirectRouter::match + */ + public function testMatch() + { + $redirect = $this->object->match('/test'); + $this->assertEquals( + array( + '_controller' => 'FrameworkBundle:Redirect:urlRedirect', + 'path' => '/target1', + 'permanent' => false, + '_route' => '_redirect_route_1' + ), + $redirect + ); + } +} diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..89d1c72 --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,34 @@ + + + + + + + ./Tests + + + + + + ./ + + ./Resources + ./vendor + + + + + + + +