Skip to content

Commit

Permalink
Merge pull request #50 from Abhoryo/testing
Browse files Browse the repository at this point in the history
  • Loading branch information
rvanlaak authored Jun 12, 2020
2 parents ec4237c + 3388c3a commit dfe01b4
Show file tree
Hide file tree
Showing 27 changed files with 245 additions and 60 deletions.
62 changes: 62 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
name: CI

on:
push:
branches:
- master
pull_request:

jobs:
test:
name: Test
runs-on: ubuntu-latest
strategy:
max-parallel: 10
matrix:
php: ['7.2', '7.3', '7.4']

steps:
- name: Set up PHP
uses: shivammathur/[email protected]
with:
php-version: ${{ matrix.php }}
coverage: xdebug
ini-values: xdebug.overload_var_dump=1
extensions: mbstring, intl
tools: prestissimo

- name: Setup Problem Matchers for PHPUnit
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Checkout code
uses: actions/checkout@v2

- name: Run tests
run: make test

test-lowest:
name: Test Lowest
runs-on: ubuntu-latest
strategy:
max-parallel: 10
matrix:
php: ['7.2', '7.3', '7.4']

steps:
- name: Set up PHP
uses: shivammathur/[email protected]
with:
php-version: ${{ matrix.php }}
coverage: xdebug
ini-values: xdebug.overload_var_dump=1
extensions: mbstring, intl
tools: prestissimo

- name: Setup Problem Matchers for PHPUnit
run: echo "::add-matcher::${{ runner.tool_cache }}/phpunit.json"

- name: Checkout code
uses: actions/checkout@v2

- name: Run tests
run: make test-lowest
5 changes: 4 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#2020-06-10
- Add PHPUnit to integration test bundle

#2015-11-10
- Allow complex expressions in breadcrumb names and route parameters

Expand Down Expand Up @@ -27,4 +30,4 @@
- Translate titles

#2011-12-17
- Initial commit
- Initial commit
6 changes: 6 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
test:
composer update --prefer-dist --no-interaction ${COMPOSER_PARAMS}
composer test

test-lowest:
COMPOSER_PARAMS='--prefer-lowest' $(MAKE) test
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ Please follow the steps given [here](https://github.com/Abhoryo/APYBreadcrumbTra

## Summary

- [Annotation configuration](Resources/doc/annotation_configuration.md)
- [PHP configuration](Resources/doc/php_configuration.md)
- [Twig configuration](Resources/doc/twig_configuration.md)
- [Render the breadcrumb trail](Resources/doc/rendering.md)
- [Override the template](Resources/doc/override_template.md)
- [Annotation configuration](src/Resources/doc/annotation_configuration.md)
- [PHP configuration](src/Resources/doc/php_configuration.md)
- [Twig configuration](src/Resources/doc/twig_configuration.md)
- [Render the breadcrumb trail](src/Resources/doc/rendering.md)
- [Override the template](src/Resources/doc/override_template.md)
35 changes: 0 additions & 35 deletions Resources/config/services.xml

This file was deleted.

23 changes: 18 additions & 5 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,15 +12,28 @@
}
],
"require": {
"php": ">=5.3.3",
"symfony/framework-bundle": "^2.3|^3.0|^4.0|^5.0",
"php": ">=7.1",
"symfony/framework-bundle": "^3.4|^4.0|^5.0",
"twig/twig": "^1.41|^2.0|^3.0"
},
"scripts": {
"test": "vendor/bin/simple-phpunit --testdox"
},
"suggest": {
"ext-intl": "*"
"doctrine/doctrine-bundle": "To allow adding breadcrumbs on controller actions by the use of annotations.",
"twig/twig": "To allow adding breadcrumbs via Twig templates."
},
"autoload": {
"psr-4": { "APY\\BreadcrumbTrailBundle\\": "" }
"psr-4": { "APY\\BreadcrumbTrailBundle\\": "src/" }
},
"require-dev": {
"symfony/phpunit-bridge": "^5.0",
"nyholm/symfony-bundle-test": "^1.5",
"matthiasnoback/symfony-dependency-injection-test": "^4.0",
"doctrine/doctrine-bundle": "^v1.0|^v2.0",
"symfony/twig-bundle": "^3.4|^4.0|^5.0"
},
"target-dir": "APY/BreadcrumbTrailBundle"
"autoload-dev": {
"psr-4": { "APY\\BreadcrumbTrailBundle\\": "tests/" }
}
}
32 changes: 32 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/6.0/phpunit.xsd"
backupGlobals="false"
colors="true"
convertErrorsToExceptions="true"
convertNoticesToExceptions="true"
convertWarningsToExceptions="true"
bootstrap="./vendor/autoload.php"
>
<php>
<ini name="error_reporting" value="-1" />
<ini name="intl.default_locale" value="en" />
<ini name="intl.error_level" value="0" />
<ini name="memory_limit" value="-1" />
<env name="SYMFONY_DEPRECATIONS_HELPER" value="max[self]=0" />
<env name="SYMFONY_PHPUNIT_REMOVE_RETURN_TYPEHINT " value="1" />
</php>

<testsuites>
<testsuite name="Test suite">
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory suffix=".php">./src/</directory>
</whitelist>
</filter>
</phpunit>
File renamed without changes.
File renamed without changes.
File renamed without changes.
19 changes: 6 additions & 13 deletions BreadcrumbTrail/Trail.php → src/BreadcrumbTrail/Trail.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@

namespace APY\BreadcrumbTrailBundle\BreadcrumbTrail;

use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;

