Skip to content

Commit

Permalink
Added priority to the annotations.
Browse files Browse the repository at this point in the history
Added sorting (segment before literal) to help eyeball-scans.
Better test coverage.
  • Loading branch information
Saeven committed Aug 10, 2017
1 parent 1c9d22b commit 7c189e4
Show file tree
Hide file tree
Showing 17 changed files with 974 additions and 349 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Spec\CirclicalAutoWire\Controller;

use Spec\CirclicalAutoWire\Form\DummyForm;
use Spec\CirclicalAutoWire\Model\DummyObject;
use Zend\Form\FormElementManager\FormElementManagerV3Polyfill;
use Zend\Mvc\Controller\AbstractActionController;

/**
* Class ControllerWithParameters
* @package Spec\CirclicalAutoWire\Controller
*/
class ControllerWithParameters extends AbstractActionController
{
/**
* ControllerWithParameters constructor.
*
* @param DummyObject $dummyObject
* @param FormElementManagerV3Polyfill $formManager
* @param DummyForm $form This should invoke the FormElementManager
* @param array $config This is a magic name that conjures the ZF config
* @param $formElementManager Magic parameter that injects the FormElementManager
* @param $serviceLocator Should inject the container (bad, but here by popular demand)
*/
public function __construct(DummyObject $dummyObject, FormElementManagerV3Polyfill $formManager, DummyForm $form, array $config, $formElementManager, $serviceLocator)
{

}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
<?php

namespace Spec\CirclicalAutoWire\Factory\Controller;

use CirclicalAutoWire\Factory\Controller\ReflectionFactory;
use CirclicalAutoWire\Model\AnnotatedRoute;
use Interop\Container\ContainerInterface;
use PhpSpec\ObjectBehavior;
use Spec\CirclicalAutoWire\Controller\AnnotatedController;
use Spec\CirclicalAutoWire\Controller\ControllerWithParameters;
use Spec\CirclicalAutoWire\Form\DummyForm;
use Spec\CirclicalAutoWire\Model\DummyObject;
use Zend\Form\FormElementManager\FormElementManagerTrait;
use Zend\Form\FormElementManager\FormElementManagerV3Polyfill;

class ReflectionFactorySpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType(ReflectionFactory::class);
}

function it_can_create_classes_that_end_with_controller(ContainerInterface $interface)
{
$this->canCreate($interface, 'SuperController')->shouldBe(true);
}

function it_only_creates_controllers(ContainerInterface $interface)
{
$this->canCreate($interface, 'SuperFactory')->shouldBe(false);
}

function it_can_create_controllers_through_reflection(ContainerInterface $interface, DummyObject $dummyObject, FormElementManagerV3Polyfill $formManager, DummyForm $form)
{
$formManager->get(DummyForm::class)->willReturn($form);
$interface->get(DummyObject::class)->willReturn($dummyObject);
$interface->get('FormElementManager')->willReturn($formManager);
$interface->get('config')->willReturn([]);
$this->__invoke($interface, ControllerWithParameters::class);
}

function it_creates_parameterless_controllers(ContainerInterface $interface)
{
$this->__invoke($interface, AnnotatedController::class);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php

namespace Spec\CirclicalAutoWire\Factory\Service;

use CirclicalAutoWire\Factory\Service\RouterServiceFactory;
use CirclicalAutoWire\Service\RouterService;
use Interop\Container\ContainerInterface;
use PhpSpec\ObjectBehavior;
use Zend\Router\Http\TreeRouteStack;

class RouterServiceFactorySpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType(RouterServiceFactory::class);
}

function it_creates_router_services(ContainerInterface $container, TreeRouteStack $stack)
{
$container->get('config')->willReturn([
'circlical' => [
'autowire' => [
'production_mode' => false,
],
],
]);
$container->get('HttpRouter')->willReturn($stack);

$this->__invoke($container, RouterService::class)->shouldBeAnInstanceOf(RouterService::class);
}
}
8 changes: 8 additions & 0 deletions bundle/Spec/CirclicalAutoWire/Form/DummyForm.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php

