Skip to content

Commit

Permalink
[ResourceWatcher] created basic resource event listener object
Browse files Browse the repository at this point in the history
  • Loading branch information
everzet committed Apr 16, 2012
1 parent 443c2b4 commit b9f0ab8
Show file tree
Hide file tree
Showing 2 changed files with 133 additions and 0 deletions.
76 changes: 76 additions & 0 deletions src/Symfony/Component/ResourceWatcher/Event/EventListener.php
@@ -0,0 +1,76 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Component\ResourceWatcher\Event;

use Symfony\Component\Config\Resource\ResourceInterface;

/**
* Resource change listener.
*
* @author Konstantin Kudryashov <ever.zet@gmail.com>
*/
class EventListener implements EventListenerInterface
{
private $resource;
private $callback;
private $eventsMask;

/**
* Initializes listener.
*
* @param ResourceInterface $resource resource to listen
* @param callable $callback callback to call on event
* @param integer $eventsMask event types to listen
*/
public function __construct(ResourceInterface $resource, $callback, $eventsMask)
{
if (!is_callable($callback)) {
throw new \InvalidArgumentException(
'EventListener\'s second argument should be callable.'
);
}

$this->resource = $resource;
$this->callback = $callback;
$this->eventsMask = $eventsMask;
}

/**
* Returns listening resource.
*
* @return ResourceInterface
*/
public function getResource()
{
return $this->resource;
}

/**
* Returns callback.
*
* @return callable
*/
public function getCallback()
{
return $this->callback;
}

/**
* Checks whether listener can handle provided resource event.
*
* @param Event $event
*/
public function handles(Event $event)
{
return 0 !== ($this->eventsMask & $event->getType());
}
}
@@ -0,0 +1,57 @@
<?php

/*
* This file is part of the Symfony package.
*
* (c) Fabien Potencier <fabien@symfony.com>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/

namespace Symfony\Tests\Component\ResourceWatcher\Event;

use Symfony\Component\Config\Resource\FileResource;
use Symfony\Component\ResourceWatcher\Event\Event;
use Symfony\Component\ResourceWatcher\Event\EventListener;

class EventListenerTest extends \PHPUnit_Framework_TestCase
{
public function testConstructAndGetters()
{
$listener = new EventListener($res = new FileResource(__FILE__), $cb = function(){}, Event::CREATED);

$this->assertSame($res, $listener->getResource());
$this->assertSame($cb, $listener->getCallback());
}

public function testHandles()
{
$res = new FileResource(__FILE__);
$cb = function(){};

$listener = new EventListener($res, $cb, Event::CREATED);

$this->assertTrue($listener->handles(new Event(1, $res, $type = Event::CREATED)));
$this->assertFalse($listener->handles(new Event(1, $res, $type = Event::MODIFIED)));
$this->assertFalse($listener->handles(new Event(1, $res, $type = Event::DELETED)));

$listener = new EventListener($res, $cb, Event::CREATED | Event::DELETED);

$this->assertTrue($listener->handles(new Event(1, $res, $type = Event::CREATED)));
$this->assertFalse($listener->handles(new Event(1, $res, $type = Event::MODIFIED)));
$this->assertTrue($listener->handles(new Event(1, $res, $type = Event::DELETED)));

$listener = new EventListener($res, $cb, Event::ALL);

$this->assertTrue($listener->handles(new Event(1, $res, $type = Event::CREATED)));
$this->assertTrue($listener->handles(new Event(1, $res, $type = Event::MODIFIED)));
$this->assertTrue($listener->handles(new Event(1, $res, $type = Event::DELETED)));

$listener = new EventListener($res, $cb, Event::DELETED);

$this->assertFalse($listener->handles(new Event(1, $res, $type = Event::CREATED)));
$this->assertFalse($listener->handles(new Event(1, $res, $type = Event::MODIFIED)));
$this->assertTrue($listener->handles(new Event(1, $res, $type = Event::DELETED)));
}
}

0 comments on commit b9f0ab8

Please sign in to comment.