Completed
Push — feat-events-tags ( 999033...033a2d )
by Julien
04:43
created

GeneralEventsTest   A

Complexity

Total Complexity 4

Size/Duplication

Total Lines 63
Duplicated Lines 0 %

Coupling/Cohesion

Dependencies 3

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 4
c 1
b 0
f 0
cbo 3
dl 0
loc 63
rs 10

4 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 10 1
A testBeforeServiceLoadedEventStop() 0 14 1
A testAfterServiceLoadedEventIsSent() 0 11 1
A testDataReferenceInEvents() 0 17 1
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->getDefinitionData();
58
            $data['testTwo'] = "test";
59
            $event->setDefinitionData($data);
60
        });
61
62
        $self = $this;
63
        $this->object->on('afterServiceLoaded', function(AfterServiceLoadedEvent $event) use ($self) {
64
            $data = $event->getDefinitionData();
65
            $self->assertTrue(isset($data['testTwo']));
66
        });
67
68
        $this->object->set('service.with.data', "da service", false, array('testOne' => true));
69
        $this->object->get('service.with.data');
70
    }
71
}