Completed
Push — master ( 761398...5ee568 )
by Julien
02:37
created

DispatcherTest::testListenerObject()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 6
rs 9.4285
cc 1
eloc 4
nc 1
nop 0
1
<?php
0 ignored issues
show
Coding Style Compatibility introduced by
For compatibility and reusability of your code, PSR1 recommends that a file should introduce either new symbols (like classes, functions, etc.) or have side-effects (like outputting something, or including other files), but not both at the same time. The first symbol is defined on line 12 and the first side effect is on line 5.

The PSR-1: Basic Coding Standard recommends that a file should either introduce new symbols, that is classes, functions, constants or similar, or have side effects. Side effects are anything that executes logic, like for example printing output, changing ini settings or writing to a file.

The idea behind this recommendation is that merely auto-loading a class should not change the state of an application. It also promotes a cleaner style of programming and makes your code less prone to errors, because the logic is not spread out all over the place.

To learn more about the PSR-1, please see the PHP-FIG site on the PSR-1.

Loading history...
2
3
namespace Fwk\Events;
4
5
if(!class_exists('\Fwk\Events\Dispatcher'))
0 ignored issues
show
Coding Style introduced by
Please always use braces to surround the code block of IF statements.
Loading history...
6
    require_once __DIR__ .'/../src/Dispatcher.php';
7
8
if(!class_exists('\Fwk\Events\Event'))
0 ignored issues
show
Coding Style introduced by
Please always use braces to surround the code block of IF statements.
Loading history...
9
    require_once __DIR__ .'/../src/Event.php';
10
11
12
class MyListenerObj 
13
{
14
    public function onTestEvent($event)
15
    {
16
        $event->setProcessed(true);
17
    }
18
    
19
    public function notAListener()
0 ignored issues
show
Coding Style introduced by
function notAListener() does not seem to conform to the naming convention (^(?:is|has|should|may|su...ster|unregister|exists)).

This check examines a number of code elements and verifies that they conform to the given naming conventions.

You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.

Loading history...
20
    {
21
        return true;
22
    }
23
}
24
25
/**
26
 * Test class for EventDispatcher.
27
 */
28
class DispatcherTest extends \PHPUnit_Framework_TestCase {
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class should be in its own file to aid autoloaders.

Having each class in a dedicated file usually plays nice with PSR autoloaders and is therefore a well established practice. If you use other autoloaders, you might not want to follow this rule.

Loading history...
29
30
    /**
31
     * @var Dispatcher
32
     */
33
    protected $object;
34
35
    /**
36
     * Sets up the fixture, for example, opens a network connection.
37
     * This method is called before a test is executed.
38
     */
39
    protected function setUp()
0 ignored issues
show
Coding Style introduced by
setUp uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
40
    {
41
        $this->object = new Dispatcher;
42
        $GLOBALS['testEvent'] = false;
43
    }
44
45
    /**
46
     */
47
    public function testOn()
0 ignored issues
show
Coding Style introduced by
testOn uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
48
    {
49
        $this->object->on('test.event', array($this, 'eventFunction'));
50
        $this->object->notify(new Event('test.event'));
51
        $this->assertTrue($GLOBALS['testEvent']);
52
    }
53
54
    // test function for event callback
55
    public function eventFunction($event)
0 ignored issues
show
Coding Style introduced by
eventFunction uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
56
    {
57
        $GLOBALS['testEvent'] = true;
58
    }
59
60
    /**
61
     */
62
    public function testRemoveListener()
0 ignored issues
show
Coding Style introduced by
testRemoveListener uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
63
    {
64
        $this->object->on('test.event', array($this, 'eventFunction'));
65
        $this->object->notify(new Event('test.event'));
66
        $this->assertTrue($GLOBALS['testEvent']);
67
        
68
        $GLOBALS['testEvent'] = false;
69
        $this->object->removeListener('test.event', array($this, 'eventFunction'));
70
        $this->object->notify(new Event('test.event'));
71
        $this->assertFalse($GLOBALS['testEvent']);
72
        
73
        $this->assertInstanceOf('Fwk\Events\Dispatcher', $this->object->removeListener('inexistant.event', array()));
74
    }
75
76
    /**
77
     */
78
    public function testRemoveAllListeners()
0 ignored issues
show
Coding Style introduced by
testRemoveAllListeners uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
79
    {
80
        $this->object->on('test.event', array($this, 'eventFunction'));
81
        
82
        $this->object->removeAllListeners('test.event');
83
        $this->object->notify(new Event('test.event'));
84
        $this->assertFalse($GLOBALS['testEvent']);
85
    }
86
87
    /**
88
     */
89
    public function testNotify()
0 ignored issues
show
Coding Style introduced by
testNotify uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
90
    {
91
        $this->assertFalse($GLOBALS['testEvent']);
92
        $this->object->on('test.event', array($this, 'eventFunction'));
93
94
        $this->object->notify($event = new Event('test.event'));
95
        $this->assertTrue($GLOBALS['testEvent']);
96
        $this->assertTrue($event->isProcessed());
97
    }
98
99
    public function testStoppedEvent()
0 ignored issues
show
Coding Style introduced by
testStoppedEvent uses the super-global variable $GLOBALS which is generally not recommended.

Instead of super-globals, we recommend to explicitly inject the dependencies of your class. This makes your code less dependent on global state and it becomes generally more testable:

// Bad
class Router
{
    public function generate($path)
    {
        return $_SERVER['HOST'].$path;
    }
}

// Better
class Router
{
    private $host;

    public function __construct($host)
    {
        $this->host = $host;
    }

    public function generate($path)
    {
        return $this->host.$path;
    }
}

class Controller
{
    public function myAction(Request $request)
    {
        // Instead of
        $page = isset($_GET['page']) ? intval($_GET['page']) : 1;

        // Better (assuming you use the Symfony2 request)
        $page = $request->query->get('page', 1);
    }
}
Loading history...
100
    {
101
        $this->assertFalse($GLOBALS['testEvent']);
102
        $this->object->on('test.event', function(Event $event) {
103
            $event->stop();
104
        });
105
        $this->object->on('test.event', array($this, 'eventFunction'));
106
        $this->object->notify('test.event');
107
        $this->assertFalse($GLOBALS['testEvent']); // event was stopped
108
    }
109
    
110
    public function testListenerObject()
111
    {
112
        $this->object->addListener(new MyListenerObj());
113
        $this->object->notify($event = new Event('testEvent'));
114
        $this->assertTrue($event->isProcessed());
115
    }
116
    
117
    public function testInvalidListenerObject()
118
    {
119
        $this->setExpectedException('\InvalidArgumentException');
120
        $this->object->addListener(function($event) { return false; });
121
    }
122
}
123