testBeforeServiceLoadedEventStop()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 14
Code Lines 9

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 14
rs 9.4285
cc 1
eloc 9
nc 1
nop 0
1
<?php
2
3
namespace Fwk\Di;
4
use Fwk\Di\Events\AfterServiceLoadedEvent;
5
use Fwk\Di\Events\BeforeServiceLoadedEvent;
6
7
/**
8
 */
9
class GeneralEventsTest extends \PHPUnit_Framework_TestCase {
10
11
    /**
12
     * @var Container
13
     */
14
    protected $object;
15
16
    public function setUp()
17
    {
18
        $this->object = new Container();
19
        $this->object->set('test.service', function () {
20
            $obj = new \stdClass();
21
            $obj->test = "test";
22
23
            return $obj;
24
        });
25
    }
26
    
27
    public function testBeforeServiceLoadedEventStop()
28
    {
29
        $this->object->on('beforeServiceLoaded', function(BeforeServiceLoadedEvent $event) {
30
            $obj = new \stdClass();
31
            $obj->notTest = "test";
32
33
            $event->setReturnValue($obj);
34
            $event->stop();
35
        });
36
37
        $service = $this->object->get('test.service');
38
        $this->assertFalse(isset($service->test));
39
        $this->assertTrue(isset($service->notTest));
40
    }
41
42
    public function testAfterServiceLoadedEventIsSent()
43
    {
44
        $ref = new \stdClass();
45
        $ref->testing = false;
46
        $this->object->on('afterServiceLoaded', function(AfterServiceLoadedEvent $event) use ($ref) {
47
             $ref->testing = true;
48
        });
49
        $this->assertFalse($ref->testing);
50
        $this->object->get('test.service');
51
        $this->assertTrue($ref->testing);
52
    }
53
54
    public function testDataReferenceInEvents()
55
    {
56
        $this->object->on('beforeServiceLoaded', function(BeforeServiceLoadedEvent $event) {
57
            $data = $event->getDefinition()->getData();
58
            $data['testTwo'] = "test";
59
            $event->getDefinition()->setData($data);
60
        });
61
62
        $self = $this;
63
        $this->object->on('afterServiceLoaded', function(AfterServiceLoadedEvent $event) use ($self) {
64
            $data = $event->getDefinition()->getData();
65
            $self->assertTrue(isset($data['testTwo']));
66
        });
67
68
        $this->object->set('service.with.data', "da service", false, array('testOne' => true));
0 ignored issues
show
Unused Code introduced by
The call to Container::set() has too many arguments starting with false.

This check compares calls to functions or methods with their respective definitions. If the call has more arguments than are defined, it raises an issue.

If a function is defined several times with a different number of parameters, the check may pick up the wrong definition and report false positives. One codebase where this has been known to happen is Wordpress.

In this case you can add the @ignore PhpDoc annotation to the duplicate definition and it will be ignored.

Loading history...
69
        $this->object->get('service.with.data');
70
    }
71
}