namespace Spec\CirclicalAutoWire\Form;

class DummyForm
{

}
9 changes: 9 additions & 0 deletions bundle/Spec/CirclicalAutoWire/Model/DummyObject.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php

namespace Spec\CirclicalAutoWire\Model;

class DummyObject
{

}

125 changes: 125 additions & 0 deletions bundle/Spec/CirclicalAutoWire/ModuleSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
<?php

namespace Spec\CirclicalAutoWire;

use CirclicalAutoWire\Module;
use CirclicalAutoWire\Service\RouterService;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Psr\Container\ContainerInterface;
use Zend\Console\Console;
use Zend\EventManager\EventManager;
use Zend\ModuleManager\Listener\ConfigListener;
use Zend\ModuleManager\ModuleEvent;
use Zend\ModuleManager\ModuleManager;
use Zend\Mvc\Application;
use Zend\Mvc\MvcEvent;
use Zend\Router\Http\TreeRouteStack;

class ModuleSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType(Module::class);
}

function it_returns_its_config()
{
$this->getConfig()->shouldBeArray();
}

function it_binds_its_events(ModuleManager $moduleManager, EventManager $eventManager)
{
$eventManager->attach(ModuleEvent::EVENT_LOAD_MODULE, Argument::type('array'))->shouldBeCalled();
$eventManager->attach(ModuleEvent::EVENT_MERGE_CONFIG, Argument::type('array'))->shouldBeCalled();
$moduleManager->getEventManager()->willReturn($eventManager);
$this->init($moduleManager);
}

function it_merges_config(ModuleEvent $event, ConfigListener $configListener)
{
$event->getConfigListener()->willReturn($configListener);
$configListener->getMergedConfig(false)->willReturn(include __DIR__ . '/../../../config/module.config.php');
$this->configMerge($event);
}

function it_freaks_out_if_autowire_config_is_missing(ModuleEvent $event, ConfigListener $configListener)
{
$event->getConfigListener()->willReturn($configListener);
$config = include __DIR__ . '/../../../config/module.config.php';
unset($config['circlical']);
$configListener->getMergedConfig(false)->willReturn($config);
$this->shouldThrow(\Exception::class)->during('configMerge', [$event]);
}

function it_sets_routes_in_production_mode_during_config_merge(ModuleEvent $event, ConfigListener $configListener)
{
$event->getConfigListener()->willReturn($configListener);
$config = include __DIR__ . '/../../../config/module.config.php';
$config['circlical']['autowire']['compile_to'] = __DIR__ . '/compiled_routes.php';
$configListener->getMergedConfig(false)->willReturn($config);
$configListener->setMergedConfig(Argument::type('array'))->shouldBeCalled();
$this->configMerge($event);
}

function it_listens_for_loading_modules(ModuleEvent $event1, ModuleEvent $event2)
{
$event1->getModuleName()->willReturn('A');
$event2->getModuleName()->willReturn('B');
$this->moduleLoaded($event1);
$this->moduleLoaded($event2);
}

function it_merges_with_existing_routes_in_production_mode_during_config_merge(ModuleEvent $event, ConfigListener $configListener)
{
$event->getConfigListener()->willReturn($configListener);
$config = include __DIR__ . '/../../../config/module.config.php';
$config['circlical']['autowire']['compile_to'] = __DIR__ . '/compiled_routes.php';
$config['router']['routes'] = [];
$configListener->getMergedConfig(false)->willReturn($config);
$configListener->setMergedConfig(Argument::type('array'))->shouldBeCalled();
$this->configMerge($event);
}

