diff --git a/composer.json b/composer.json index aa57523e..d214bffb 100644 --- a/composer.json +++ b/composer.json @@ -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": { diff --git a/src/Symfony/DependencyInjection/Configuration.php b/src/Symfony/DependencyInjection/Configuration.php index 50c8aaf7..bb078f51 100644 --- a/src/Symfony/DependencyInjection/Configuration.php +++ b/src/Symfony/DependencyInjection/Configuration.php @@ -25,7 +25,7 @@ final class Configuration implements ConfigurationInterface ->children() ->scalarNode('default') ->cannotBeEmpty() - ->defaultValue('notyf') + ->defaultValue('toastr') ->end() ->arrayNode('scripts') ->prototype('scalar')->end() diff --git a/src/Symfony/Tests/DependencyInjection/Compiler/EventSubscriberCompilerPassTest.php b/src/Symfony/Tests/DependencyInjection/Compiler/EventSubscriberCompilerPassTest.php new file mode 100644 index 00000000..26602964 --- /dev/null +++ b/src/Symfony/Tests/DependencyInjection/Compiler/EventSubscriberCompilerPassTest.php @@ -0,0 +1,63 @@ +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; + } +} diff --git a/src/Symfony/Tests/DependencyInjection/Compiler/FactoryCompilerPassTest.php b/src/Symfony/Tests/DependencyInjection/Compiler/FactoryCompilerPassTest.php new file mode 100644 index 00000000..47c4b9db --- /dev/null +++ b/src/Symfony/Tests/DependencyInjection/Compiler/FactoryCompilerPassTest.php @@ -0,0 +1,53 @@ +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; + } +} diff --git a/src/Symfony/Tests/DependencyInjection/ConfigurationTest.php b/src/Symfony/Tests/DependencyInjection/ConfigurationTest.php new file mode 100644 index 00000000..f7fb969d --- /dev/null +++ b/src/Symfony/Tests/DependencyInjection/ConfigurationTest.php @@ -0,0 +1,111 @@ +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); + } +} diff --git a/src/Symfony/Tests/DependencyInjection/FlasherExtensionTest.php b/src/Symfony/Tests/DependencyInjection/FlasherExtensionTest.php new file mode 100644 index 00000000..8dbc1873 --- /dev/null +++ b/src/Symfony/Tests/DependencyInjection/FlasherExtensionTest.php @@ -0,0 +1,45 @@ +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; + } +} diff --git a/src/Symfony/Tests/Storage/StorageTest.php b/src/Symfony/Tests/Storage/StorageTest.php new file mode 100644 index 00000000..d8e1a625 --- /dev/null +++ b/src/Symfony/Tests/Storage/StorageTest.php @@ -0,0 +1,129 @@ +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()); + } +} diff --git a/src/Symfony/Tests/TestCase.php b/src/Symfony/Tests/TestCase.php new file mode 100644 index 00000000..52079bc3 --- /dev/null +++ b/src/Symfony/Tests/TestCase.php @@ -0,0 +1,40 @@ +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); + } +} diff --git a/src/Symfony/Tests/Twig/FlasherTwigExtensionTest.php b/src/Symfony/Tests/Twig/FlasherTwigExtensionTest.php new file mode 100644 index 00000000..3303aa36 --- /dev/null +++ b/src/Symfony/Tests/Twig/FlasherTwigExtensionTest.php @@ -0,0 +1,33 @@ +toastr.success('success title')"; + + $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'); + } +} diff --git a/src/Symfony/Twig/FlasherTwigExtension.php b/src/Symfony/Twig/FlasherTwigExtension.php index a23ef5a0..bb954c05 100644 --- a/src/Symfony/Twig/FlasherTwigExtension.php +++ b/src/Symfony/Twig/FlasherTwigExtension.php @@ -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;