From 098f8b2372e6e456afac61d5d672e0604fef9e82 Mon Sep 17 00:00:00 2001 From: Al Ganiev Date: Mon, 9 Nov 2015 20:03:49 +1000 Subject: [PATCH 1/2] increased number of tests --- .travis.yml | 2 + Configuration/ElFinderConfigurationReader.php | 4 +- Loader/ElFinderLoader.php | 11 +- .../ElFinderConfigurationReaderTest.php | 143 ++++++++++++++++++ .../Compiler/TwigFormPassTest.php | 26 ++++ .../FMElfinderExtensionTest.php | 4 + .../Event/ElFinderPostExecutionEventTest.php | 4 + Tests/Event/ElFinderPreExecutionEventTest.php | 5 + Tests/Form/Type/ElFinderTypeTest.php | 50 ++++++ .../Extension/FMElfinderExtensionTest.php | 70 +++++++-- phpunit.xml.dist | 4 +- 11 files changed, 302 insertions(+), 21 deletions(-) create mode 100644 Tests/DependencyInjection/Compiler/TwigFormPassTest.php create mode 100644 Tests/Form/Type/ElFinderTypeTest.php diff --git a/.travis.yml b/.travis.yml index fdfec31..7fe387e 100644 --- a/.travis.yml +++ b/.travis.yml @@ -7,6 +7,8 @@ php: - 7.0 - hhvm +sudo: false + matrix: allow_failures: - php: diff --git a/Configuration/ElFinderConfigurationReader.php b/Configuration/ElFinderConfigurationReader.php index 955c449..35c9996 100644 --- a/Configuration/ElFinderConfigurationReader.php +++ b/Configuration/ElFinderConfigurationReader.php @@ -70,8 +70,8 @@ public function getConfiguration($instance) $options = array(); $options['corsSupport'] = $parameters['cors_support']; $options['debug'] = $parameters['connector']['debug']; - $options['bind'] = $parameters['connector']['binds']; - $options['plugins'] = $parameters['connector']['plugins']; + $options['bind'] = $parameters['connector']['binds']; + $options['plugins'] = $parameters['connector']['plugins']; $options['roots'] = array(); foreach ($parameters['connector']['roots'] as $parameter) { diff --git a/Loader/ElFinderLoader.php b/Loader/ElFinderLoader.php index 9b32ced..4ea1d3e 100644 --- a/Loader/ElFinderLoader.php +++ b/Loader/ElFinderLoader.php @@ -2,14 +2,14 @@ namespace FM\ElfinderBundle\Loader; -use Exception; use FM\ElFinderPHP\Connector\ElFinderConnector; use FM\ElfinderBundle\Bridge\ElFinderBridge; use FM\ElfinderBundle\Model\ElFinderConfigurationProviderInterface; use Symfony\Component\HttpFoundation\Request; /** - * Class ElFinderLoader. + * Class ElFinderLoader + * @package FM\ElfinderBundle\Loader */ class ElFinderLoader { @@ -51,7 +51,7 @@ public function configure() { $configurator = $this->configurator; if (!($configurator instanceof ElFinderConfigurationProviderInterface)) { - throw new Exception('Configurator class must implement ElFinderConfigurationProviderInterface'); + throw new \Exception('Configurator class must implement ElFinderConfigurationProviderInterface'); } $parameters = $configurator->getConfiguration($this->instance); @@ -67,13 +67,14 @@ public function initBridge($instance) { $this->setInstance($instance); $this->config = $this->configure(); - $this->bridge = new ElFinderBridge($config); + $this->bridge = new ElFinderBridge($this->config); } /** * Starts ElFinder. * * @var Request + * @return void|array */ public function load(Request $request) { @@ -97,7 +98,7 @@ public function setInstance($instance) /** * @param \FM\ElfinderBundle\Model\ElFinderConfigurationProviderInterface $configurator */ - public function setConfigurator($configurator) + public function setConfigurator(ElFinderConfigurationProviderInterface $configurator) { $this->configurator = $configurator; } diff --git a/Tests/Configuration/ElFinderConfigurationReaderTest.php b/Tests/Configuration/ElFinderConfigurationReaderTest.php index 39b2d79..62c3b42 100644 --- a/Tests/Configuration/ElFinderConfigurationReaderTest.php +++ b/Tests/Configuration/ElFinderConfigurationReaderTest.php @@ -2,12 +2,155 @@ namespace FM\ElfinderBundle\Tests\Configuration; +use FM\ElfinderBundle\Configuration\ElFinderConfigurationReader; +use Symfony\Component\DependencyInjection\ContainerInterface; + +/** + * Class ElFinderConfigurationReaderTest + * @package FM\ElfinderBundle\Tests\Configuration + */ class ElFinderConfigurationReaderTest extends \PHPUnit_Framework_TestCase { + + /** + * @var ElFinderConfigurationReader + */ + protected $reader; + + /** + * @var \FM\ElFinderPHP\Driver\ElFinderVolumeLocalFileSystem + */ + protected $elFinderVolumeMock; + + + protected function setUp() + { + /** @var \Symfony\Component\DependencyInjection\ContainerInterface|\PHPUnit_Framework_MockObject_MockObject */ + $containerMock = $this->getMock('Symfony\Component\DependencyInjection\ContainerInterface'); + + $this->elFinderVolumeMock = $this->getMock('FM\ElFinderPHP\Driver\ElFinderVolumeLocalFileSystem'); + + $containerMock + ->expects($this->any()) + ->method('has') + ->will($this->returnValueMap(array( + array( + 'elfinder.driver.local', + ContainerInterface::EXCEPTION_ON_INVALID_REFERENCE, + $this->elFinderVolumeMock, + ) + ))); + + /** @var \Symfony\Component\HttpFoundation\RequestStack $requestStack|\PHPUnit_Framework_MockObject_MockObject */ + $requestStack = $this->getMock('Symfony\Component\HttpFoundation\RequestStack'); + /** @var \Symfony\Component\HttpFoundation\Request $requestObject */ + $requestObject = $this->getMock('Symfony\Component\HttpFoundation\Request'); + + $requestObject + ->expects($this->any()) + ->method('get') + ->will($this->returnValue('')); + + /** @var \Symfony\Component\HttpFoundation\ParameterBag $attributesObject */ + $attributesObject = $this->getMock('\Symfony\Component\HttpFoundation\ParameterBag'); + $attributesObject + ->expects($this->any()) + ->method('get') + ->will($this->returnValue('')); + + $requestObject->attributes = $attributesObject; + + $requestStack + ->expects($this->any()) + ->method('getCurrentRequest') + ->will($this->returnValue($requestObject)); + + $params = array( + 'instances' => array( + 'default' => array( + 'cors_support' => '', + 'connector' => array( + 'debug' => '', 'binds' => '', 'plugins'=> '', + 'roots' => array( + 'uploads' => array( + 'flysystem' => array('enabled'=>false), + 'volume_id' => 0, + 'show_hidden' => false, + 'path' => '', + 'driver' => 'LocalFileSystem', + 'glide_url' => '', + 'glide_key' => '', + 'plugins' => '', + 'start_path' => '', + 'alias' => '', + 'mime_detect' => '', + 'mimefile' => '', + 'img_lib' => '', + 'tmb_path' => '', + 'tmb_path_mode' => '', + 'tmb_url' => '', + 'tmb_size' => '', + 'tmb_crop' => '', + 'tmb_bg_color' => '', + 'copy_overwrite' => '', + 'copy_join' => '', + 'copy_from' => '', + 'copy_to' => '', + 'upload_overwrite' => '', + 'upload_allow' => '', + 'upload_deny' => '', + 'upload_max_size' => '', + 'defaults' => '', + 'attributes' => '', + 'accepted_name' => '', + 'disabled_commands' => '', + 'tree_deep' => '', + 'check_subfolders' => '', + 'separator' => '', + 'time_format' => '', + 'archive_mimes' => '', + 'archivers' => '' + ) + ), + ) + ) + ) + ); + + $this->reader = new ElFinderConfigurationReader($params, $requestStack, $containerMock); + } + + protected function tearDown() + { + unset($this->reader); + } + + public function testConfiguration() + { + $configuration = $this->reader->getConfiguration('default'); + $this->assertArrayHasKey('roots', $configuration); + $this->assertArrayHasKey('corsSupport', $configuration); + $this->assertSame('LocalFileSystem', $configuration['roots'][0]['driver']); + } + public function testSubClassOfHelper() { $rc = new \ReflectionClass('FM\ElfinderBundle\Configuration\ElFinderConfigurationReader'); $this->assertTrue($rc->isSubclassOf('FM\ElfinderBundle\Model\ElFinderConfigurationProviderInterface')); } + + public function testAccessHidden() + { + $hiddenPath = '.hiddenPath'; + $this->assertFalse($this->reader->access('read',$hiddenPath, 'dummy', 'dummy')); + $this->assertFalse($this->reader->access('write',$hiddenPath, 'dummy', 'dummy')); + } + + public function testAccessVisible() + { + $visiblePath = 'hiddenPath'; + $this->assertNull($this->reader->access('read',$visiblePath, 'dummy', 'dummy')); + $this->assertNull($this->reader->access('write',$visiblePath, 'dummy', 'dummy')); + } } diff --git a/Tests/DependencyInjection/Compiler/TwigFormPassTest.php b/Tests/DependencyInjection/Compiler/TwigFormPassTest.php new file mode 100644 index 0000000..183af78 --- /dev/null +++ b/Tests/DependencyInjection/Compiler/TwigFormPassTest.php @@ -0,0 +1,26 @@ +process($container); + $this->assertFalse($container->hasParameter('twig.form.resources')); + $container = new ContainerBuilder(); + $container->setParameter('twig.form.resources', array()); + $pass->process($container); + $this->assertEquals(array( + 'FMElfinderBundle:Form:elfinder_widget.html.twig', + ), $container->getParameter('twig.form.resources')); + } +} diff --git a/Tests/DependencyInjection/FMElfinderExtensionTest.php b/Tests/DependencyInjection/FMElfinderExtensionTest.php index 2552723..cf9f315 100644 --- a/Tests/DependencyInjection/FMElfinderExtensionTest.php +++ b/Tests/DependencyInjection/FMElfinderExtensionTest.php @@ -7,6 +7,10 @@ use Symfony\Component\DependencyInjection\ContainerBuilder; use Symfony\Component\Yaml\Parser; +/** + * Class FMElfinderExtensionTest + * @package FM\ElfinderBundle\Tests\DependencyInjection + */ class FMElfinderExtensionTest extends AbstractExtensionTestCase { protected function getContainerExtensions() diff --git a/Tests/Event/ElFinderPostExecutionEventTest.php b/Tests/Event/ElFinderPostExecutionEventTest.php index ea08d47..8239a34 100644 --- a/Tests/Event/ElFinderPostExecutionEventTest.php +++ b/Tests/Event/ElFinderPostExecutionEventTest.php @@ -6,6 +6,10 @@ use Symfony\Component\HttpFoundation\Request; use FM\ElfinderBundle\Event\ElFinderPostExecutionEvent; +/** + * Class ElFinderPostExecutionEventTest + * @package FM\ElfinderBundle\Tests\Event + */ class ElFinderPostExecutionEventTest extends TestCase { public function testHasErrors() diff --git a/Tests/Event/ElFinderPreExecutionEventTest.php b/Tests/Event/ElFinderPreExecutionEventTest.php index 8471546..10697fa 100644 --- a/Tests/Event/ElFinderPreExecutionEventTest.php +++ b/Tests/Event/ElFinderPreExecutionEventTest.php @@ -6,6 +6,10 @@ use Symfony\Component\HttpFoundation\Request; use FM\ElfinderBundle\Event\ElFinderPreExecutionEvent; +/** + * Class ElFinderPreExecutionEventTest + * @package FM\ElfinderBundle\Tests\Event + */ class ElFinderPreExecutionEventTest extends TestCase { public function testGetCommand() @@ -30,5 +34,6 @@ public function testSubRequest() 'instance' => $event->getInstance(), 'homeFolder' => $event->getHomeFolder(), ), $request->query->all()); + } } diff --git a/Tests/Form/Type/ElFinderTypeTest.php b/Tests/Form/Type/ElFinderTypeTest.php new file mode 100644 index 0000000..2c4e72c --- /dev/null +++ b/Tests/Form/Type/ElFinderTypeTest.php @@ -0,0 +1,50 @@ +factory = Forms::createFormFactoryBuilder() + ->addType($elfinderType) + ->getFormFactory(); + } + + public function testDefaults() + { + $form = $this->factory->create('elfinder'); + $view = $form->createView(); + + $this->assertTrue($view->vars['enable']); + } + + public function testDefaultInstance() + { + $form = $this->factory->create('elfinder'); + $view = $form->createView(); + + $this->assertSame('', $view->vars['instance']); + } + + public function testDefaultHomeFolder() + { + $form = $this->factory->create('elfinder'); + $view = $form->createView(); + + $this->assertSame('', $view->vars['homeFolder']); + } + +} \ No newline at end of file diff --git a/Tests/Twig/Extension/FMElfinderExtensionTest.php b/Tests/Twig/Extension/FMElfinderExtensionTest.php index 009cbf3..057da7e 100644 --- a/Tests/Twig/Extension/FMElfinderExtensionTest.php +++ b/Tests/Twig/Extension/FMElfinderExtensionTest.php @@ -18,9 +18,7 @@ class FMElfinderExtensionTest extends \PHPUnit_Framework_TestCase /** @var \Symfony\Component\Routing\RouterInterface|\PHPUnit_Framework_MockObject_MockObject */ protected $routerMock; - /** - * @var FMElfinderTinymceExtension - */ + /** @var \FM\ElfinderBundle\Twig\Extension\FMElFinderExtension */ protected $extension; /** @var \Twig_Environment */ @@ -29,9 +27,6 @@ class FMElfinderExtensionTest extends \PHPUnit_Framework_TestCase /** @var \Twig_Template */ protected $template; - /** - * {@inheritdoc} - */ protected function setUp() { $this->twig = new \Twig_Environment(new \Twig_Loader_Filesystem(array(__DIR__.'/../../../Resources/views/Elfinder/helper'))); @@ -102,9 +97,26 @@ function elFinderBrowser (field_name, url, type, win) { $this->assertSame($this->normalizeOutput($expected), $this->normalizeOutput($testData)); } - /** - * - */ + public function testRenderSummernote() + { + $this->template = $this->twig->loadTemplate('_summernote.html.twig'); + $testData = $this->renderTemplate(array('instance' => 'minimal')); + + $expected = << + function elFinderBrowser(){ + window.open( + "http://localhost/elfinder/minimal", + "", + "width=, height=, resizable=yes, scrollbars=no, status=no, toolbar=no" + ); + return false; + } + +EOF; + + } + public function testName() { $this->assertEquals('fm_elfinder_init', $this->extension->getName()); @@ -139,13 +151,47 @@ protected function normalizeOutput($output) return preg_replace("/\r|\n/", '', str_replace(PHP_EOL, '', str_replace(' ', '', $output))); } - /** - * - */ public function testSubClassOfTwigExtension() { $rc = new \ReflectionClass('FM\ElfinderBundle\Twig\Extension\FMElfinderExtension'); $this->assertTrue($rc->isSubclassOf('Twig_Extension')); } + + /** + * @expectedException \Twig_Error_Runtime + * @expectedExceptionMessage The function can be applied to strings only. + */ + public function testSummernoteInstanceNotString() + { + $this->extension->summernote(array()); + } + + /** + * @expectedException \Twig_Error_Runtime + * @expectedExceptionMessage The function can be applied to strings only. + */ + public function testTinyMCEInstanceNotString() + { + $this->extension->tinymce(array()); + } + + /** + * @expectedException \Twig_Error_Runtime + * @expectedExceptionMessage The function can be applied to strings only. + */ + public function testTinyMCE4InstanceNotString() + { + $this->extension->tinymce4(array()); + } + + public function testGetFunctions() + { + $twigFunctions = $this->extension->getFunctions(); + + foreach ($twigFunctions as $twigFunction) { + $this->assertInstanceOf('Twig_SimpleFunction', $twigFunction); + } + + } } diff --git a/phpunit.xml.dist b/phpunit.xml.dist index d285c89..80c3a90 100644 --- a/phpunit.xml.dist +++ b/phpunit.xml.dist @@ -1,11 +1,11 @@ - + - + ./Tests From d8633fddb442dac36c124704da47378fe35e20c8 Mon Sep 17 00:00:00 2001 From: Al Ganiev Date: Mon, 9 Nov 2015 22:45:46 +1000 Subject: [PATCH 2/2] prefixed Twig_Error_Runtime with backslash --- Twig/Extension/FMElfinderExtension.php | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Twig/Extension/FMElfinderExtension.php b/Twig/Extension/FMElfinderExtension.php index 4ddac31..7e71ee5 100644 --- a/Twig/Extension/FMElfinderExtension.php +++ b/Twig/Extension/FMElfinderExtension.php @@ -84,16 +84,15 @@ public function tinymce4($instance = 'default', $parameters = array('width' => 9 /** * @param string $instance - * @param array $parameters - * - * @throws \Twig_Error_Runtime - * + * @param string $selector + * @param array $parameters * @return mixed + * @throws \Twig_Error_Runtime */ public function summernote($instance = 'default', $selector = '.summenote', $parameters = array('width' => 900, 'height' => 450, 'title' => 'elFinder 2.0')) { if (!is_string($instance)) { - throw new Twig_Error_Runtime('The function can be applied to strings only.'); + throw new \Twig_Error_Runtime('The function can be applied to strings only.'); } return $this->twig->render('FMElfinderBundle:Elfinder/helper:_summernote.html.twig',