TableTest::testGetColumnFail()   A
last analyzed

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
2
3
namespace Fwk\Db;
4
5
/**
6
 * Test class for Table.
7
 * Generated by PHPUnit on 2012-05-27 at 15:10:12.
8
 */
9
class TableTest extends \PHPUnit_Framework_TestCase
10
{
11
    /**
12
     * @var Table
13
     */
14
    protected $object;
15
16
    /**
17
     *
18
     * @var Connection
19
     */
20
    protected $connection;
21
22
    public function __construct()
23
    {
24
        $this->connection = new Connection(array(
25
                    'memory' => true,
26
                    'driver' => 'pdo_sqlite'
27
                ));
28
29
        $schema = $this->connection->getSchema();
30
31
        $myTable = $schema->createTable("test_table");
32
        $myTable->addColumn("id", "integer", array("unsigned" => true));
33
        $myTable->addColumn("username", "string", array("length" => 32));
34
        $myTable->setPrimaryKey(array("id"));
35
        $myTable->addUniqueIndex(array("username"));
36
37
        $myTable = $schema->createTable("test_table2");
38
        $myTable->addColumn("id", "integer", array("unsigned" => true));
39
        $myTable->addColumn("username", "string", array("length" => 32));
40
41
        $myTable = $schema->createTable("test_table3");
42
        $myTable->addColumn("id", "integer", array("unsigned" => true));
43
        $myTable->addIndex(array('id'));
44
    }
45
46
    /**
47
     * Sets up the fixture, for example, opens a network connection.
48
     * This method is called before a test is executed.
49
     */
50
    protected function setUp()
51
    {
52
        $this->object = new Table('test_table');
53
54
        \FwkDbTestUtil::createTestDb($this->connection);
55
    }
56
57
    /**
58
     * Tears down the fixture, for example, closes a network connection.
59
     * This method is called after a test is executed.
60
     */
61
    protected function tearDown()
62
    {
63
        unset($this->object);
64
65
        \FwkDbTestUtil::dropTestDb($this->connection);
66
    }
67
68
    /**
69
     */
70
    public function testGetColumnsFail()
71
    {
72
        $this->setExpectedException('Fwk\Db\Exception');
73
        $this->assertEquals(2, count($this->object->getColumns()));
74
    }
75
76
    /**
77
     */
78
    public function testGetColumns()
79
    {
80
        $this->object->setConnection($this->connection);
81
        $this->assertEquals(2, count($this->object->getColumns()));
82
    }
83
84
    /**
85
     */
86
    public function testSetConnection()
87
    {
88
        $this->assertEquals($this->object, $this->object->setConnection($this->connection));
89
    }
90
91
    /**
92
     */
93
    public function testGetConnectionFail()
94
    {
95
        $this->setExpectedException("\Fwk\Db\Exception");
96
        $this->object->getConnection();
97
    }
98
99
    /**
100
     */
101
    public function testGetConnection()
102
    {
103
        $this->object->setConnection($this->connection);
104
        $this->assertEquals($this->connection, $this->object->getConnection());
105
    }
106
107
    public function testGetFinder()
108
    {
109
        $this->assertInstanceOf('\Fwk\Db\Finder', $this->object->finder());
110
    }
111
112
    /**
113
     */
114
    public function testGetName()
115
    {
116
        $this->assertEquals('test_table', $this->object->getName());
117
    }
118
119
    public function testGetIdentifiersKeys()
120
    {
121
        $this->object->setConnection($this->connection);
122
        $this->assertEquals(array('id'), $this->object->getIdentifiersKeys());
123
    }
124
125
    /**
126
     */
127
    public function testGetRegistry()
128
    {
129
        $this->assertInstanceOf('\Fwk\Db\Registry\Registry', $this->object->getRegistry());
130
    }
131
132
    /**
133
     */
134
    public function testGetColumnFail()
135
    {
136
        $this->object->setConnection($this->connection);
137
        $this->setExpectedException('\Fwk\Db\Exceptions\TableColumnNotFoundException');
138
        $this->object->getColumn('test');
139
    }
140
141
    public function testGetColumn()
142
    {
143
        $this->object->setConnection($this->connection);
144
        $this->assertInstanceOf('\Doctrine\DBAL\Schema\Column', $this->object->getColumn('username'));
145
    }
146
147
    /**
148
     */
149
    public function testHasColumn()
150
    {
151
        $this->object->setConnection($this->connection);
152
        $this->assertFalse($this->object->hasColumn('test'));
153
        $this->assertTrue($this->object->hasColumn('username'));
154
    }
155
156
    /**
157
     */
158
    public function testDefaultEntity()
159
    {
160
        $this->object->setConnection($this->connection);
161
        $this->assertEquals('\stdClass', $this->object->getDefaultEntity());
162
        $this->object->setDefaultEntity('\MyTestEntity');
163
        $this->assertEquals('\MyTestEntity', $this->object->getDefaultEntity());
164
    }
165
166
    /**
167
     */
168
    public function testDeleteAll()
169
    {
170
        $user = new \stdClass;
171
        $user->username = "joebar";
172
173
        $u2 = clone $user;
174
        $u2->username = "barjoe";
175
176
        $this->connection->table('fwkdb_test_users')->save(array($user, $u2));
177
        $this->assertEquals(2, count($this->connection->table('fwkdb_test_users')->finder()->all()));
178
179
        $this->connection->table('fwkdb_test_users')->deleteAll();
180
        $this->assertEquals(0, count($this->connection->table('fwkdb_test_users')->finder()->all()));
181
    }
182
183
    public function testTableLacksIdentifiers()
184
    {
185
        $this->setExpectedException('\Fwk\Db\Exceptions\TableLacksIdentifiersException');
186
        $this->connection->table('test_table2')->getIdentifiersKeys();
187
    }
188
189
    public function testTableLacksIdentifiers2()
190
    {
191
        $this->setExpectedException('\Fwk\Db\Exceptions\TableLacksIdentifiersException');
192
        $this->connection->table('test_table3')->getIdentifiersKeys();
193
    }
194
    
195
    public function testSaveFail()
196
    {
197
        $this->setExpectedException('\InvalidArgumentException');
198
        $this->object->save(array(null));
199
    }
200
    
201
    public function testDeleteFail()
202
    {
203
        $this->setExpectedException('\InvalidArgumentException');
204
        $this->object->delete(array(null));
205
    }
206
}
207