From 851e7e345d4afe03f90a4fbadcd7862709ee7d2d Mon Sep 17 00:00:00 2001 From: Emanuele Minotto Date: Fri, 1 Apr 2016 00:00:34 +0200 Subject: [PATCH 1/9] CS fixer configuration --- .php_cs | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) create mode 100644 .php_cs diff --git a/.php_cs b/.php_cs new file mode 100644 index 0000000..d3e500e --- /dev/null +++ b/.php_cs @@ -0,0 +1,20 @@ +level(Symfony\CS\FixerInterface::SYMFONY_LEVEL) + ->fixers([ + 'ordered_use', + 'phpdoc_order', + 'short_array_syntax', + ]); + +if (null === $input->getArgument('path')) { + $config + ->finder( + Symfony\CS\Finder\DefaultFinder::create() + ->exclude('Resources') + ->in(__DIR__) + ); +} + +return $config; From 8f9337b5b3d2402fb779db6c4263e97480c1a2be Mon Sep 17 00:00:00 2001 From: Emanuele Minotto Date: Fri, 1 Apr 2016 00:01:06 +0200 Subject: [PATCH 2/9] Update dependencies --- composer.json | 19 +++++++++++-------- 1 file changed, 11 insertions(+), 8 deletions(-) diff --git a/composer.json b/composer.json index 6ffc519..8ed6109 100644 --- a/composer.json +++ b/composer.json @@ -10,22 +10,25 @@ } ], "require": { - "php": ">=5.3.3", - "symfony/framework-bundle": "~2.3", - "symfony/security": "~2.3", - "symfony/twig-bundle": "~2.3", - "doctrine/orm": "~2.2,>=2.2.3", + "php": "^5.4||^7.0", + "symfony/framework-bundle": "^2.3||^3.0", + "symfony/security": "^2.3||^3.0", + "symfony/twig-bundle": "^2.3||^3.0", + "doctrine/orm": "^2.2.3", "friendsofsymfony/user-bundle": "1.3.*", - "sonata-project/admin-bundle": "2.3.x-dev", - "sonata-project/doctrine-orm-admin-bundle": "2.3.x-dev" + "sonata-project/admin-bundle": "^2.3.4", + "sonata-project/doctrine-orm-admin-bundle": "^2.3.1" }, "autoload": { + "exclude-from-classmap": [ + "/Tests/" + ], "psr-4": { "Ae\\FeatureBundle\\": "" } }, "require-dev": { - "phpunit/phpunit": "^4.8|^5.1", "matthiasnoback/symfony-dependency-injection-test": "^0.7.6" + "phpunit/phpunit": "^4.8||^5.0", } } From 7904d0e525b35fa540b5d6eb5962bbba356385dd Mon Sep 17 00:00:00 2001 From: Emanuele Minotto Date: Fri, 1 Apr 2016 00:01:36 +0200 Subject: [PATCH 3/9] Include CS check --- .travis.yml | 7 +++++-- composer.json | 3 ++- 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7f08b35..7ca7df5 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,6 +8,8 @@ cache: matrix: include: + - php: 5.4 + env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest"' - php: 5.4 - php: 5.5 - php: 5.6 @@ -23,10 +25,11 @@ before_install: - 'if [[ -n "$GH_TOKEN" ]]; then composer config github-oauth.github.com ${GH_TOKEN}; fi;' - 'if [[ -n "$GH_TOKEN" ]]; then composer config preferred-install dist; fi;' -install: composer install --no-interaction --no-progress +install: 'composer update ${COMPOSER_FLAGS} --no-progress' script: - - phpunit -v + - ./vendor/bin/php-cs-fixer fix --diff --dry-run + - ./vendor/bin/phpunit -v - 'if [[ $TRAVIS_PULL_REQUEST == "false" && $TRAVIS_BRANCH == "master" && $TRAVIS_PHP_VERSION == "5.6" ]]; then sh generate-api.sh; fi;' notifications: diff --git a/composer.json b/composer.json index 8ed6109..334b84c 100644 --- a/composer.json +++ b/composer.json @@ -28,7 +28,8 @@ } }, "require-dev": { - "matthiasnoback/symfony-dependency-injection-test": "^0.7.6" "phpunit/phpunit": "^4.8||^5.0", + "matthiasnoback/symfony-dependency-injection-test": "^0.7.6", + "fabpot/php-cs-fixer": "^1.11" } } From 937c87a297ad505420db4338aad0af124fd317e2 Mon Sep 17 00:00:00 2001 From: Emanuele Minotto Date: Fri, 1 Apr 2016 00:50:29 +0200 Subject: [PATCH 4/9] Remove useless lines --- .gitignore | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/.gitignore b/.gitignore index 73124a0..5414c2c 100644 --- a/.gitignore +++ b/.gitignore @@ -1,5 +1,3 @@ composer.lock phpunit.xml -Tests/autoload.php - -vendor/* +vendor/ From 97e331322d759155474aa62b5ec045c03c0cde3e Mon Sep 17 00:00:00 2001 From: Emanuele Minotto Date: Fri, 1 Apr 2016 00:53:02 +0200 Subject: [PATCH 5/9] Improve documentation --- Admin/FeatureAdmin.php | 9 +++++++++ Command/LoadFeatureCommand.php | 3 +++ DependencyInjection/AeFeatureExtension.php | 2 +- Entity/FeatureManager.php | 10 +++++----- Security/FeatureSecurity.php | 7 +++++-- Tests/DependencyInjection/AeFeatureExtensionTest.php | 3 ++- Tests/Entity/FeatureManagerTest.php | 1 + Tests/Entity/FeatureTest.php | 8 ++------ Tests/Security/FeatureSecurityTest.php | 1 + Tests/Service/FeatureTest.php | 1 + Tests/Twig/IntegrationTest.php | 1 + Tests/Twig/Node/FeatureNodeTest.php | 5 +---- Twig/Extension/FeatureExtension.php | 12 ++++++++---- Twig/Node/FeatureNode.php | 7 ------- Twig/TokenParser/FeatureTokenParser.php | 4 ++-- 15 files changed, 42 insertions(+), 32 deletions(-) diff --git a/Admin/FeatureAdmin.php b/Admin/FeatureAdmin.php index d6b09df..093040f 100644 --- a/Admin/FeatureAdmin.php +++ b/Admin/FeatureAdmin.php @@ -13,6 +13,9 @@ */ class FeatureAdmin extends Admin { + /** + * {@inheritdoc} + */ protected function configureDatagridFilters(DatagridMapper $datagridMapper) { $datagridMapper @@ -21,6 +24,9 @@ protected function configureDatagridFilters(DatagridMapper $datagridMapper) ; } + /** + * {@inheritdoc} + */ protected function configureListFields(ListMapper $listMapper) { $listMapper @@ -37,6 +43,9 @@ protected function configureListFields(ListMapper $listMapper) ; } + /** + * {@inheritdoc} + */ protected function configureFormFields(FormMapper $formMapper) { $roles = $this->getRoles(); diff --git a/Command/LoadFeatureCommand.php b/Command/LoadFeatureCommand.php index b68d61c..3961b41 100644 --- a/Command/LoadFeatureCommand.php +++ b/Command/LoadFeatureCommand.php @@ -15,6 +15,9 @@ */ class LoadFeatureCommand extends ContainerAwareCommand { + /** + * {@inheritdoc} + */ protected function configure() { $this diff --git a/DependencyInjection/AeFeatureExtension.php b/DependencyInjection/AeFeatureExtension.php index 0538949..4e63c4e 100644 --- a/DependencyInjection/AeFeatureExtension.php +++ b/DependencyInjection/AeFeatureExtension.php @@ -15,7 +15,7 @@ class AeFeatureExtension extends Extension { /** - * {@inheritDoc} + * {@inheritdoc} */ public function load(array $configs, ContainerBuilder $container) { diff --git a/Entity/FeatureManager.php b/Entity/FeatureManager.php index e36d503..6e8062c 100644 --- a/Entity/FeatureManager.php +++ b/Entity/FeatureManager.php @@ -11,7 +11,7 @@ class FeatureManager { /** - * @avr \Doctrine\ORM\EntityManager + * @var EntityManager */ protected $em; @@ -21,7 +21,7 @@ public static function generateCacheKey($parentName, $name) } /** - * @param \Doctrine\ORM\EntityManager $em + * @param EntityManager $em */ public function __construct(EntityManager $em) { @@ -48,7 +48,7 @@ public function find($name, $parent) /** * @param string $name Feature name * - * @return Ae\FeatureBundle\Entity\Feature + * @return Feature */ public function findParent($name) { @@ -99,8 +99,8 @@ public function create($name, Feature $parent = null) } /** - * @param \Ae\FeatureBundle\Entity\Feature $feature - * @param bool $andFlush + * @param Feature $feature + * @param bool $andFlush */ public function update(Feature $feature, $andFlush = true) { diff --git a/Security/FeatureSecurity.php b/Security/FeatureSecurity.php index a57eb6d..d90217c 100644 --- a/Security/FeatureSecurity.php +++ b/Security/FeatureSecurity.php @@ -12,10 +12,13 @@ */ class FeatureSecurity { + /** + * @param SecurityContextInterface|null + */ protected $context; /** - * @param \Symfony\Component\Security\Core\SecurityContextInterface $context + * @param SecurityContextInterface $context */ public function __construct(SecurityContextInterface $context = null) { @@ -23,7 +26,7 @@ public function __construct(SecurityContextInterface $context = null) } /** - * @param \Ae\FeatureBundle\Entity\Feature $feature + * @param Feature $feature * * @return bool */ diff --git a/Tests/DependencyInjection/AeFeatureExtensionTest.php b/Tests/DependencyInjection/AeFeatureExtensionTest.php index 3614e47..e7e2252 100644 --- a/Tests/DependencyInjection/AeFeatureExtensionTest.php +++ b/Tests/DependencyInjection/AeFeatureExtensionTest.php @@ -6,7 +6,8 @@ use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase; /** - * @covers \Ae\FeatureBundle\DependencyInjection\AeFeatureExtension + * @author Emanuele Minotto + * @covers Ae\FeatureBundle\DependencyInjection\AeFeatureExtension */ class AeFeatureExtensionTest extends AbstractExtensionTestCase { diff --git a/Tests/Entity/FeatureManagerTest.php b/Tests/Entity/FeatureManagerTest.php index bad509a..bade8e4 100644 --- a/Tests/Entity/FeatureManagerTest.php +++ b/Tests/Entity/FeatureManagerTest.php @@ -6,6 +6,7 @@ /** * @author Carlo Forghieri + * @covers Ae\FeatureBundle\Entity\FeatureManager */ class FeatureManagerTest extends \PHPUnit_Framework_TestCase { diff --git a/Tests/Entity/FeatureTest.php b/Tests/Entity/FeatureTest.php index 8d8092b..9836250 100644 --- a/Tests/Entity/FeatureTest.php +++ b/Tests/Entity/FeatureTest.php @@ -6,6 +6,7 @@ /** * @author Carlo Forghieri + * @covers Ae\FeatureBundle\Entity\Feature */ class FeatureTest extends \PHPUnit_Framework_TestCase { @@ -17,8 +18,7 @@ protected function setUp() } /** - * @covers Ae\FeatureBundle\Entity\Feature::getParent - * @covers Ae\FeatureBundle\Entity\Feature::setParent + * Test parent getter & setter. */ public function testParent() { @@ -27,10 +27,6 @@ public function testParent() $this->assertEquals($parent, $this->entity->getParent()); } - /** - * @covers Ae\FeatureBundle\Entity\Feature::addFeature - * @covers Ae\FeatureBundle\Entity\Feature::getChildren - */ public function testChildren() { $parent = $this->getMock('Ae\FeatureBundle\Entity\Feature'); diff --git a/Tests/Security/FeatureSecurityTest.php b/Tests/Security/FeatureSecurityTest.php index d73b9d5..54cad93 100644 --- a/Tests/Security/FeatureSecurityTest.php +++ b/Tests/Security/FeatureSecurityTest.php @@ -6,6 +6,7 @@ /** * @author Carlo Forghieri + * @covers Ae\FeatureBundle\Security\FeatureSecurity */ class FeatureSecurityTest extends \PHPUnit_Framework_TestCase { diff --git a/Tests/Service/FeatureTest.php b/Tests/Service/FeatureTest.php index 5880560..897b198 100644 --- a/Tests/Service/FeatureTest.php +++ b/Tests/Service/FeatureTest.php @@ -6,6 +6,7 @@ /** * @author Carlo Forghieri + * @covers Ae\FeatureBundle\Service\Feature */ class FeatureTest extends \PHPUnit_Framework_TestCase { diff --git a/Tests/Twig/IntegrationTest.php b/Tests/Twig/IntegrationTest.php index 1cbd30a..adde8f6 100644 --- a/Tests/Twig/IntegrationTest.php +++ b/Tests/Twig/IntegrationTest.php @@ -6,6 +6,7 @@ /** * @author Carlo Forghieri + * @covers Ae\FeatureBundle\Twig\Node\FeatureNode */ class IntegrationTest extends \Twig_Test_IntegrationTestCase { diff --git a/Tests/Twig/Node/FeatureNodeTest.php b/Tests/Twig/Node/FeatureNodeTest.php index 12207fb..8c041c4 100644 --- a/Tests/Twig/Node/FeatureNodeTest.php +++ b/Tests/Twig/Node/FeatureNodeTest.php @@ -6,12 +6,10 @@ /** * @author Carlo Forghieri + * @covers Ae\FeatureBundle\Twig\Node\FeatureNode */ class FeatureNodeTest extends \Twig_Test_NodeTestCase { - /** - * @covers Ae\FeatureBundle\Twig\Node\FeatureNode::__construct - */ public function testConstructor() { $name = 'foo'; @@ -27,7 +25,6 @@ public function testConstructor() } /** - * @covers Ae\FeatureBundle\Twig\Node\FeatureNode::compile * @dataProvider getTests */ public function testCompile($node, $source, $environment = null, $isPattern = null) diff --git a/Twig/Extension/FeatureExtension.php b/Twig/Extension/FeatureExtension.php index 022b433..430d11c 100644 --- a/Twig/Extension/FeatureExtension.php +++ b/Twig/Extension/FeatureExtension.php @@ -10,10 +10,13 @@ */ class FeatureExtension extends \Twig_Extension { + /** + * @var Feature + */ protected $service; /** - * @param \Ae\FeatureBundle\Service\Feature $service + * @param Feature $service */ public function __construct(Feature $service) { @@ -21,9 +24,7 @@ public function __construct(Feature $service) } /** - * Returns the token parser instance to add to the existing list. - * - * @return array An array of Twig_TokenParser instances + * {@inheritdoc} */ public function getTokenParsers() { @@ -32,6 +33,9 @@ public function getTokenParsers() ); } + /** + * {@inheritdoc} + */ public function getName() { return 'feature'; diff --git a/Twig/Node/FeatureNode.php b/Twig/Node/FeatureNode.php index 08fa9b9..1989307 100644 --- a/Twig/Node/FeatureNode.php +++ b/Twig/Node/FeatureNode.php @@ -7,10 +7,6 @@ */ class FeatureNode extends \Twig_Node_If { - /** - * @param int $lineno - * @param string $tag - */ public function __construct($name, $parent, $body, $else, $lineno, $tag = null) { $tests = new \Twig_Node(array( @@ -21,9 +17,6 @@ public function __construct($name, $parent, $body, $else, $lineno, $tag = null) parent::__construct($tests, $else, $lineno, $tag = null); } - /** - * @param integer $lineno - */ protected function createExpression($name, $parent, $lineno) { return new \Twig_Node_Expression_MethodCall( diff --git a/Twig/TokenParser/FeatureTokenParser.php b/Twig/TokenParser/FeatureTokenParser.php index 1d9ca3c..d6721d7 100644 --- a/Twig/TokenParser/FeatureTokenParser.php +++ b/Twig/TokenParser/FeatureTokenParser.php @@ -12,9 +12,9 @@ class FeatureTokenParser extends \Twig_TokenParser /** * Parses a token and returns a node. * - * @param \Twig_Token $token A Twig_Token instance + * @param Twig_Token $token A Twig_Token instance. * - * @return \Twig_NodeInterface A Twig_NodeInterface instance + * @return \Twig_NodeInterface A Twig_NodeInterface instance. */ public function parse(\Twig_Token $token) { From f36c8839a07a01c2b036a8be5109b03032364975 Mon Sep 17 00:00:00 2001 From: Emanuele Minotto Date: Fri, 1 Apr 2016 00:53:52 +0200 Subject: [PATCH 6/9] Fix coding style --- Admin/FeatureAdmin.php | 78 +++++++++++-------- Command/LoadFeatureCommand.php | 74 +++++++++++++----- Entity/FeatureManager.php | 12 +-- Security/FeatureSecurity.php | 2 +- Service/Feature.php | 26 +++++-- .../AeFeatureExtensionTest.php | 51 ++++++++---- Tests/Entity/FeatureManagerTest.php | 21 +++-- Tests/Entity/FeatureTest.php | 8 +- Tests/Security/FeatureSecurityTest.php | 58 ++++++++------ Tests/Service/FeatureTest.php | 45 ++++++----- Tests/Twig/IntegrationTest.php | 55 +++++-------- Tests/Twig/Node/FeatureNodeTest.php | 46 ++++++----- Twig/Extension/FeatureExtension.php | 9 ++- Twig/Node/FeatureNode.php | 33 ++++---- Twig/TokenParser/FeatureTokenParser.php | 64 ++++++++++----- 15 files changed, 358 insertions(+), 224 deletions(-) diff --git a/Admin/FeatureAdmin.php b/Admin/FeatureAdmin.php index 093040f..5fdba83 100644 --- a/Admin/FeatureAdmin.php +++ b/Admin/FeatureAdmin.php @@ -2,11 +2,11 @@ namespace Ae\FeatureBundle\Admin; +use Ae\FeatureBundle\Entity\FeatureManager; use Sonata\AdminBundle\Admin\Admin; -use Sonata\AdminBundle\Datagrid\ListMapper; use Sonata\AdminBundle\Datagrid\DatagridMapper; +use Sonata\AdminBundle\Datagrid\ListMapper; use Sonata\AdminBundle\Form\FormMapper; -use Ae\FeatureBundle\Entity\FeatureManager; /** * @author Carlo Forghieri @@ -19,9 +19,8 @@ class FeatureAdmin extends Admin protected function configureDatagridFilters(DatagridMapper $datagridMapper) { $datagridMapper - ->add('name') - ->add('enabled') - ; + ->add('name') + ->add('enabled'); } /** @@ -34,13 +33,12 @@ protected function configureListFields(ListMapper $listMapper) ->add('name') ->add('role') ->add('enabled') - ->add('_action', 'actions', array( - 'actions' => array( - 'edit' => array(), - 'delete' => array(), - ), - )) - ; + ->add('_action', 'actions', [ + 'actions' => [ + 'edit' => [], + 'delete' => [], + ], + ]); } /** @@ -51,35 +49,41 @@ protected function configureFormFields(FormMapper $formMapper) $roles = $this->getRoles(); $formMapper - ->add('name', 'text', array( + ->add('name', 'text', [ 'required' => true, - )) - ->add('enabled', 'checkbox', array( + ]) + ->add('enabled', 'checkbox', [ 'required' => false, - )) - ->add('role', 'choice', array( - 'choices' => array_combine($roles, $roles), + ]) + ->add('role', 'choice', [ + 'choices' => array_combine($roles, $roles), 'multiple' => false, 'required' => false, - )) - ; + ]); + if (!$this->getSubject()->getParent()) { - $formMapper - ->add('children', 'sonata_type_collection', array( - 'required' => false, - ), array( - 'edit' => 'inline', - 'inline' => 'table', - )) - ; + $formMapper->add( + 'children', + 'sonata_type_collection', + [ + 'required' => false, + ], + [ + 'edit' => 'inline', + 'inline' => 'table', + ] + ); } } protected function getRoles() { - $roleHierarchy = $this->getConfigurationPool()->getContainer()->getParameter('security.role_hierarchy.roles'); + $roleHierarchy = $this + ->getConfigurationPool() + ->getContainer() + ->getParameter('security.role_hierarchy.roles'); - $roles = array_keys($roleHierarchy); + $roles = array_keys($roleHierarchy); $roles[] = 'ROLE_PREVIOUS_ADMIN'; return $roles; @@ -89,7 +93,8 @@ public function createQuery($context = 'list') { $query = parent::createQuery($context); if ($context === 'list') { - $query->andWhere(current($query->getDQLPart('from'))->getAlias().'.parent IS NULL'); + $alias = current($query->getDQLPart('from'))->getAlias(); + $query->andWhere($alias.'.parent IS NULL'); } return $query; @@ -97,7 +102,11 @@ public function createQuery($context = 'list') public function postUpdate($object) { - $cache = $this->modelManager->getEntityManager($object)->getConfiguration()->getResultCacheImpl(); + $cache = $this->modelManager + ->getEntityManager($object) + ->getConfiguration() + ->getResultCacheImpl(); + foreach ($object->getChildren() as $child) { $cache->delete($this->getObjectCacheKey($child)); } @@ -105,6 +114,9 @@ public function postUpdate($object) protected function getObjectCacheKey($object) { - return FeatureManager::generateCacheKey($object->getParent()->getName(), $object->getName()); + return FeatureManager::generateCacheKey( + $object->getParent()->getName(), + $object->getName() + ); } } diff --git a/Command/LoadFeatureCommand.php b/Command/LoadFeatureCommand.php index 3961b41..a1aeddf 100644 --- a/Command/LoadFeatureCommand.php +++ b/Command/LoadFeatureCommand.php @@ -2,13 +2,16 @@ namespace Ae\FeatureBundle\Command; +use Ae\FeatureBundle\Twig\Node\FeatureNode; +use Exception; +use InvalidArgumentException; use Symfony\Bundle\FrameworkBundle\Command\ContainerAwareCommand; use Symfony\Component\Console\Input\InputArgument; -use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Input\InputInterface; +use Symfony\Component\Console\Input\InputOption; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Finder\Finder; -use Ae\FeatureBundle\Twig\Node\FeatureNode; +use Twig_Node; /** * @author Carlo Forghieri @@ -23,57 +26,88 @@ protected function configure() $this ->setName('features:load') ->setDescription('Persist new features found in templates') - ->setDefinition(array( - new InputArgument('bundle', InputArgument::REQUIRED, 'The bundle where to load the features'), - new InputOption('dry-run', null, InputOption::VALUE_NONE, 'Do not persist new features'), - )); + ->addArgument( + 'bundle', + InputArgument::REQUIRED, + 'The bundle where to load the features' + ) + ->addOption( + 'dry-run', + null, + InputOption::VALUE_NONE, + 'Do not persist new features' + ); } + /** + * {@inheritdoc} + */ public function execute(InputInterface $input, OutputInterface $output) { - $twig = $this->getContainer()->get('twig'); - $bundle = $this->getApplication()->getKernel()->getBundle($input->getArgument('bundle')); + $twig = $this->getContainer()->get('twig'); + $bundle = $this + ->getApplication() + ->getKernel() + ->getBundle($input->getArgument('bundle')); + if (!$bundle) { - new \InvalidArgumentException("Bundle `$bundle` does not exists"); + throw new InvalidArgumentException("Bundle `$bundle` does not exists"); } - $found = array(); + $found = []; $dir = $bundle->getPath().'/Resources/views/'; if (!is_dir($dir)) { - throw \Exception("'Directory `$dir` does not exists."); + throw new Exception("'Directory `$dir` does not exists."); } $finder = new Finder(); - $files = $finder->files()->name('*.html.twig')->in($dir); + $files = $finder->files()->name('*.html.twig')->in($dir); foreach ($files as $file) { - $tree = $twig->parse($twig->tokenize(file_get_contents($file->getPathname()))); + $tree = $twig->parse( + $twig->tokenize(file_get_contents($file->getPathname())) + ); $tags = $this->findFeatureNodes($tree); if ($tags) { $found = array_merge($found, $tags); foreach ($tags as $tag) { - $output->writeln(sprintf('Found %s.%s in %s', $tag['parent'], $tag['name'], $file->getFilename())); + $output->writeln(sprintf( + 'Found %s.%s in %s', + $tag['parent'], + $tag['name'], + $file->getFilename() + )); } } } + if ($input->getOption('dry-run')) { return; } + $manager = $this->getContainer()->get('cw_feature.manager'); foreach ($found as $tag) { $manager->findOrCreate($tag['name'], $tag['parent']); } } - protected function findFeatureNodes(\Twig_Node $node) + protected function findFeatureNodes(Twig_Node $node) { - $found = array(); - $stack = array($node); + $found = []; + $stack = [$node]; while ($stack) { $node = array_pop($stack); if ($node instanceof FeatureNode) { - $arguments = $node->getNode('tests')->getNode(0)->getNode('arguments')->getKeyValuePairs(); - $tag = array(); + $arguments = $node + ->getNode('tests') + ->getNode(0) + ->getNode('arguments') + ->getKeyValuePairs(); + + $tag = []; foreach ($arguments as $argument) { - $tag[$argument['key']->getAttribute('value')] = $argument['value']->getAttribute('value'); + $keyAttr = $argument['key']->getAttribute('value'); + $valueAttr = $argument['value']->getAttribute('value'); + + $tag[$keyAttr] = $valueAttr; } $key = md5(serialize($tag)); $found[$key] = $tag; diff --git a/Entity/FeatureManager.php b/Entity/FeatureManager.php index 6e8062c..ca87b56 100644 --- a/Entity/FeatureManager.php +++ b/Entity/FeatureManager.php @@ -36,11 +36,12 @@ public function __construct(EntityManager $em) */ public function find($name, $parent) { - return $this->em->createQuery('SELECT f,p FROM AeFeatureBundle:Feature f JOIN f.parent p WHERE f.name = :name AND p.name = :parent') - ->setParameters(array( - 'name' => $name, + return $this->em + ->createQuery('SELECT f,p FROM AeFeatureBundle:Feature f JOIN f.parent p WHERE f.name = :name AND p.name = :parent') + ->setParameters([ + 'name' => $name, 'parent' => $parent, - )) + ]) ->useResultCache(true, 3600 * 24, self::generateCacheKey($parent, $name)) ->getSingleResult(); } @@ -52,7 +53,8 @@ public function find($name, $parent) */ public function findParent($name) { - return $this->em->createQuery('SELECT f FROM AeFeatureBundle:Feature f WHERE f.name = :name AND f.parent IS NULL') + return $this->em + ->createQuery('SELECT f FROM AeFeatureBundle:Feature f WHERE f.name = :name AND f.parent IS NULL') ->setParameter('name', $name) ->getSingleResult(); } diff --git a/Security/FeatureSecurity.php b/Security/FeatureSecurity.php index d90217c..fbdafe0 100644 --- a/Security/FeatureSecurity.php +++ b/Security/FeatureSecurity.php @@ -2,8 +2,8 @@ namespace Ae\FeatureBundle\Security; -use Symfony\Component\Security\Core\SecurityContextInterface; use Ae\FeatureBundle\Entity\Feature; +use Symfony\Component\Security\Core\SecurityContextInterface; /** * Controls access to a Feature. diff --git a/Service/Feature.php b/Service/Feature.php index 5798bf9..622c6dc 100644 --- a/Service/Feature.php +++ b/Service/Feature.php @@ -4,21 +4,31 @@ use Ae\FeatureBundle\Entity\FeatureManager; use Ae\FeatureBundle\Security\FeatureSecurity; +use Exception; /** * @author Carlo Forghieri */ class Feature { - protected $security; + /** + * @var FeatureManager + */ protected $manager; /** - * @param \Ae\FeatureBundle\Entity\FeatureManager $manager - * @param \Ae\FeatureBundle\Security\FeatureSecurity $security + * @var FeatureSecurity */ - public function __construct(FeatureManager $manager, FeatureSecurity $security) - { + protected $security; + + /** + * @param FeatureManager $manager + * @param FeatureSecurity $security + */ + public function __construct( + FeatureManager $manager, + FeatureSecurity $security + ) { $this->manager = $manager; $this->security = $security; } @@ -34,8 +44,10 @@ public function __construct(FeatureManager $manager, FeatureSecurity $security) public function isGranted($name, $parent) { try { - return $this->security->isGranted($this->manager->find($name, $parent)); - } catch (\Exception $e) { + return $this->security->isGranted( + $this->manager->find($name, $parent) + ); + } catch (Exception $exception) { return false; } } diff --git a/Tests/DependencyInjection/AeFeatureExtensionTest.php b/Tests/DependencyInjection/AeFeatureExtensionTest.php index e7e2252..fa4d6ee 100644 --- a/Tests/DependencyInjection/AeFeatureExtensionTest.php +++ b/Tests/DependencyInjection/AeFeatureExtensionTest.php @@ -30,13 +30,18 @@ protected function getContainerExtensions() * @dataProvider parametersProvider * @group legacy */ - public function testLegacyParameters($parameterName, $expectedParameterValue) - { + public function testLegacyParameters( + $parameterName, + $expectedParameterValue + ) { $this->load(); $this->compile(); - $parameterName = 'cw' . substr($parameterName, 2); - $this->assertContainerBuilderHasParameter($parameterName, $expectedParameterValue); + $parameterName = 'cw'.substr($parameterName, 2); + $this->assertContainerBuilderHasParameter( + $parameterName, + $expectedParameterValue + ); } /** @@ -52,7 +57,10 @@ public function testParameters($parameterName, $expectedParameterValue) $this->load(); $this->compile(); - $this->assertContainerBuilderHasParameter($parameterName, $expectedParameterValue); + $this->assertContainerBuilderHasParameter( + $parameterName, + $expectedParameterValue + ); } /** @@ -61,14 +69,22 @@ public function testParameters($parameterName, $expectedParameterValue) public function parametersProvider() { return [ - ['ae_feature.manager.class', 'Ae\FeatureBundle\Entity\FeatureManager'], - ['ae_feature.security.class', 'Ae\FeatureBundle\Security\FeatureSecurity'], + [ + 'ae_feature.manager.class', + 'Ae\FeatureBundle\Entity\FeatureManager', + ], + [ + 'ae_feature.security.class', + 'Ae\FeatureBundle\Security\FeatureSecurity', + ], ['ae_feature.feature.class', 'Ae\FeatureBundle\Service\Feature'], - ['ae_feature.twig.extension.feature.class', 'Ae\FeatureBundle\Twig\Extension\FeatureExtension'], + [ + 'ae_feature.twig.extension.feature.class', + 'Ae\FeatureBundle\Twig\Extension\FeatureExtension', + ], ]; } - /** * Test services "alias" to migrate from CreativeWeb to AdEspresso. * @@ -83,9 +99,12 @@ public function testLegacyServices($serviceId, $expectedClass) $this->load(); $this->compile(); - $oldServiceId = 'cw' . substr($serviceId, 2); + $oldServiceId = 'cw'.substr($serviceId, 2); $this->assertContainerBuilderHasService($serviceId, $expectedClass); - $this->assertContainerBuilderHasServiceDefinitionWithParent($oldServiceId, $serviceId); + $this->assertContainerBuilderHasServiceDefinitionWithParent( + $oldServiceId, + $serviceId + ); } /** @@ -111,9 +130,15 @@ public function servicesProvider() { return [ ['ae_feature.manager', 'Ae\FeatureBundle\Entity\FeatureManager'], - ['ae_feature.security', 'Ae\FeatureBundle\Security\FeatureSecurity'], + [ + 'ae_feature.security', + 'Ae\FeatureBundle\Security\FeatureSecurity', + ], ['ae_feature.feature', 'Ae\FeatureBundle\Service\Feature'], - ['ae_feature.twig.extension.feature', 'Ae\FeatureBundle\Twig\Extension\FeatureExtension'], + [ + 'ae_feature.twig.extension.feature', + 'Ae\FeatureBundle\Twig\Extension\FeatureExtension', + ], ]; } } diff --git a/Tests/Entity/FeatureManagerTest.php b/Tests/Entity/FeatureManagerTest.php index bade8e4..b2852fe 100644 --- a/Tests/Entity/FeatureManagerTest.php +++ b/Tests/Entity/FeatureManagerTest.php @@ -3,12 +3,13 @@ namespace Ae\FeatureBundle\Tests\Entity; use Ae\FeatureBundle\Entity\FeatureManager; +use PHPUnit_Framework_TestCase; /** * @author Carlo Forghieri * @covers Ae\FeatureBundle\Entity\FeatureManager */ -class FeatureManagerTest extends \PHPUnit_Framework_TestCase +class FeatureManagerTest extends PHPUnit_Framework_TestCase { protected $em; protected $manager; @@ -18,16 +19,17 @@ protected function setUp() $this->em = $this->getMockBuilder('\Doctrine\ORM\EntityManager') ->disableOriginalConstructor() ->getMock(); - $this->manager = $this->getMockBuilder('\Ae\FeatureBundle\Entity\FeatureManager') - ->setConstructorArgs(array($this->em)) - ->setMethods(array('emptyCache')) + $this->manager = $this + ->getMockBuilder('\Ae\FeatureBundle\Entity\FeatureManager') + ->setConstructorArgs([$this->em]) + ->setMethods(['emptyCache']) ->getMock(); } public function testCreate() { - $name = 'foo'; - $parent = $this->getMock('Ae\FeatureBundle\Entity\Feature'); + $name = 'foo'; + $parent = $this->getMock('Ae\FeatureBundle\Entity\Feature'); $feature = $this->manager->create($name, $parent); $this->assertEquals($name, $feature->getName($name)); @@ -37,7 +39,7 @@ public function testCreate() public function testUpdate() { $feature = $this->getMock('Ae\FeatureBundle\Entity\Feature'); - $parent = $this->getMock('Ae\FeatureBundle\Entity\Feature'); + $parent = $this->getMock('Ae\FeatureBundle\Entity\Feature'); $feature->expects($this->once()) ->method('getParent') ->will($this->returnValue($parent)); @@ -54,6 +56,9 @@ public function testGenerateCacheKey() $parentName = 'PNAME'; $name = 'NAME'; - $this->assertEquals('feature_pname_name', FeatureManager::generateCacheKey($parentName, $name)); + $this->assertEquals( + 'feature_pname_name', + FeatureManager::generateCacheKey($parentName, $name) + ); } } diff --git a/Tests/Entity/FeatureTest.php b/Tests/Entity/FeatureTest.php index 9836250..1768f2f 100644 --- a/Tests/Entity/FeatureTest.php +++ b/Tests/Entity/FeatureTest.php @@ -3,12 +3,13 @@ namespace Ae\FeatureBundle\Tests\Entity; use Ae\FeatureBundle\Entity\Feature; +use PHPUnit_Framework_TestCase; /** * @author Carlo Forghieri * @covers Ae\FeatureBundle\Entity\Feature */ -class FeatureTest extends \PHPUnit_Framework_TestCase +class FeatureTest extends PHPUnit_Framework_TestCase { protected $entity; @@ -32,7 +33,10 @@ public function testChildren() $parent = $this->getMock('Ae\FeatureBundle\Entity\Feature'); $this->entity->addFeature($parent); $collection = $this->entity->getChildren(); - $this->assertInstanceOf('Doctrine\Common\Collections\Collection', $collection); + $this->assertInstanceOf( + 'Doctrine\Common\Collections\Collection', + $collection + ); $this->assertEquals($parent, $collection->first()); } diff --git a/Tests/Security/FeatureSecurityTest.php b/Tests/Security/FeatureSecurityTest.php index 54cad93..2db84cd 100644 --- a/Tests/Security/FeatureSecurityTest.php +++ b/Tests/Security/FeatureSecurityTest.php @@ -3,26 +3,30 @@ namespace Ae\FeatureBundle\Tests\Security; use Ae\FeatureBundle\Security\FeatureSecurity; +use PHPUnit_Framework_TestCase; /** * @author Carlo Forghieri * @covers Ae\FeatureBundle\Security\FeatureSecurity */ -class FeatureSecurityTest extends \PHPUnit_Framework_TestCase +class FeatureSecurityTest extends PHPUnit_Framework_TestCase { protected $security; protected function setUp() { - $context = $this->getMockBuilder('\Symfony\Component\Security\Core\SecurityContextInterface') + $context = $this + ->getMockBuilder( + '\Symfony\Component\Security\Core\SecurityContextInterface' + ) ->disableOriginalConstructor() ->getMock(); $context->expects($this->any()) ->method('isGranted') - ->will($this->returnValueMap(array( - array('ROLE_USER', null, true), - array('ROLE_ADMIN', null, false), - ))); + ->will($this->returnValueMap([ + ['ROLE_USER', null, true], + ['ROLE_ADMIN', null, false], + ])); $this->security = new FeatureSecurity($context); } @@ -34,42 +38,52 @@ public function testIsGranted($feature, $expected) $this->assertEquals($expected, $this->security->isGranted($feature)); } + /** + * @return array + */ public function getFeatures() { - $tests = array(); + $tests = []; - $f = $this->getMock('Ae\FeatureBundle\Entity\Feature'); - $f->expects($this->once()) + $feature = $this->getMock('Ae\FeatureBundle\Entity\Feature'); + $feature + ->expects($this->once()) ->method('isEnabled') ->will($this->returnValue(false)); - $tests[] = array($f, false); + $tests[] = [$feature, false]; - $f = $this->getMock('Ae\FeatureBundle\Entity\Feature'); - $f->expects($this->once()) + $feature = $this->getMock('Ae\FeatureBundle\Entity\Feature'); + $feature + ->expects($this->once()) ->method('isEnabled') ->will($this->returnValue(true)); - $tests[] = array($f, true); + $tests[] = [$feature, true]; - $f = $this->getMock('Ae\FeatureBundle\Entity\Feature'); - $f->expects($this->once()) + $feature = $this->getMock('Ae\FeatureBundle\Entity\Feature'); + $feature + ->expects($this->once()) ->method('isEnabled') ->will($this->returnValue(true)); - $f->expects($this->atLeastOnce()) + $feature + ->expects($this->atLeastOnce()) ->method('getRole') ->will($this->returnValue('ROLE_USER')); - $tests[] = array($f, true); + $tests[] = [$feature, true]; - $f = $this->getMock('Ae\FeatureBundle\Entity\Feature'); - $f->expects($this->once()) + $feature = $this->getMock('Ae\FeatureBundle\Entity\Feature'); + $feature + ->expects($this->once()) ->method('isEnabled') ->will($this->returnValue(true)); - $f->expects($this->atLeastOnce()) + $feature + ->expects($this->atLeastOnce()) ->method('getRole') ->will($this->returnValue('ROLE_USER')); - $f->expects($this->atLeastOnce()) + $feature + ->expects($this->atLeastOnce()) ->method('getParentRole') ->will($this->returnValue('ROLE_ADMIN')); - $tests[] = array($f, false); + $tests[] = [$feature, false]; return $tests; } diff --git a/Tests/Service/FeatureTest.php b/Tests/Service/FeatureTest.php index 897b198..972bd1d 100644 --- a/Tests/Service/FeatureTest.php +++ b/Tests/Service/FeatureTest.php @@ -3,12 +3,13 @@ namespace Ae\FeatureBundle\Tests\Service; use Ae\FeatureBundle\Service\Feature; +use PHPUnit_Framework_TestCase; /** * @author Carlo Forghieri * @covers Ae\FeatureBundle\Service\Feature */ -class FeatureTest extends \PHPUnit_Framework_TestCase +class FeatureTest extends PHPUnit_Framework_TestCase { protected $manager; protected $security; @@ -16,10 +17,12 @@ class FeatureTest extends \PHPUnit_Framework_TestCase protected function setUp() { - $this->manager = $this->getMockBuilder('Ae\FeatureBundle\Entity\FeatureManager') + $this->manager = $this + ->getMockBuilder('Ae\FeatureBundle\Entity\FeatureManager') ->disableOriginalConstructor() ->getMock(); - $this->security = $this->getMockBuilder('Ae\FeatureBundle\Security\FeatureSecurity') + $this->security = $this + ->getMockBuilder('Ae\FeatureBundle\Security\FeatureSecurity') ->disableOriginalConstructor() ->getMock(); $this->service = new Feature($this->manager, $this->security); @@ -28,16 +31,18 @@ protected function setUp() public function testIsGrantedTrue() { $featureEnabled = $this->getMock('Ae\FeatureBundle\Entity\Feature'); - $this->manager->expects($this->atLeastOnce()) + $this->manager + ->expects($this->atLeastOnce()) ->method('find') - ->will($this->returnValueMap(array( - array('featureA', 'group', $featureEnabled), - ))); - $this->security->expects($this->atLeastOnce()) + ->will($this->returnValueMap([ + ['featureA', 'group', $featureEnabled], + ])); + $this->security + ->expects($this->atLeastOnce()) ->method('isGranted') - ->will($this->returnValueMap(array( - array($featureEnabled, true), - ))); + ->will($this->returnValueMap([ + [$featureEnabled, true], + ])); $this->assertTrue($this->service->isGranted('featureA', 'group')); } @@ -45,16 +50,18 @@ public function testIsGrantedTrue() public function testIsGrantedFalse() { $featureDisabled = $this->getMock('Ae\FeatureBundle\Entity\Feature'); - $this->manager->expects($this->atLeastOnce()) + $this->manager + ->expects($this->atLeastOnce()) ->method('find') - ->will($this->returnValueMap(array( - array('featureB', 'group', $featureDisabled), - ))); - $this->security->expects($this->atLeastOnce()) + ->will($this->returnValueMap([ + ['featureB', 'group', $featureDisabled], + ])); + $this->security + ->expects($this->atLeastOnce()) ->method('isGranted') - ->will($this->returnValueMap(array( - array($featureDisabled, false), - ))); + ->will($this->returnValueMap([ + [$featureDisabled, false], + ])); $this->assertFalse($this->service->isGranted('featureB', 'group')); } diff --git a/Tests/Twig/IntegrationTest.php b/Tests/Twig/IntegrationTest.php index adde8f6..48e05eb 100644 --- a/Tests/Twig/IntegrationTest.php +++ b/Tests/Twig/IntegrationTest.php @@ -3,54 +3,35 @@ namespace Ae\FeatureBundle\Tests\Twig; use Ae\FeatureBundle\Twig\Extension\FeatureExtension; +use Twig_Test_IntegrationTestCase; /** * @author Carlo Forghieri * @covers Ae\FeatureBundle\Twig\Node\FeatureNode */ -class IntegrationTest extends \Twig_Test_IntegrationTestCase +class IntegrationTest extends Twig_Test_IntegrationTestCase { - protected static $originalErrorLevel; - - public function __construct($name = null, array $data = array(), $dataName = '') - { - self::$originalErrorLevel = error_reporting(); - error_reporting(self::$originalErrorLevel & ~E_WARNING); - - parent::__construct($name, $data, $dataName); - } - - public function tearDown() - { - parent::tearDown(); - - error_reporting(self::$originalErrorLevel); - } - public function getExtensions() { - return array( - $this->getFeatureExtension(), - ); - } - - public function getFixturesDir() - { - return dirname(__FILE__).'/Fixtures/'; - } - - protected function getFeatureExtension() - { - $service = $this->getMockBuilder('Ae\FeatureBundle\Service\Feature') + $service = $this + ->getMockBuilder('Ae\FeatureBundle\Service\Feature') ->disableOriginalConstructor() ->getMock(); - $service->expects($this->atLeastOnce()) + $service + ->expects($this->atLeastOnce()) ->method('isGranted') - ->will($this->returnValueMap(array( - array('featureA', 'group', true), - array('featureB', 'group', false), - ))); + ->will($this->returnValueMap([ + ['featureA', 'group', true], + ['featureB', 'group', false], + ])); + + return [ + new FeatureExtension($service), + ]; + } - return new FeatureExtension($service); + public function getFixturesDir() + { + return __DIR__.'/Fixtures'; } } diff --git a/Tests/Twig/Node/FeatureNodeTest.php b/Tests/Twig/Node/FeatureNodeTest.php index 8c041c4..56c1d84 100644 --- a/Tests/Twig/Node/FeatureNodeTest.php +++ b/Tests/Twig/Node/FeatureNodeTest.php @@ -3,21 +3,25 @@ namespace Ae\FeatureBundle\Tests\Twig\Node; use Ae\FeatureBundle\Twig\Node\FeatureNode; +use Twig_Node; +use Twig_Node_Expression_Name; +use Twig_Node_Print; +use Twig_Test_NodeTestCase; /** * @author Carlo Forghieri * @covers Ae\FeatureBundle\Twig\Node\FeatureNode */ -class FeatureNodeTest extends \Twig_Test_NodeTestCase +class FeatureNodeTest extends Twig_Test_NodeTestCase { public function testConstructor() { - $name = 'foo'; + $name = 'foo'; $parent = 'parent'; - $body = new \Twig_Node(array(), array(), 1); + $body = new Twig_Node([], [], 1); - $node = new FeatureNode($name, $parent, $body, null, 1); - $tests = $node->getNode('tests')->getIterator(); + $node = new FeatureNode($name, $parent, $body, null, 1); + $tests = $node->getNode('tests')->getIterator(); $this->assertInstanceOf('Twig_Node_Expression_MethodCall', $tests[0]); $this->assertEquals($body, $tests[1]); @@ -27,35 +31,39 @@ public function testConstructor() /** * @dataProvider getTests */ - public function testCompile($node, $source, $environment = null, $isPattern = null) - { + public function testCompile( + $node, + $source, + $environment = null, + $isPattern = null + ) { parent::testCompile($node, $source, $environment, $isPattern); } public function getTests() { - $tests = array(); + $tests = []; - $name = 'foo'; + $name = 'foo'; $parent = 'parent'; - $body = new \Twig_Node(array( - new \Twig_Node_Print(new \Twig_Node_Expression_Name('foo', 1), 1), - ), array(), 1); + $body = new Twig_Node([ + new Twig_Node_Print(new Twig_Node_Expression_Name('foo', 1), 1), + ], [], 1); $node = new FeatureNode($name, $parent, $body, null, 1); - $tests[] = array($node, <<env->getExtension('feature')->isGranted("$name", "$parent")) { echo {$this->getVariableGetter('foo')}; } EOF - ); + ]; - $else = new \Twig_Node(array( - new \Twig_Node_Print(new \Twig_Node_Expression_Name('bar', 1), 1), - ), array(), 1); + $else = new Twig_Node([ + new Twig_Node_Print(new Twig_Node_Expression_Name('bar', 1), 1), + ], [], 1); $node = new FeatureNode($name, $parent, $body, $else, 1); - $tests[] = array($node, <<env->getExtension('feature')->isGranted("$name", "$parent")) { echo {$this->getVariableGetter('foo')}; @@ -63,7 +71,7 @@ public function getTests() echo {$this->getVariableGetter('bar')}; } EOF - ); + ]; return $tests; } diff --git a/Twig/Extension/FeatureExtension.php b/Twig/Extension/FeatureExtension.php index 430d11c..372ca52 100644 --- a/Twig/Extension/FeatureExtension.php +++ b/Twig/Extension/FeatureExtension.php @@ -4,11 +4,12 @@ use Ae\FeatureBundle\Service\Feature; use Ae\FeatureBundle\Twig\TokenParser\FeatureTokenParser; +use Twig_Extension; /** * @author Carlo Forghieri */ -class FeatureExtension extends \Twig_Extension +class FeatureExtension extends Twig_Extension { /** * @var Feature @@ -20,7 +21,7 @@ class FeatureExtension extends \Twig_Extension */ public function __construct(Feature $service) { - $this->service = $service; + $this->service = $service; } /** @@ -28,9 +29,9 @@ public function __construct(Feature $service) */ public function getTokenParsers() { - return array( + return [ new FeatureTokenParser(), - ); + ]; } /** diff --git a/Twig/Node/FeatureNode.php b/Twig/Node/FeatureNode.php index 1989307..9dd2791 100644 --- a/Twig/Node/FeatureNode.php +++ b/Twig/Node/FeatureNode.php @@ -2,33 +2,40 @@ namespace Ae\FeatureBundle\Twig\Node; +use Twig_Node; +use Twig_Node_Expression_Array; +use Twig_Node_Expression_Constant; +use Twig_Node_Expression_ExtensionReference; +use Twig_Node_Expression_MethodCall; +use Twig_Node_If; + /** * @author Carlo Forghieri */ -class FeatureNode extends \Twig_Node_If +class FeatureNode extends Twig_Node_If { public function __construct($name, $parent, $body, $else, $lineno, $tag = null) { - $tests = new \Twig_Node(array( + $tests = new Twig_Node([ $this->createExpression($name, $parent, $lineno), $body, - )); + ]); - parent::__construct($tests, $else, $lineno, $tag = null); + parent::__construct($tests, $else, $lineno, $tag); } protected function createExpression($name, $parent, $lineno) { - return new \Twig_Node_Expression_MethodCall( - new \Twig_Node_Expression_ExtensionReference('feature', $lineno), + return new Twig_Node_Expression_MethodCall( + new Twig_Node_Expression_ExtensionReference('feature', $lineno), 'isGranted', - new \Twig_Node_Expression_Array( - array( - new \Twig_Node_Expression_Constant('name', $lineno), - new \Twig_Node_Expression_Constant($name, $lineno), - new \Twig_Node_Expression_Constant('parent', $lineno), - new \Twig_Node_Expression_Constant($parent, $lineno), - ), + new Twig_Node_Expression_Array( + [ + new Twig_Node_Expression_Constant('name', $lineno), + new Twig_Node_Expression_Constant($name, $lineno), + new Twig_Node_Expression_Constant('parent', $lineno), + new Twig_Node_Expression_Constant($parent, $lineno), + ], $lineno ), $lineno diff --git a/Twig/TokenParser/FeatureTokenParser.php b/Twig/TokenParser/FeatureTokenParser.php index d6721d7..a8f947a 100644 --- a/Twig/TokenParser/FeatureTokenParser.php +++ b/Twig/TokenParser/FeatureTokenParser.php @@ -3,11 +3,14 @@ namespace Ae\FeatureBundle\Twig\TokenParser; use Ae\FeatureBundle\Twig\Node\FeatureNode; +use Twig_Error_Syntax; +use Twig_Token; +use Twig_TokenParser; /** * @author Carlo Forghieri */ -class FeatureTokenParser extends \Twig_TokenParser +class FeatureTokenParser extends Twig_TokenParser { /** * Parses a token and returns a node. @@ -16,40 +19,44 @@ class FeatureTokenParser extends \Twig_TokenParser * * @return \Twig_NodeInterface A Twig_NodeInterface instance. */ - public function parse(\Twig_Token $token) + public function parse(Twig_Token $token) { $lineno = $token->getLine(); $stream = $this->parser->getStream(); - $name = null; + $name = null; $parent = null; - if (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) { - if ($stream->test(\Twig_Token::STRING_TYPE)) { + if (!$stream->test(Twig_Token::BLOCK_END_TYPE)) { + if ($stream->test(Twig_Token::STRING_TYPE)) { // {% feature "name" %} $name = $stream->next()->getValue(); - } elseif (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) { - throw new \Twig_Error_Syntax('Unexpected token. Twig was looking for the "name" string.'); + } elseif (!$stream->test(Twig_Token::BLOCK_END_TYPE)) { + throw new Twig_Error_Syntax( + 'Unexpected token. Twig was looking for the "name" string.' + ); } if ($stream->test('from')) { // {% feature "name" from "parent" %} $stream->next(); $parent = $stream->next()->getValue(); - } elseif (!$stream->test(\Twig_Token::BLOCK_END_TYPE)) { - throw new \Twig_Error_Syntax('Unexpected token. Twig was looking for the "from" keyword.'); + } elseif (!$stream->test(Twig_Token::BLOCK_END_TYPE)) { + throw new Twig_Error_Syntax( + 'Unexpected token. Twig was looking for the "from" keyword.' + ); } } // {% feature %}...{% endfeature %} - $stream->expect(\Twig_Token::BLOCK_END_TYPE); - $body = $this->parser->subparse(array($this, 'decideFeatureFork')); + $stream->expect(Twig_Token::BLOCK_END_TYPE); + $body = $this->parser->subparse([$this, 'decideFeatureFork']); $else = null; - $end = false; + $end = false; while (!$end) { switch ($this->parser->getStream()->next()->getValue()) { case 'else': - $stream->expect(\Twig_Token::BLOCK_END_TYPE); - $else = $this->parser->subparse(array($this, 'decideFeatureEnd')); + $stream->expect(Twig_Token::BLOCK_END_TYPE); + $else = $this->parser->subparse([$this, 'decideFeatureEnd']); break; case 'endfeature': @@ -57,23 +64,38 @@ public function parse(\Twig_Token $token) break; default: - throw new \Twig_Error_Syntax(sprintf('Unexpected end of template. Twig was looking for the following tags "else" or "endfeature" to close the "feature" block started at line %d)', $lineno), -1); + throw new Twig_Error_Syntax( + sprintf( + 'Unexpected end of template. Twig was looking for '. + 'the following tags "else" or "endfeature" to '. + 'close the "feature" block started at line %d)', + $lineno + ), + -1 + ); } } - $stream->expect(\Twig_Token::BLOCK_END_TYPE); + $stream->expect(Twig_Token::BLOCK_END_TYPE); - return new FeatureNode($name, $parent, $body, $else, $lineno, $this->getTag()); + return new FeatureNode( + $name, + $parent, + $body, + $else, + $lineno, + $this->getTag() + ); } - public function decideFeatureFork(\Twig_Token $token) + public function decideFeatureFork(Twig_Token $token) { - return $token->test(array('else', 'endfeature')); + return $token->test(['else', 'endfeature']); } - public function decideFeatureEnd(\Twig_Token $token) + public function decideFeatureEnd(Twig_Token $token) { - return $token->test(array('endfeature')); + return $token->test(['endfeature']); } /** From fbfeb33e663b0201de7f05a332386202cb21828f Mon Sep 17 00:00:00 2001 From: Emanuele Minotto Date: Thu, 26 May 2016 21:13:43 +0200 Subject: [PATCH 7/9] Order composer configuration --- composer.json | 41 ++++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 19 deletions(-) diff --git a/composer.json b/composer.json index 334b84c..aaf8ef6 100644 --- a/composer.json +++ b/composer.json @@ -1,24 +1,10 @@ { - "name": "adespresso/feature-bundle", - "description": "Manage release of new features", - "license": "Apache-2.0", - "type": "symfony-bundle", "authors": [ { - "name": "Carlo Forghieri", - "email": "carlo@adespresso.com" + "email": "carlo@adespresso.com", + "name": "Carlo Forghieri" } ], - "require": { - "php": "^5.4||^7.0", - "symfony/framework-bundle": "^2.3||^3.0", - "symfony/security": "^2.3||^3.0", - "symfony/twig-bundle": "^2.3||^3.0", - "doctrine/orm": "^2.2.3", - "friendsofsymfony/user-bundle": "1.3.*", - "sonata-project/admin-bundle": "^2.3.4", - "sonata-project/doctrine-orm-admin-bundle": "^2.3.1" - }, "autoload": { "exclude-from-classmap": [ "/Tests/" @@ -27,9 +13,26 @@ "Ae\\FeatureBundle\\": "" } }, + "config": { + "sort-packages": true + }, + "description": "Manage release of new features", + "license": "Apache-2.0", + "name": "adespresso/feature-bundle", + "require": { + "php": "^5.4 || ^7.0", + "doctrine/orm": "^2.2.3", + "friendsofsymfony/user-bundle": "1.3.*", + "sonata-project/admin-bundle": "^2.3.4", + "sonata-project/doctrine-orm-admin-bundle": "^2.3.1", + "symfony/framework-bundle": "^2.7 || ^3.0", + "symfony/security": "^2.3 || ^3.0", + "symfony/twig-bundle": "^2.3 || ^3.0" + }, "require-dev": { - "phpunit/phpunit": "^4.8||^5.0", + "fabpot/php-cs-fixer": "^1.11", "matthiasnoback/symfony-dependency-injection-test": "^0.7.6", - "fabpot/php-cs-fixer": "^1.11" - } + "phpunit/phpunit": "^4.8 || ^5.0" + }, + "type": "symfony-bundle" } From 0f960d063547c60ee9eb91c99f1019a78f33b1bd Mon Sep 17 00:00:00 2001 From: Emanuele Minotto Date: Fri, 27 May 2016 16:51:34 +0200 Subject: [PATCH 8/9] Require dependency-injection 2.8+ due to xsd --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index aaf8ef6..c023d59 100644 --- a/composer.json +++ b/composer.json @@ -25,6 +25,7 @@ "friendsofsymfony/user-bundle": "1.3.*", "sonata-project/admin-bundle": "^2.3.4", "sonata-project/doctrine-orm-admin-bundle": "^2.3.1", + "symfony/dependency-injection": "^2.8 || ^3.0", "symfony/framework-bundle": "^2.7 || ^3.0", "symfony/security": "^2.3 || ^3.0", "symfony/twig-bundle": "^2.3 || ^3.0" From b1410efaad79d260789ed2300cb19b519b67fefb Mon Sep 17 00:00:00 2001 From: Emanuele Minotto Date: Tue, 31 May 2016 15:44:43 +0200 Subject: [PATCH 9/9] Update minimum PHP version to 5.5 --- .travis.yml | 3 +- .../AeFeatureExtensionTest.php | 32 +++++++------------ Tests/Entity/FeatureManagerTest.php | 22 ++++++++----- Tests/Entity/FeatureTest.php | 10 +++--- Tests/Security/FeatureSecurityTest.php | 14 ++++---- Tests/Service/FeatureTest.php | 15 +++++---- Tests/Twig/IntegrationTest.php | 3 +- composer.json | 2 +- 8 files changed, 50 insertions(+), 51 deletions(-) diff --git a/.travis.yml b/.travis.yml index 7ca7df5..b09f6ae 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,9 +8,8 @@ cache: matrix: include: - - php: 5.4 + - php: 5.5 env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest"' - - php: 5.4 - php: 5.5 - php: 5.6 - php: 7.0 diff --git a/Tests/DependencyInjection/AeFeatureExtensionTest.php b/Tests/DependencyInjection/AeFeatureExtensionTest.php index fa4d6ee..785cb32 100644 --- a/Tests/DependencyInjection/AeFeatureExtensionTest.php +++ b/Tests/DependencyInjection/AeFeatureExtensionTest.php @@ -3,6 +3,10 @@ namespace Ae\FeatureBundle\Tests\DependencyInjection; use Ae\FeatureBundle\DependencyInjection\AeFeatureExtension; +use Ae\FeatureBundle\Entity\FeatureManager; +use Ae\FeatureBundle\Security\FeatureSecurity; +use Ae\FeatureBundle\Service\Feature; +use Ae\FeatureBundle\Twig\Extension\FeatureExtension; use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase; /** @@ -69,18 +73,12 @@ public function testParameters($parameterName, $expectedParameterValue) public function parametersProvider() { return [ - [ - 'ae_feature.manager.class', - 'Ae\FeatureBundle\Entity\FeatureManager', - ], - [ - 'ae_feature.security.class', - 'Ae\FeatureBundle\Security\FeatureSecurity', - ], - ['ae_feature.feature.class', 'Ae\FeatureBundle\Service\Feature'], + ['ae_feature.manager.class', FeatureManager::class], + ['ae_feature.security.class', FeatureSecurity::class], + ['ae_feature.feature.class', Feature::class], [ 'ae_feature.twig.extension.feature.class', - 'Ae\FeatureBundle\Twig\Extension\FeatureExtension', + FeatureExtension::class, ], ]; } @@ -129,16 +127,10 @@ public function testServices($serviceId, $expectedClass) public function servicesProvider() { return [ - ['ae_feature.manager', 'Ae\FeatureBundle\Entity\FeatureManager'], - [ - 'ae_feature.security', - 'Ae\FeatureBundle\Security\FeatureSecurity', - ], - ['ae_feature.feature', 'Ae\FeatureBundle\Service\Feature'], - [ - 'ae_feature.twig.extension.feature', - 'Ae\FeatureBundle\Twig\Extension\FeatureExtension', - ], + ['ae_feature.manager', FeatureManager::class], + ['ae_feature.security', FeatureSecurity::class], + ['ae_feature.feature', Feature::class], + ['ae_feature.twig.extension.feature', FeatureExtension::class], ]; } } diff --git a/Tests/Entity/FeatureManagerTest.php b/Tests/Entity/FeatureManagerTest.php index b2852fe..1de23c2 100644 --- a/Tests/Entity/FeatureManagerTest.php +++ b/Tests/Entity/FeatureManagerTest.php @@ -2,7 +2,9 @@ namespace Ae\FeatureBundle\Tests\Entity; +use Ae\FeatureBundle\Entity\Feature; use Ae\FeatureBundle\Entity\FeatureManager; +use Doctrine\ORM\EntityManager; use PHPUnit_Framework_TestCase; /** @@ -16,11 +18,12 @@ class FeatureManagerTest extends PHPUnit_Framework_TestCase protected function setUp() { - $this->em = $this->getMockBuilder('\Doctrine\ORM\EntityManager') + $this->em = $this + ->getMockBuilder(EntityManager::class) ->disableOriginalConstructor() ->getMock(); $this->manager = $this - ->getMockBuilder('\Ae\FeatureBundle\Entity\FeatureManager') + ->getMockBuilder(FeatureManager::class) ->setConstructorArgs([$this->em]) ->setMethods(['emptyCache']) ->getMock(); @@ -29,7 +32,7 @@ protected function setUp() public function testCreate() { $name = 'foo'; - $parent = $this->getMock('Ae\FeatureBundle\Entity\Feature'); + $parent = $this->getMock(Feature::class); $feature = $this->manager->create($name, $parent); $this->assertEquals($name, $feature->getName($name)); @@ -38,15 +41,18 @@ public function testCreate() public function testUpdate() { - $feature = $this->getMock('Ae\FeatureBundle\Entity\Feature'); - $parent = $this->getMock('Ae\FeatureBundle\Entity\Feature'); - $feature->expects($this->once()) + $feature = $this->getMock(Feature::class); + $parent = $this->getMock(Feature::class); + $feature + ->expects($this->once()) ->method('getParent') ->will($this->returnValue($parent)); - $this->em->expects($this->once()) + $this->em + ->expects($this->once()) ->method('persist') ->with($feature); - $this->em->expects($this->once()) + $this->em + ->expects($this->once()) ->method('flush'); $this->manager->update($feature); } diff --git a/Tests/Entity/FeatureTest.php b/Tests/Entity/FeatureTest.php index 1768f2f..7f866ef 100644 --- a/Tests/Entity/FeatureTest.php +++ b/Tests/Entity/FeatureTest.php @@ -3,6 +3,7 @@ namespace Ae\FeatureBundle\Tests\Entity; use Ae\FeatureBundle\Entity\Feature; +use Doctrine\Common\Collections\Collection; use PHPUnit_Framework_TestCase; /** @@ -23,20 +24,17 @@ protected function setUp() */ public function testParent() { - $parent = $this->getMock('Ae\FeatureBundle\Entity\Feature'); + $parent = $this->getMock(Feature::class); $this->entity->setParent($parent); $this->assertEquals($parent, $this->entity->getParent()); } public function testChildren() { - $parent = $this->getMock('Ae\FeatureBundle\Entity\Feature'); + $parent = $this->getMock(Feature::class); $this->entity->addFeature($parent); $collection = $this->entity->getChildren(); - $this->assertInstanceOf( - 'Doctrine\Common\Collections\Collection', - $collection - ); + $this->assertInstanceOf(Collection::class, $collection); $this->assertEquals($parent, $collection->first()); } diff --git a/Tests/Security/FeatureSecurityTest.php b/Tests/Security/FeatureSecurityTest.php index 2db84cd..62838d6 100644 --- a/Tests/Security/FeatureSecurityTest.php +++ b/Tests/Security/FeatureSecurityTest.php @@ -2,8 +2,10 @@ namespace Ae\FeatureBundle\Tests\Security; +use Ae\FeatureBundle\Entity\Feature; use Ae\FeatureBundle\Security\FeatureSecurity; use PHPUnit_Framework_TestCase; +use Symfony\Component\Security\Core\SecurityContextInterface; /** * @author Carlo Forghieri @@ -16,9 +18,7 @@ class FeatureSecurityTest extends PHPUnit_Framework_TestCase protected function setUp() { $context = $this - ->getMockBuilder( - '\Symfony\Component\Security\Core\SecurityContextInterface' - ) + ->getMockBuilder(SecurityContextInterface::class) ->disableOriginalConstructor() ->getMock(); $context->expects($this->any()) @@ -45,21 +45,21 @@ public function getFeatures() { $tests = []; - $feature = $this->getMock('Ae\FeatureBundle\Entity\Feature'); + $feature = $this->getMock(Feature::class); $feature ->expects($this->once()) ->method('isEnabled') ->will($this->returnValue(false)); $tests[] = [$feature, false]; - $feature = $this->getMock('Ae\FeatureBundle\Entity\Feature'); + $feature = $this->getMock(Feature::class); $feature ->expects($this->once()) ->method('isEnabled') ->will($this->returnValue(true)); $tests[] = [$feature, true]; - $feature = $this->getMock('Ae\FeatureBundle\Entity\Feature'); + $feature = $this->getMock(Feature::class); $feature ->expects($this->once()) ->method('isEnabled') @@ -70,7 +70,7 @@ public function getFeatures() ->will($this->returnValue('ROLE_USER')); $tests[] = [$feature, true]; - $feature = $this->getMock('Ae\FeatureBundle\Entity\Feature'); + $feature = $this->getMock(Feature::class); $feature ->expects($this->once()) ->method('isEnabled') diff --git a/Tests/Service/FeatureTest.php b/Tests/Service/FeatureTest.php index 972bd1d..7b95256 100644 --- a/Tests/Service/FeatureTest.php +++ b/Tests/Service/FeatureTest.php @@ -2,7 +2,10 @@ namespace Ae\FeatureBundle\Tests\Service; -use Ae\FeatureBundle\Service\Feature; +use Ae\FeatureBundle\Entity\Feature; +use Ae\FeatureBundle\Entity\FeatureManager; +use Ae\FeatureBundle\Security\FeatureSecurity; +use Ae\FeatureBundle\Service\Feature as FeatureService; use PHPUnit_Framework_TestCase; /** @@ -18,19 +21,19 @@ class FeatureTest extends PHPUnit_Framework_TestCase protected function setUp() { $this->manager = $this - ->getMockBuilder('Ae\FeatureBundle\Entity\FeatureManager') + ->getMockBuilder(FeatureManager::class) ->disableOriginalConstructor() ->getMock(); $this->security = $this - ->getMockBuilder('Ae\FeatureBundle\Security\FeatureSecurity') + ->getMockBuilder(FeatureSecurity::class) ->disableOriginalConstructor() ->getMock(); - $this->service = new Feature($this->manager, $this->security); + $this->service = new FeatureService($this->manager, $this->security); } public function testIsGrantedTrue() { - $featureEnabled = $this->getMock('Ae\FeatureBundle\Entity\Feature'); + $featureEnabled = $this->getMock(Feature::class); $this->manager ->expects($this->atLeastOnce()) ->method('find') @@ -49,7 +52,7 @@ public function testIsGrantedTrue() public function testIsGrantedFalse() { - $featureDisabled = $this->getMock('Ae\FeatureBundle\Entity\Feature'); + $featureDisabled = $this->getMock(Feature::class); $this->manager ->expects($this->atLeastOnce()) ->method('find') diff --git a/Tests/Twig/IntegrationTest.php b/Tests/Twig/IntegrationTest.php index 48e05eb..cff3424 100644 --- a/Tests/Twig/IntegrationTest.php +++ b/Tests/Twig/IntegrationTest.php @@ -2,6 +2,7 @@ namespace Ae\FeatureBundle\Tests\Twig; +use Ae\FeatureBundle\Service\Feature; use Ae\FeatureBundle\Twig\Extension\FeatureExtension; use Twig_Test_IntegrationTestCase; @@ -14,7 +15,7 @@ class IntegrationTest extends Twig_Test_IntegrationTestCase public function getExtensions() { $service = $this - ->getMockBuilder('Ae\FeatureBundle\Service\Feature') + ->getMockBuilder(Feature::class) ->disableOriginalConstructor() ->getMock(); $service diff --git a/composer.json b/composer.json index c023d59..3e60431 100644 --- a/composer.json +++ b/composer.json @@ -20,7 +20,7 @@ "license": "Apache-2.0", "name": "adespresso/feature-bundle", "require": { - "php": "^5.4 || ^7.0", + "php": "^5.5 || ^7.0", "doctrine/orm": "^2.2.3", "friendsofsymfony/user-bundle": "1.3.*", "sonata-project/admin-bundle": "^2.3.4",