Skip to content

Commit

Permalink
Merge pull request #150 from nicolasmure/events-unit-tests
Browse files Browse the repository at this point in the history
Events unit tests
  • Loading branch information
helios-ag committed Aug 6, 2015
2 parents c1ebef8 + 71c1afb commit 2d60d30
Show file tree
Hide file tree
Showing 9 changed files with 203 additions and 1 deletion.
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
composer.lock
Tests/cache
Tests/log

Tests/Functional/cache
Tests/Functional/log
70 changes: 70 additions & 0 deletions Tests/Controller/ElFinderControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
<?php

namespace FM\ElfinderBundle\Tests\Controller;

use Symfony\Bundle\FrameworkBundle\Test\WebTestCase;
use FM\ElfinderBundle\Event\ElFinderEvents;
use FM\ElfinderBundle\Controller\ElFinderController;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\DependencyInjection\Container;

class ElFinderControllerTest extends WebTestCase
{
public function setUp()
{
parent::setUp();
$this->container = static::createClient()->getContainer();
}

public function testDispatchedPrePostExecutionEvents()
{
$preExecCount = 0;
$postExecCount = 0;
// storing the real event dispatcher
$eventDispatcher = $this->container->get('event_dispatcher');
// creating a container with mocked services for this test
$container = $this->getContainer($eventDispatcher);
// adding listeners
$eventDispatcher->addListener(ElFinderEvents::PRE_EXECUTION, $preExecListener = function (ElFinderEvents $e) use (&$preExecCount) {
$preExecCount++;
});
$eventDispatcher->addListener(ElFinderEvents::POST_EXECUTION, $postExecListener = function (ElFinderEvents $e) use (&$postExecCount) {
$postExecCount++;
});

$controller = new ElFinderController();
$controller->setContainer($container);
$controller->loadAction(new Request(array('cmd' => 'info')), 'default', null);

$this->assertGreaterThan(0, $preExecCount);
$this->assertGreaterThan(0, $postExecCount);
}

private function getContainer($eventDispatcher)
{
$container = new Container();
$container->set('fm_elfinder.loader', $this->getElFinderLoaderMock());
$container->set('http_kernel', $this->getHttpKernelMock());
$container->set('event_dispatcher', $eventDispatcher);
return $container;
}

private function getElFinderLoaderMock()
{
$stub = $this->getMockBuilder('FM\ElfinderBundle\Loader\ElFinderLoader')
->disableOriginalConstructor()
->getMock();
$stub->expects($this->once())
->method('load')
->willReturn(array());
return $stub;
}

private function getHttpKernelMock()
{
$stub = $this->getMockBuilder('Symfony\Component\HttpKernel\HttpKernelInterface')
->disableOriginalConstructor()
->getMock();
return $stub;
}
}
21 changes: 21 additions & 0 deletions Tests/Event/ElFinderPostExecutionEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

namespace FM\ElfinderBundle\Tests\Event;

use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\HttpFoundation\Request;
use FM\ElfinderBundle\Event\ElFinderPostExecutionEvent;

class ElFinderPostExecutionEventTest extends TestCase
{
public function testHasErrors()
{
$request = new Request();
$httpKernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
$event = new ElFinderPostExecutionEvent($request, $httpKernel, 'testInstance', 'testHomeFolder', array());
$this->assertEquals(false, $event->hasErrors());

$event = new ElFinderPostExecutionEvent($request, $httpKernel, 'testInstance', 'testHomeFolder', array('error' => true));
$this->assertEquals(true, $event->hasErrors());
}
}
34 changes: 34 additions & 0 deletions Tests/Event/ElFinderPreExecutionEventTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
<?php

namespace FM\ElfinderBundle\Tests\Event;

use Symfony\Bundle\FrameworkBundle\Tests\TestCase;
use Symfony\Component\HttpFoundation\Request;
use FM\ElfinderBundle\Event\ElFinderPreExecutionEvent;

class ElFinderPreExecutionEventTest extends TestCase
{
public function testGetCommand()
{
$command = 'rm';
$request = new Request(array('cmd' => $command));
$httpKernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
$event = new ElFinderPreExecutionEvent($request, $httpKernel, 'testInstance', 'testHomeFolder');
$this->assertEquals($command, $event->getCommand());
}

public function testSubRequest()
{
$request = new Request(array('cmd' => 'info'));
$httpKernel = $this->getMock('Symfony\Component\HttpKernel\HttpKernelInterface');
$httpKernel
->expects($this->once())
->method('handle');
$event = new ElFinderPreExecutionEvent($request, $httpKernel, 'testInstance', 'testHomeFolder');

$jsonResponse = $event->subRequest(array(
'instance' => $event->getInstance(),
'homeFolder' => $event->getHomeFolder()
), $request->query->all());
}
}
23 changes: 23 additions & 0 deletions Tests/Functional/AppKernel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php

use Symfony\Component\HttpKernel\Kernel;
use Symfony\Component\Config\Loader\LoaderInterface;

class AppKernel extends Kernel
{
public function registerBundles()
{
$bundles = array(
new Symfony\Bundle\FrameworkBundle\FrameworkBundle(),
new Symfony\Bundle\TwigBundle\TwigBundle(),
new FM\ElfinderBundle\FMElfinderBundle(),
);

return $bundles;
}

public function registerContainerConfiguration(LoaderInterface $loader)
{
$loader->load(__DIR__.'/config/config_'.$this->getEnvironment().'.yml');
}
}
39 changes: 39 additions & 0 deletions Tests/Functional/config/config.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
framework:
secret: "thisismysecret"
router:
resource: "%kernel.root_dir%/config/routing.yml"
strict_requirements: ~
form: ~
csrf_protection: ~
validation: { enable_annotations: true }
templating:
engines: ['twig']
default_locale: "en"
trusted_hosts: ~
trusted_proxies: ~
session:
# handler_id set to null will use default session handler from php.ini
handler_id: ~
fragments: ~
http_method_override: true


# ElFinder file manager
fm_elfinder:
instances:
default:
locale: en # defaults to current request locale
editor: custom # other options are tinymce, tinymce4, form, custom and simple,
cors_support: true # full symfony life cycle
fullscreen: true # defaults true, applies to simple and ckeditor editors
include_assets: true # disable if you want to handle loading of the javascript and css assets yourself
connector:
debug: false # defaults to false
roots: # at least one root must be defined
uploads:
show_hidden: false # defaults to false
driver: LocalFileSystem
volume_id: 1
path: uploads


9 changes: 9 additions & 0 deletions Tests/Functional/config/config_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
imports:
- { resource: config.yml }

framework:
test: ~
session:
storage_id: session.storage.mock_file
profiler:
collect: false
4 changes: 4 additions & 0 deletions Tests/Functional/config/routing.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
# ElFinder file manager
elfinder:
resource: "@FMElfinderBundle/Resources/config/routing.yml"

1 change: 1 addition & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
],
"require": {
"php": ">=5.3.3",
"symfony/symfony": "~2.4",
"symfony/framework-bundle": "~2.4",
"symfony/twig-bundle": "~2.4",
"helios-ag/fm-elfinder-php-connector": "~2.0",
Expand Down

0 comments on commit 2d60d30

Please sign in to comment.