Skip to content

Commit

Permalink
✅ add Dependency injection and Helper tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Leny BERNARD committed Nov 3, 2016
1 parent 2c58770 commit d52c53f
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 9 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor/*
composer.lock
26 changes: 26 additions & 0 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
language: php

sudo: false

cache:
directory:
- $HOME/.composer/cache/files

php:
- 5.3
- 5.4
- 5.5
- 5.6
- 7.0
- hhvm

matrix:
include:
- php: 5.3
env: COMPOSER_FLAGS='--prefer-lowest --prefer-stable'
- php: 5.6
env: DEPENDENCIES='dev'

install: composer update $COMPOSER_FLAGS

script: phpunit -v
107 changes: 107 additions & 0 deletions Tests/DependencyInjection/AlertifyExtensionTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php

namespace Troopers\AlertifyBundle\Tests\DependencyInjection;

use Troopers\AlertifyBundle\DependencyInjection\TroopersAlertifyExtension;
use Symfony\Component\DependencyInjection\ContainerBuilder;

class AlertifyExtensionTest extends \PHPUnit_Framework_TestCase
{
public function testDefault()
{
$container = new ContainerBuilder();
$loader = new TroopersAlertifyExtension();
$loader->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'));
}
}
61 changes: 61 additions & 0 deletions Tests/Helper/AlertifyHelperTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace Troopers\AlertifyBundle\Tests\Helper;

use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
use Symfony\Component\HttpFoundation\Session\Session;
use Troopers\AlertifyBundle\Helper\AlertifyHelper;

class AlertifyHelperTest extends \PHPUnit_Framework_TestCase
{
public function testFlashBagPopulate()
{
$session = $this->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());
}
}
11 changes: 2 additions & 9 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -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": [
Expand All @@ -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"
}
20 changes: 20 additions & 0 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit colors="true" bootstrap="vendor/autoload.php">
<testsuites>
<testsuite name="TroopersAlertifyBundle Test Suite">
<directory suffix="Test.php">./Tests/</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./Resources</directory>
<directory>./Tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
</filter>
</phpunit>

0 comments on commit d52c53f

Please sign in to comment.