class Trail implements \IteratorAggregate, \Countable
Expand All @@ -27,24 +27,22 @@ class Trail implements \IteratorAggregate, \Countable
private $router;

/**
* @var ContainerInterface container
* @var RequestStack
*/
private $container;
private $requestStack;

/**
* @var string Template to render the breadcrumb trail
*/
private $template;

/**
* Constructor.
*
* @param UrlGeneratorInterface $router URL generator class
*/
public function __construct(UrlGeneratorInterface $router, ContainerInterface $container)
public function __construct(UrlGeneratorInterface $router, RequestStack $requestStack)
{
$this->router = $router;
$this->container = $container;
$this->requestStack = $requestStack;
$this->breadcrumbs = new \SplObjectStorage();
}

Expand Down Expand Up @@ -86,12 +84,7 @@ public function add($breadcrumb_or_title, $routeName = null, $routeParameters =
throw new \InvalidArgumentException('The title of a breadcrumb must be a string.');
}

if ($this->container->has('request_stack')) {
$request = $this->container->get('request_stack')->getCurrentRequest();
} else {
// For Symfony < 2.4
$request = $this->container->get('request', ContainerInterface::NULL_ON_INVALID_REFERENCE);
}
$request = $this->requestStack->getCurrentRequest();

if ($request !== null) {
preg_match_all('#\{(?P<variable>\w+).?(?P<function>([\w\.])*):?(?P<parameters>(\w|,| )*)\}#', $breadcrumb_or_title, $matches, PREG_OFFSET_CAPTURE | PREG_SET_ORDER);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public function load(array $configs, ContainerBuilder $container)

$container->setParameter('apy_breadcrumb_trail.template', $config['template']);

$loader = new XmlFileLoader($container, new FileLocator(__DIR__.'/../Resources/config'));
$loader = new XmlFileLoader($container, new FileLocator(__DIR__ . '/../Resources/config'));
$loader->load('services.xml');
}
}
File renamed without changes.
File renamed without changes.
21 changes: 21 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?xml version="1.0" ?>
<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services https://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<defaults public="false" autoconfigure="true" autowire="true" />

<prototype namespace="APY\BreadcrumbTrailBundle\" resource="../../*" exclude="../../{Annotation,Resources}"/>

<service id="apy_breadcrumb_trail" class="APY\BreadcrumbTrailBundle\BreadcrumbTrail\Trail" public="true">
<call method="setTemplate">
<argument>%apy_breadcrumb_trail.template%</argument>
</call>
</service>

<service id="apy_breadcrumb_trail.annotation.listener" class="APY\BreadcrumbTrailBundle\EventListener\BreadcrumbListener">
<tag name="kernel.event_listener" event="kernel.controller" method="onKernelController" priority="-1" />
</service>
</services>
</container>
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
22 changes: 22 additions & 0 deletions tests/BundleInitializationTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<?php

namespace APY\BreadcrumbTrailBundle;

use Nyholm\BundleTest\BaseBundleTestCase;

class BundleInitializationTest extends BaseBundleTestCase
{
protected function getBundleClass()
{
return APYBreadcrumbTrailBundle::class;
}

public function testServicesAreRegistrated()
{
$this->bootKernel();
$container = $this->getContainer();

$this->assertTrue($container->has('apy_breadcrumb_trail'));
$this->assertTrue($container->hasParameter('apy_breadcrumb_trail.template'));
}
}
27 changes: 27 additions & 0 deletions tests/ExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace APY\BreadcrumbTrailBundle;

use APY\BreadcrumbTrailBundle\BreadcrumbTrail\Trail;
use APY\BreadcrumbTrailBundle\DependencyInjection\APYBreadcrumbTrailExtension;
use APY\BreadcrumbTrailBundle\EventListener\BreadcrumbListener;
use APY\BreadcrumbTrailBundle\Twig\BreadcrumbTrailExtension;
use Matthias\SymfonyDependencyInjectionTest\PhpUnit\AbstractExtensionTestCase;

class ExtensionTest extends AbstractExtensionTestCase
{
protected function getContainerExtensions(): array
{
return [
new APYBreadcrumbTrailExtension()
];
}

public function testContainerHasExtension()
{
$this->load();
$this->assertContainerBuilderHasService(Trail::class);
$this->assertContainerBuilderHasService(BreadcrumbListener::class);
$this->assertContainerBuilderHasService(BreadcrumbTrailExtension::class);
}
}
41 changes: 41 additions & 0 deletions tests/Twig/BreadcrumbTrailExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

namespace APY\BreadcrumbTrailBundle\Twig;

use APY\BreadcrumbTrailBundle\APYBreadcrumbTrailBundle;
use Nyholm\BundleTest\BaseBundleTestCase;
use Nyholm\BundleTest\CompilerPass\PublicServicePass;
use Symfony\Bundle\TwigBundle\TwigBundle;
use Twig\Environment;

/**
* @coversDefaultClass \APY\BreadcrumbTrailBundle\Twig\BreadcrumbTrailExtension
*/
class BreadcrumbTrailExtensionTest extends BaseBundleTestCase
{
protected function getBundleClass()
{
return APYBreadcrumbTrailBundle::class;
}

protected function setUp(): void
{
$this->addCompilerPass(new PublicServicePass('|twig|'));

$kernel = $this->createKernel();
$kernel->addBundle(TwigBundle::class);
$kernel->boot();
}

public function testTwigFunctionGetsRegistered()
{
$container = $this->getContainer();

/** @var Environment $twig */
$twig = $container->get('twig');

self::assertNotNull(
$twig->getFunction('apy_breadcrumb_trail_render')
);
}
}

0 comments on commit dfe01b4

Please sign in to comment.