function it_scans_modules_during_bootstrap_in_dev_mode_only(MvcEvent $mvcEvent, Application $application, ContainerInterface $container)
{
$mvcEvent->getApplication()->willReturn($application);
$application->getServiceManager()->willReturn($container);

Console::overrideIsConsole(false);

$container->get('config')->willReturn([
'circlical' => [
'autowire' => [
'production_mode' => false,
'compile_to' => __DIR__ . '/compiled_routes.php',
],
],
]);

$container->get(RouterService::class)->willReturn(new RouterService(new TreeRouteStack(), false));

$this->onBootstrap($mvcEvent);
}

function it_skips_compiling_in_prod_mode(MvcEvent $mvcEvent, Application $application, ContainerInterface $container)
{
$mvcEvent->getApplication()->willReturn($application);
$application->getServiceManager()->willReturn($container);

Console::overrideIsConsole(false);

$container->get('config')->willReturn([
'circlical' => [
'autowire' => [
'production_mode' => true,
'compile_to' => __DIR__ . '/compiled_routes.php',
],
],
]);

$container->get(RouterService::class)->shouldNotBeCalled();

$this->onBootstrap($mvcEvent);
}
}
17 changes: 9 additions & 8 deletions bundle/Spec/CirclicalAutoWire/Service/RouterServiceSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,11 @@ function let(TreeRouteStack $routeStack)

function it_parses_controllers_by_class()
{
include __DIR__ . '/../../CirclicalAutoWire/Controller/SimpleController.php';
$this->parseController(SimpleController::class);
}

function it_parses_controllers_with_annotations($routeStack)
{
include __DIR__ . '/../../CirclicalAutoWire/Controller/AnnotatedController.php';

$routeStack->addRoute('baseroute1', [
'type' => Literal::class,
'options' => [
Expand Down Expand Up @@ -67,8 +64,6 @@ function it_parses_controllers_with_annotations($routeStack)

function it_parses_child_routes($routeStack)
{
include __DIR__ . '/../../CirclicalAutoWire/Controller/ChildRouteController.php';

$routeStack->addRoute('icecream', [
'type' => Literal::class,
'options' => [
Expand Down Expand Up @@ -168,9 +163,6 @@ function it_parses_child_routes_across_controllers($routeStack)

function it_lets_children_have_same_names($routeStack)
{
include __DIR__ . '/../../CirclicalAutoWire/Controller/SameNameAController.php';
include __DIR__ . '/../../CirclicalAutoWire/Controller/SameNameBController.php';

$routeStack->addRoute('foocrud', [
'type' => Literal::class,
'options' => [
Expand Down Expand Up @@ -221,4 +213,13 @@ function it_lets_children_have_same_names($routeStack)
$this->parseController(SameNameBController::class);
$this->compile();
}

function its_annotions_can_be_reset()
{
$this->parseController(SameNameAController::class);
$this->getAnnotations()->shouldHaveCount(2);
$this->reset();
$this->getAnnotations()->shouldBeArray();
$this->getAnnotations()->shouldHaveCount(0);
}
}
10 changes: 10 additions & 0 deletions bundle/Spec/CirclicalAutoWire/bootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
<?php

include __DIR__ . '/Controller/AnnotatedController.php';
include __DIR__ . '/Controller/SimpleController.php';
include __DIR__ . '/Controller/SameNameAController.php';
include __DIR__ . '/Controller/SameNameBController.php';
include __DIR__ . '/Controller/ChildRouteController.php';
include __DIR__ . '/Controller/ControllerWithParameters.php';
include __DIR__ . '/Model/DummyObject.php';
include __DIR__ . '/Form/DummyForm.php';
3 changes: 3 additions & 0 deletions bundle/Spec/CirclicalAutoWire/compiled_routes.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?php
return array(
);
1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
},
"require-dev": {
"phpspec/phpspec": "@stable",
"zendframework/zend-form": "@stable",
"henrikbjorn/phpspec-code-coverage": "@stable",
"codacy/coverage": "dev-master"
},
Expand Down
Loading

0 comments on commit 7c189e4

Please sign in to comment.