From 5e1ca9e65b9d1cf318ba41450349e81b6e3bfff6 Mon Sep 17 00:00:00 2001 From: Leny BERNARD Date: Fri, 4 Nov 2016 00:21:34 +0100 Subject: [PATCH 1/2] :white_check_mark: add Dependency injection and Helper tests --- .gitignore | 2 + .travis.yml | 23 ++++ .../AlertifyExtensionTest.php | 107 ++++++++++++++++++ Tests/Helper/AlertifyHelperTest.php | 61 ++++++++++ composer.json | 11 +- phpunit.xml.dist | 20 ++++ 6 files changed, 215 insertions(+), 9 deletions(-) create mode 100644 .gitignore create mode 100644 .travis.yml create mode 100644 Tests/DependencyInjection/AlertifyExtensionTest.php create mode 100644 Tests/Helper/AlertifyHelperTest.php create mode 100644 phpunit.xml.dist diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..f2fddf9 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +vendor/* +composer.lock \ No newline at end of file diff --git a/.travis.yml b/.travis.yml new file mode 100644 index 0000000..6596397 --- /dev/null +++ b/.travis.yml @@ -0,0 +1,23 @@ +language: php + +sudo: false + +cache: + directory: + - $HOME/.composer/cache/files + +php: + - 5.4 + - 5.5 + - 5.6 + - 7.0 + - hhvm + +matrix: + include: + - php: 5.6 + env: DEPENDENCIES='dev' + +install: composer update $COMPOSER_FLAGS + +script: phpunit -v \ No newline at end of file diff --git a/Tests/DependencyInjection/AlertifyExtensionTest.php b/Tests/DependencyInjection/AlertifyExtensionTest.php new file mode 100644 index 0000000..e939dc3 --- /dev/null +++ b/Tests/DependencyInjection/AlertifyExtensionTest.php @@ -0,0 +1,107 @@ +load([[]], $container); + $this->assertTrue($container->hasDefinition('alertify'), 'The alertify service exists'); + $this->assertEquals('Troopers\\AlertifyBundle\\Twig\\Extension\\AlertifyExtension', $container->getParameter('alertify.twig.extension.class')); + $this->assertEquals('Troopers\\AlertifyBundle\\Handler\\AlertifySessionHandler', $container->getParameter('alertify.handler.session.class')); + $this->assertEquals('Troopers\\AlertifyBundle\\Helper\\AlertifyHelper', $container->getParameter('alertify.helper.class')); + $this->assertEquals('Troopers\\AlertifyBundle\\EventListener\\AlertifyListener', $container->getParameter('alertify.event_listener')); + $this->assertEquals([ + 'default' => [ + 'context' => 'front', + 'engine' => 'toastr', + 'layout' => null, + 'translationDomain' => 'alertify' + ], + 'contexts' => [] + ], $container->getParameter('troopers_alertify')); + $this->assertEquals([], $container->getParameter('troopers_alertify.contexts')); + $this->assertEquals('front', $container->getParameter('troopers_alertify.default.context')); + $this->assertEquals('toastr', $container->getParameter('troopers_alertify.default.engine')); + $this->assertEquals(null, $container->getParameter('troopers_alertify.default.layout')); + $this->assertEquals('alertify', $container->getParameter('troopers_alertify.default.translationdomain')); + } + + public function testContexts() + { + $container = new ContainerBuilder(); + $loader = new TroopersAlertifyExtension(); + $loader->load([[ + 'contexts' => [ + 'front' => [ + 'engine' => 'notie', + 'options' => [ + 'animationDelay' => 300 + ] + ], + 'back' => [ + 'engine' => 'pushjs', + 'translationDomain' => 'admin' + ] + ], + 'default' => [ + 'engine' => 'notie' + ]]], $container); + $this->assertSame([ + 'contexts' => [ + 'front' => [ + 'engine' => 'notie', + 'options' => [ + 'animationDelay' => 300 + ], + 'layout' => null, + 'translationDomain' => 'alertify', + 'timeout' => null, + ], + 'back' => [ + 'engine' => 'pushjs', + 'translationDomain' => 'admin', + 'layout' => null, + 'timeout' => null, + 'options' => [] + ] + ], + 'default' => [ + 'engine' => 'notie', + 'context' => 'front', + 'layout' => null, + 'translationDomain' => 'alertify' + ] + , + ], $container->getParameter('troopers_alertify')); + + $this->assertEquals([ + 'front' => [ + 'engine' => 'notie', + 'layout' => null, + 'translationDomain' => 'alertify', + 'timeout' => null, + 'options' => [ + 'animationDelay' => 300 + ] + ], + 'back' => [ + 'engine' => 'pushjs', + 'translationDomain' => 'admin', + 'layout' => null, + 'timeout' => null, + 'options' => [] + ] + ], $container->getParameter('troopers_alertify.contexts')); + $this->assertEquals('front', $container->getParameter('troopers_alertify.default.context')); + $this->assertEquals('notie', $container->getParameter('troopers_alertify.default.engine')); + $this->assertEquals(null, $container->getParameter('troopers_alertify.default.layout')); + $this->assertEquals('alertify', $container->getParameter('troopers_alertify.default.translationdomain')); + } +} \ No newline at end of file diff --git a/Tests/Helper/AlertifyHelperTest.php b/Tests/Helper/AlertifyHelperTest.php new file mode 100644 index 0000000..68f54f2 --- /dev/null +++ b/Tests/Helper/AlertifyHelperTest.php @@ -0,0 +1,61 @@ +getSessionMock(); + $helper = new AlertifyHelper($session); + $helper->alert('Alert', 'success'); + $this->assertEquals([['body' => 'Alert', 'options' => []]], $session->getFlashBag()->get('success')); + + $helper->alert('Alert', 'success'); + $helper->alert('Alert queued', 'success'); + $this->assertEquals([['body' => 'Alert', 'options' => []], ['body' => 'Alert queued', 'options' => []]], $session->getFlashBag()->get('success')); + + $helper->congrat('Congratulation'); + $this->assertEquals([['body' => 'Congratulation', 'options' => []]], $session->getFlashBag()->get('success')); + + $helper->inform('Information'); + $this->assertEquals([['body' => 'Information', 'options' => []]], $session->getFlashBag()->get('info')); + + $helper->warn('Warning'); + $this->assertEquals([['body' => 'Warning', 'options' => []]], $session->getFlashBag()->get('warning')); + + $helper->scold('Danger !'); + $this->assertEquals([['body' => 'Danger !', 'options' => []]], $session->getFlashBag()->get('error')); + } + + public function testFlashBagWithOptions() + { + $session = $this->getSessionMock(); + $helper = new AlertifyHelper($session); + $helper->alert('Alert', 'success', ['backgroundColor' => 'green']); + $this->assertEquals([['body' => 'Alert', 'options' => ['backgroundColor' => 'green']]], $session->getFlashBag()->get('success')); + + $helper->congrat('Congratulation', ['backgroundColor' => 'green']); + $this->assertEquals([['body' => 'Congratulation', 'options' => ['backgroundColor' => 'green']]], $session->getFlashBag()->get('success')); + + $helper->inform('Information', ['backgroundColor' => 'blue']); + $this->assertEquals([['body' => 'Information', 'options' => ['backgroundColor' => 'blue']]], $session->getFlashBag()->get('info')); + + $helper->warn('Warning', ['backgroundColor' => 'orange']); + $this->assertEquals([['body' => 'Warning', 'options' => ['backgroundColor' => 'orange']]], $session->getFlashBag()->get('warning')); + + $helper->scold('Danger !', ['backgroundColor' => 'red']); + $this->assertEquals([['body' => 'Danger !', 'options' => ['backgroundColor' => 'red']]], $session->getFlashBag()->get('error')); + } + + /** + * @return Session + */ + protected function getSessionMock() { + return new Session(new MockArraySessionStorage()); + } +} \ No newline at end of file diff --git a/composer.json b/composer.json index 2d9d680..88ad4f4 100644 --- a/composer.json +++ b/composer.json @@ -2,7 +2,7 @@ "name": "troopers/alertify-bundle", "type": "symfony-bundle", "description": "Symfony AlertifyBundle", - "keywords": ["alert", "bootstrap"], + "keywords": ["alert"], "homepage": "https://github.com/Troopers/TroopersAlertifyBundle", "license": "MIT", "authors": [ @@ -29,12 +29,5 @@ "autoload": { "psr-0": { "Troopers\\AlertifyBundle": "" } }, - "target-dir": "Troopers/AlertifyBundle", - - "minimum-stability": "dev", - "extra": { - "branch-alias": { - "dev-master": "1.0.x-dev" - } - } + "target-dir": "Troopers/AlertifyBundle" } diff --git a/phpunit.xml.dist b/phpunit.xml.dist new file mode 100644 index 0000000..5d643ce --- /dev/null +++ b/phpunit.xml.dist @@ -0,0 +1,20 @@ + + + + + + ./Tests/ + + + + + + ./ + + ./Resources + ./Tests + ./vendor + + + + \ No newline at end of file From 5b46f3f3f78524aa777efd33e0a279f7080e4d1e Mon Sep 17 00:00:00 2001 From: Leny BERNARD Date: Thu, 3 Nov 2016 23:25:59 +0000 Subject: [PATCH 2/2] Applied fixes from StyleCI --- .../AlertifyExtensionTest.php | 81 +++++++++---------- Tests/Helper/AlertifyHelperTest.php | 7 +- 2 files changed, 44 insertions(+), 44 deletions(-) diff --git a/Tests/DependencyInjection/AlertifyExtensionTest.php b/Tests/DependencyInjection/AlertifyExtensionTest.php index e939dc3..7091690 100644 --- a/Tests/DependencyInjection/AlertifyExtensionTest.php +++ b/Tests/DependencyInjection/AlertifyExtensionTest.php @@ -2,8 +2,8 @@ namespace Troopers\AlertifyBundle\Tests\DependencyInjection; -use Troopers\AlertifyBundle\DependencyInjection\TroopersAlertifyExtension; use Symfony\Component\DependencyInjection\ContainerBuilder; +use Troopers\AlertifyBundle\DependencyInjection\TroopersAlertifyExtension; class AlertifyExtensionTest extends \PHPUnit_Framework_TestCase { @@ -19,12 +19,12 @@ public function testDefault() $this->assertEquals('Troopers\\AlertifyBundle\\EventListener\\AlertifyListener', $container->getParameter('alertify.event_listener')); $this->assertEquals([ 'default' => [ - 'context' => 'front', - 'engine' => 'toastr', - 'layout' => null, - 'translationDomain' => 'alertify' + 'context' => 'front', + 'engine' => 'toastr', + 'layout' => null, + 'translationDomain' => 'alertify', ], - 'contexts' => [] + 'contexts' => [], ], $container->getParameter('troopers_alertify')); $this->assertEquals([], $container->getParameter('troopers_alertify.contexts')); $this->assertEquals('front', $container->getParameter('troopers_alertify.default.context')); @@ -40,68 +40,67 @@ public function testContexts() $loader->load([[ 'contexts' => [ 'front' => [ - 'engine' => 'notie', + 'engine' => 'notie', 'options' => [ - 'animationDelay' => 300 - ] + 'animationDelay' => 300, + ], ], 'back' => [ - 'engine' => 'pushjs', - 'translationDomain' => 'admin' - ] + 'engine' => 'pushjs', + 'translationDomain' => 'admin', + ], ], 'default' => [ - 'engine' => 'notie' - ]]], $container); + 'engine' => 'notie', + ], ]], $container); $this->assertSame([ 'contexts' => [ 'front' => [ - 'engine' => 'notie', + 'engine' => 'notie', 'options' => [ - 'animationDelay' => 300 + 'animationDelay' => 300, ], - 'layout' => null, + 'layout' => null, 'translationDomain' => 'alertify', - 'timeout' => null, + 'timeout' => null, ], 'back' => [ - 'engine' => 'pushjs', + 'engine' => 'pushjs', 'translationDomain' => 'admin', - 'layout' => null, - 'timeout' => null, - 'options' => [] - ] + 'layout' => null, + 'timeout' => null, + 'options' => [], + ], ], 'default' => [ - 'engine' => 'notie', - 'context' => 'front', - 'layout' => null, - 'translationDomain' => 'alertify' - ] - , + 'engine' => 'notie', + 'context' => 'front', + 'layout' => null, + 'translationDomain' => 'alertify', + ], ], $container->getParameter('troopers_alertify')); $this->assertEquals([ 'front' => [ - 'engine' => 'notie', - 'layout' => null, + 'engine' => 'notie', + 'layout' => null, 'translationDomain' => 'alertify', - 'timeout' => null, - 'options' => [ - 'animationDelay' => 300 - ] + 'timeout' => null, + 'options' => [ + 'animationDelay' => 300, + ], ], 'back' => [ - 'engine' => 'pushjs', + 'engine' => 'pushjs', 'translationDomain' => 'admin', - 'layout' => null, - 'timeout' => null, - 'options' => [] - ] + 'layout' => null, + 'timeout' => null, + 'options' => [], + ], ], $container->getParameter('troopers_alertify.contexts')); $this->assertEquals('front', $container->getParameter('troopers_alertify.default.context')); $this->assertEquals('notie', $container->getParameter('troopers_alertify.default.engine')); $this->assertEquals(null, $container->getParameter('troopers_alertify.default.layout')); $this->assertEquals('alertify', $container->getParameter('troopers_alertify.default.translationdomain')); } -} \ No newline at end of file +} diff --git a/Tests/Helper/AlertifyHelperTest.php b/Tests/Helper/AlertifyHelperTest.php index 68f54f2..9c78fea 100644 --- a/Tests/Helper/AlertifyHelperTest.php +++ b/Tests/Helper/AlertifyHelperTest.php @@ -2,8 +2,8 @@ namespace Troopers\AlertifyBundle\Tests\Helper; -use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Symfony\Component\HttpFoundation\Session\Session; +use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage; use Troopers\AlertifyBundle\Helper\AlertifyHelper; class AlertifyHelperTest extends \PHPUnit_Framework_TestCase @@ -55,7 +55,8 @@ public function testFlashBagWithOptions() /** * @return Session */ - protected function getSessionMock() { + protected function getSessionMock() + { return new Session(new MockArraySessionStorage()); } -} \ No newline at end of file +}