add symfony bundle tests

This commit is contained in:
Khoubza Younes
2020-12-12 08:37:30 +01:00
parent de14f39752
commit e11bdf2f15
10 changed files with 484 additions and 3 deletions
+9 -1
View File
@@ -13,7 +13,15 @@
],
"require": {
"php": ">=5.3",
"ext-json": "*"
"ext-json": "*",
"symfony/config": "^2.7|^3.0|^4.0|^5.0",
"symfony/dependency-injection": "^2.7|^3.0|^4.0|^5.0",
"symfony/http-kernel": "^2.7|^3.0|^4.0|^5.0",
"symfony/yaml": "^2.7|^3.0|^4.0|^5.0",
"twig/twig": "^1.34|^2.0|^3.0"
},
"require-dev": {
"phpunit/phpunit": "4.8.*"
},
"autoload": {
"psr-4": {
@@ -25,7 +25,7 @@ final class Configuration implements ConfigurationInterface
->children()
->scalarNode('default')
->cannotBeEmpty()
->defaultValue('notyf')
->defaultValue('toastr')
->end()
->arrayNode('scripts')
->prototype('scalar')->end()
@@ -0,0 +1,63 @@
<?php
namespace Flasher\Symfony\Tests\DependencyInjection\Compiler;
use Flasher\Symfony\DependencyInjection\FlasherExtension;
use Flasher\Symfony\FlasherBundle;
use Flasher\Symfony\Tests\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
class EventSubscriberCompilerPassTest extends TestCase
{
public function testProcess()
{
$container = $this->getContainer();
$this->assertTrue($container->hasDefinition('test_subscriber'));
$flasher = $container->getDefinition('test_subscriber');
$this->assertTrue($flasher->hasTag('flasher.event_subscriber'));
$manager = $container->getDefinition('flasher.event_dispatcher');
$calls = $manager->getMethodCalls();
$this->assertCount(4, $calls);
$this->assertEquals('addSubscriber', $calls[0][0]);
$this->assertEquals('test_subscriber', $calls[0][1][0]);
$this->assertEquals('addSubscriber', $calls[1][0]);
$this->assertEquals('flasher.event_listener.filter_listener', $calls[1][1][0]);
$this->assertEquals('addSubscriber', $calls[2][0]);
$this->assertEquals('flasher.event_listener.post_flush_listener', $calls[2][1][0]);
$this->assertEquals('addSubscriber', $calls[3][0]);
$this->assertEquals('flasher.event_listener.stamps_listener', $calls[3][1][0]);
}
private function getContainer()
{
$container = new ContainerBuilder();
$flasher = new Definition('test_subscriber');
$flasher->addTag('flasher.event_subscriber');
$container->setDefinition('test_subscriber', $flasher);
$extension = new FlasherExtension();
$container->registerExtension($extension);
$bundle = new FlasherBundle();
$bundle->build($container);
$container->getCompilerPassConfig()->setOptimizationPasses(array());
$container->getCompilerPassConfig()->setRemovingPasses(array());
$container->getCompilerPassConfig()->setAfterRemovingPasses(array());
$container->loadFromExtension('flasher', array());
$container->compile();
return $container;
}
}
@@ -0,0 +1,53 @@
<?php
namespace Flasher\Symfony\Tests\DependencyInjection\Compiler;
use Flasher\Symfony\DependencyInjection\FlasherExtension;
use Flasher\Symfony\FlasherBundle;
use Flasher\Symfony\Tests\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
use Symfony\Component\DependencyInjection\Definition;
class FactoryCompilerPassTest extends TestCase
{
public function testProcess()
{
$container = $this->getContainer();
$this->assertTrue($container->hasDefinition('test_flasher'));
$flasher = $container->getDefinition('test_flasher');
$this->assertTrue($flasher->hasTag('flasher.factory'));
$manager = $container->getDefinition('flasher');
$calls = $manager->getMethodCalls();
$this->assertCount(1, $calls);
$this->assertEquals('addFactory', $calls[0][0]);
$this->assertEquals('test_flasher', $calls[0][1][0]);
}
private function getContainer()
{
$container = new ContainerBuilder();
$flasher = new Definition('test_flasher');
$flasher->addTag('flasher.factory', array('alias' => 'test_flasher'));
$container->setDefinition('test_flasher', $flasher);
$extension = new FlasherExtension();
$container->registerExtension($extension);
$bundle = new FlasherBundle();
$bundle->build($container);
$container->getCompilerPassConfig()->setOptimizationPasses(array());
$container->getCompilerPassConfig()->setRemovingPasses(array());
$container->getCompilerPassConfig()->setAfterRemovingPasses(array());
$container->loadFromExtension('flasher', array());
$container->compile();
return $container;
}
}
@@ -0,0 +1,111 @@
<?php
namespace Flasher\Symfony\Tests\DependencyInjection;
use Flasher\Symfony\DependencyInjection\Configuration;
use Flasher\Symfony\Tests\TestCase;
use Symfony\Component\Config\Definition\Processor;
class ConfigurationTest extends TestCase
{
public function testDefaultConfig()
{
$config = $this->process(array());
$expected = array(
'default' => 'toastr',
'scripts' => array(
'/bundles/flasher/flasher.js',
),
'styles' => array(),
'auto_create_from_session' => true,
'types_mapping' => array(
'success' => array('success'),
'error' => array('error', 'danger'),
'warning' => array('warning', 'alarm'),
'info' => array('info', 'notice', 'alert'),
),
'adapters' => array(),
);
$this->assertSame($expected, $config);
}
public function testSimpleConfig()
{
$config = $this->process(array(array(
'default' => 'notyf',
'adapters' => array(
'notyf' => array(
'scripts' => array(
'jquery.js',
'notyf.js'
),
'styles' => array(
'notyf.css'
),
'options' => array(
'timeout' => 5000,
'position' => 'top-right'
)
)
),
'scripts' => array(),
'styles' => array(
'/bundles/flasher/flasher.css',
)
)));
$expected = array(
'default' => 'notyf',
'scripts' => array(),
'styles' => array(
'/bundles/flasher/flasher.css',
),
'auto_create_from_session' => true,
'types_mapping' => array(
'success' => array('success'),
'error' => array('error', 'danger'),
'warning' => array('warning', 'alarm'),
'info' => array('info', 'notice', 'alert'),
),
'adapters' => array(
'notyf' => array(
'scripts' => array(
'jquery.js',
'notyf.js'
),
'styles' => array(
'notyf.css'
),
'options' => array(
'timeout' => 5000,
'position' => 'top-right'
)
)
),
);
$this->assertEquals($expected, $config);
}
public function testEmptyDefault()
{
$this->setExpectedException('\Symfony\Component\Config\Definition\Exception\InvalidConfigurationException', 'The path "flasher.default" cannot contain an empty value, but got "".');
$this->process(array(array('default' => '')));
}
/**
* Processes an array of configurations and returns a compiled version.
*
* @param array $configs An array of raw configurations
*
* @return array A normalized array
*/
private function process($configs)
{
$processor = new Processor();
return $processor->processConfiguration(new Configuration(), $configs);
}
}
@@ -0,0 +1,45 @@
<?php
namespace Flasher\Symfony\Tests\DependencyInjection;
use Flasher\Symfony\DependencyInjection\FlasherExtension;
use Flasher\Symfony\FlasherBundle;
use PHPUnit\Framework\TestCase;
use Symfony\Component\DependencyInjection\ContainerBuilder;
class FlasherExtensionTest extends TestCase
{
public function testContainerContainFlasherServices()
{
$container = $this->getRawContainer();
$container->loadFromExtension('flasher', array());
$container->compile();
$this->assertTrue($container->has('flasher'));
}
private function getRawContainer()
{
$container = new ContainerBuilder();
$extension = new FlasherExtension();
$container->registerExtension($extension);
$bundle = new FlasherBundle();
$bundle->build($container);
$container->getCompilerPassConfig()->setOptimizationPasses(array());
$container->getCompilerPassConfig()->setRemovingPasses(array());
$container->getCompilerPassConfig()->setAfterRemovingPasses(array());
return $container;
}
private function getContainer()
{
$container = $this->getRawContainer();
$container->compile();
return $container;
}
}
+129
View File
@@ -0,0 +1,129 @@
<?php
namespace Flasher\Symfony\Tests\Storage;
use Flasher\Prime\Envelope;
use Flasher\Prime\Notification\Notification;
use Flasher\Prime\Stamp\PriorityStamp;
use Flasher\Prime\Stamp\UuidStamp;
use Flasher\Symfony\Storage\Storage;
use PHPUnit\Framework\TestCase;
use Symfony\Component\HttpFoundation\Session\Session;
use Symfony\Component\HttpFoundation\Session\Storage\MockArraySessionStorage;
class StorageTest extends TestCase
{
/**
* @var Storage
*/
private $storage;
protected function setUp()
{
$this->storage = new Storage(new Session(new MockArraySessionStorage()));
}
protected function tearDown()
{
$this->storage = null;
}
public function testInitialState()
{
$this->assertSame(array(), $this->storage->all());
}
public function testAddEnvelope()
{
$envelope = new Envelope(new Notification());
$this->storage->add($envelope);
$this->assertSame(array($envelope), $this->storage->all());
}
public function testAddMultipleEnvelopes()
{
$envelopes = array(
new Envelope(new Notification()),
new Envelope(new Notification()),
);
$this->storage->add($envelopes);
$this->assertSame($envelopes, $this->storage->all());
}
public function testUpdateEnvelopes()
{
$envelopes = array(
new Envelope(new Notification(), array(
new UuidStamp(),
)),
new Envelope(new Notification(), array(
new UuidStamp(),
)),
);
$this->storage->add($envelopes);
$this->assertSame($envelopes, $this->storage->all());
$envelopes[1]->withStamp(new PriorityStamp(1));
$this->storage->update($envelopes[1]);
$this->assertSame($envelopes, $this->storage->all());
$this->assertInstanceOf('Flasher\Prime\Stamp\PriorityStamp', $envelopes[1]->get('Flasher\Prime\Stamp\PriorityStamp'));
}
public function testRemoveEnvelopes()
{
$envelopes = array(
new Envelope(new Notification(), array(
new UuidStamp(),
)),
new Envelope(new Notification(), array(
new UuidStamp(),
)),
);
$this->storage->add($envelopes);
$this->assertSame($envelopes, $this->storage->all());
$this->storage->remove($envelopes[1]);
$this->assertSame(array($envelopes[0]), $this->storage->all());
}
public function testRemoveMultipleEnvelopes()
{
$envelopes = array(
new Envelope(new Notification(), array(
new UuidStamp(),
)),
new Envelope(new Notification(), array(
new UuidStamp(),
)),
);
$this->storage->add($envelopes);
$this->assertSame($envelopes, $this->storage->all());
$this->storage->remove($envelopes);
$this->assertSame(array(), $this->storage->all());
}
public function testClearAllEnvelopes()
{
$envelopes = array(
new Envelope(new Notification(), array(
new UuidStamp(),
)),
new Envelope(new Notification(), array(
new UuidStamp(),
)),
);
$this->storage->add($envelopes);
$this->assertSame($envelopes, $this->storage->all());
$this->storage->clear();
$this->assertSame(array(), $this->storage->all());
}
}
+40
View File
@@ -0,0 +1,40 @@
<?php
namespace Flasher\Symfony\Tests;
use ReflectionClass;
class TestCase extends \PHPUnit\Framework\TestCase
{
public function setExpectedException($exceptionName, $exceptionMessage = '', $exceptionCode = null)
{
if (method_exists($this, 'expectException')) {
$this->expectException($exceptionName);
$this->expectExceptionMessage($exceptionMessage);
} else {
parent::setExpectedException($exceptionName, $exceptionMessage, $exceptionCode);
}
}
/**
* Call protected/private method of a class.
*
* @param object $object Instantiated object that we will run method on.
* @param string $methodName Method name to call.
* @param array|mixed $parameters array of parameters to pass into method.
*
* @return mixed method return
*
* @throws \ReflectionException
*/
protected function callMethod(&$object, $methodName, $parameters = array())
{
$reflection = new ReflectionClass(get_class($object));
$method = $reflection->getMethod($methodName);
$method->setAccessible(true);
$parameters = is_array($parameters) ? $parameters : array_slice(func_get_args(), 2);
return $method->invokeArgs($object, $parameters);
}
}
@@ -0,0 +1,33 @@
<?php
namespace Flasher\Symfony\Tests\Twig;
use Flasher\Prime\Renderer\RendererInterface;
use Flasher\Symfony\Tests\TestCase;
use Flasher\Symfony\Twig\FlasherTwigExtension;
use Twig\Environment;
use Twig\Loader\ArrayLoader;
class FlasherTwigExtensionTest extends TestCase
{
public function testFlasherRenderInstanceOfFunctionExpression()
{
$expected = "<script>toastr.success('success title')</script>";
$renderer = $this->getMockBuilder('Flasher\Prime\Renderer\RendererInterface')->getMock();
$renderer->method('render')->willReturn($expected);
$this->assertEquals($expected, $this->render('{{ flasher_render() }}', $renderer));
}
private function render($template, RendererInterface $renderer)
{
$twig = new Environment(new ArrayLoader(array('template' => $template)), array(
'debug' => true, 'cache' => false, 'autoescape' => 'html', 'optimizations' => 0,
));
$twig->addExtension(new FlasherTwigExtension($renderer));
return $twig->render('template');
}
}
@@ -2,7 +2,6 @@
namespace Flasher\Symfony\Twig;
use Flasher\Prime\Renderer\Adapter\HtmlPresenter;
use Flasher\Prime\Renderer\RendererInterface;
use Twig\Extension\AbstractExtension;
use Twig\TwigFunction;