User2::listenerMethod()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
c 1
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
namespace Fwk\Db;
3
4
use Fwk\Db\Events\FreshEvent;
5
use Fwk\Db\Listeners\Timestampable;
6
use Fwk\Db\Listeners\Typable;
7
use Fwk\Db\EventSubscriberInterface;
8
9
class TestListener
10
{
11
    public function onFresh(FreshEvent $event)
12
    {
13
        $event->getEntity()->property = "listener!";
14
    }
15
}
16
17
class User2 extends \stdClass implements EventSubscriberInterface
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...
18
{
19
    public $emails;
20
21
    public $metas;
22
23
    public $phone;
24
25
    public $property = null;
26
    public $callableProperty = null;
27
28
    public function __construct()
29
    {
30
        $this->emails = new Relations\Many2Many(
31
            'id',
32
            'user_id',
33
            'fwkdb_test_emails',
34
            'fwkdb_test_users_emails',
35
            'id',
36
            'email_id',
37
            'Fwk\Db\Email2'
38
        );
39
40
        $this->metas = new Relations\One2Many(
41
            'id',
42
            'user_id',
43
            'fwkdb_test_users_metas'
44
        );
45
        $this->metas->setReference('name');
46
47
        $this->phone = new Relations\One2One('phone_id', 'id', 'fwkdb_test_phones');
48
    }
49
50
    /**
51
     * @return array
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use array<*,TestListener|array<User2|string>>.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
52
     */
53
    public function getListeners()
54
    {
55
        return array(
56
            new TestListener(),
57
            'fresh' => array($this, 'listenerMethod')
58
        );
59
    }
60
61
    public function listenerMethod(FreshEvent $event)
62
    {
63
        $this->callableProperty = 'callableCalled!';
64
    }
65
}
66
67
class Email2 extends \stdClass
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...
68
{
69
}
70
71
/**
72
 * Test class for Accessor.
73
 * Generated by PHPUnit on 2012-05-27 at 17:46:42.
74
 */
75
class EventSubscriberTest 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...
76
{
77
    /**
78
     *
79
     * @var \Fwk\Db\Connection
80
     */
81
    protected $connection;
82
83
    /**
84
     * Sets up the fixture, for example, opens a network connection.
85
     * This method is called before a test is executed.
86
     */
87
    protected function setUp()
88
    {
89
        $this->connection = new Connection(array(
90
            'memory'    => true,
91
            'driver'    => 'pdo_sqlite'
92
        ));
93
94
        \FwkDbTestUtil::createTestDb($this->connection);
95
96
        $obj = new \stdClass;
97
        $obj->username = "joeBar";
98
99
        $obj2 = new \stdClass;
100
        $obj2->username = "joeBar4";
101
        $obj2->created_at = date('Y-m-d H:i:s');
102
        $this->connection->table("fwkdb_test_users")->save(array($obj, $obj2));
103
    }
104
105
    protected function tearDown()
106
    {
107
        \FwkDbTestUtil::dropTestDb($this->connection);
108
    }
109
110
    public function testListenerIsTriggered()
111
    {
112
        $obj = $this->connection->table("fwkdb_test_users")->finder()->setEntity('Fwk\Db\User2')->all();
113
        $this->assertEquals(2, count($obj));
114
        $this->assertEquals("listener!", $obj[0]->property);
115
    }
116
117
    public function testCallableListenerIsTriggered()
118
    {
119
        $obj = $this->connection->table("fwkdb_test_users")->finder()->setEntity('Fwk\Db\User2')->all();
120
        $this->assertEquals(2, count($obj));
121
        $this->assertEquals("callableCalled!", $obj[0]->callableProperty);
122
    }
123
124
    public function testCustomTableListeners()
125
    {
126
        $table = $this->connection->table("fwkdb_test_users");
127
        $table->setDefaultEntityListeners(array(new Timestampable()));
128
129
        $obj = new \stdClass;
130
        $obj->username = "joeBar2";
131
132
        $this->assertFalse(isset($obj->created_at));
133
        $this->connection->table("fwkdb_test_users")->save($obj);
134
135
        $this->assertTrue(isset($obj->created_at));
136
        $this->assertNotNull($obj->created_at);
137
    }
138
139
    public function testFinderListeners()
140
    {
141
        $table = $this->connection->table("fwkdb_test_users");
142
143
        $obj = $table->finder(null, array(new Typable()))->one(2);
144
145
        $this->assertTrue($obj instanceof \stdClass);
146
        $this->assertTrue(isset($obj->created_at));
147
        $this->assertNotNull($obj->created_at);
148
        $this->assertTrue(isset($obj->created_at));
149
    }
150
}
151