Completed
Push — master ( ef5eaa...6d70d2 )
by Lars
03:21 queued 30s
created

ORMTest::testNullPrimaryKey()   A

Complexity

Conditions 2
Paths 3

Size

Total Lines 16
Code Lines 11

Duplication

Lines 16
Ratio 100 %
Metric Value
dl 16
loc 16
rs 9.4286
cc 2
eloc 11
nc 3
nop 0
1
<?php
2
3
use idiorm\orm\IdiormMethodMissingException;
4
use idiorm\orm\ORM;
5
6
class ORMTest extends PHPUnit_Framework_TestCase {
7
8
    public function setUp() {
9
        // Enable logging
10
        ORM::configure('logging', true);
11
12
        // Set up the dummy database connection
13
        $db = new MockPDO('sqlite::memory:');
14
        ORM::set_db($db);
15
    }
16
17
    public function tearDown() {
18
        ORM::reset_config();
19
        ORM::reset_db();
20
    }
21
22
    public function testStaticAtrributes() {
23
        $this->assertEquals('0', ORM::CONDITION_FRAGMENT);
24
        $this->assertEquals('1', ORM::CONDITION_VALUES);
25
    }
26
27
    public function testForTable() {
28
        $result = ORM::for_table('test');
29
        $this->assertInstanceOf('idiorm\orm\ORM', $result);
30
    }
31
32
    public function testCreate() {
33
        $model = ORM::for_table('test')->create();
34
        $this->assertInstanceOf('idiorm\orm\ORM', $model);
35
        $this->assertTrue($model->is_new());
36
    }
37
38
    public function testIsNew() {
39
        $model = ORM::for_table('test')->create();
40
        $this->assertTrue($model->is_new());
41
42
        $model = ORM::for_table('test')->create(array('test' => 'test'));
43
        $this->assertTrue($model->is_new());
44
    }
45
46
    public function testIsDirty() {
47
        $model = ORM::for_table('test')->create();
48
        $this->assertFalse($model->is_dirty('test'));
49
        
50
        $model = ORM::for_table('test')->create(array('test' => 'test'));
51
        $this->assertTrue($model->is_dirty('test'));
52
    }
53
54
    public function testArrayAccess() {
55
        $value = 'test';
56
        $model = ORM::for_table('test')->create();
57
        $model['test'] = $value;
58
        $this->assertTrue(isset($model['test']));
59
        $this->assertEquals($model['test'], $value);
60
        unset($model['test']);
61
        $this->assertFalse(isset($model['test']));
62
    }
63
64
    public function testFindResultSet() {
65
        $result_set = ORM::for_table('test')->find_result_set();
66
        $this->assertInstanceOf('idiorm\orm\IdiormResultSet', $result_set);
67
        $this->assertSame(count($result_set), 5);
68
    }
69
70
    public function testFindResultSetByDefault() {
71
        ORM::configure('return_result_sets', true);
72
73
        $result_set = ORM::for_table('test')->find_many();
74
        $this->assertInstanceOf('idiorm\orm\IdiormResultSet', $result_set);
75
        $this->assertSame(count($result_set), 5);
76
        
77
        ORM::configure('return_result_sets', false);
78
        
79
        $result_set = ORM::for_table('test')->find_many();
80
        $this->assertInternalType('array', $result_set);
81
        $this->assertSame(count($result_set), 5);
82
    }
83
84
    public function testGetLastPdoStatement() {
85
        ORM::for_table('widget')->where('name', 'Fred')->find_one();
86
        $statement = ORM::get_last_statement();
87
        $this->assertInstanceOf('MockPDOStatement', $statement);
88
    }
89
90
    /**
91
     * @expectedException idiorm\orm\IdiormMethodMissingException
92
     */
93
    public function testInvalidORMFunctionCallShouldCreateException() {
94
        $orm = ORM::for_table('test');
95
        $orm->invalidFunctionCall();
96
    }
97
98
    /**
99
     * @expectedException idiorm\orm\IdiormMethodMissingException
100
     */
101
    public function testInvalidResultsSetFunctionCallShouldCreateException() {
102
        $resultSet = ORM::for_table('test')->find_result_set();
103
        $resultSet->invalidFunctionCall();
104
    }
105
106
    /**
107
     * These next two tests are needed because if you have select()ed some fields,
108
     * but not the primary key, then the primary key is not available for the
109
     * update/delete query - see issue #203.
110
     * We need to change the primary key here to something other than `id`
111
     * becuase MockPDOStatement->fetch() always returns an id.
112
     */
113
    public function testUpdateNullPrimaryKey() {
114
        try {
115
            $widget = ORM::for_table('widget')
116
                ->use_id_column('primary')
117
                ->select('foo')
118
                ->where('primary', 1)
119
                ->find_one()
120
            ;
121
122
            $widget->foo = 'bar';
123
            $widget->save();
124
125
            throw new Exception('Test did not throw expected exception');
126
        } catch (Exception $e) {
127
            $this->assertEquals($e->getMessage(), 'Primary key ID missing from row or is null');
128
        }
129
    }
130
131
    public function testDeleteNullPrimaryKey() {
132
        try {
133
            $widget = ORM::for_table('widget')
134
                ->use_id_column('primary')
135
                ->select('foo')
136
                ->where('primary', 1)
137
                ->find_one()
138
            ;
139
140
            $widget->delete();
141
142
            throw new Exception('Test did not throw expected exception');
143
        } catch (Exception $e) {
144
            $this->assertEquals($e->getMessage(), 'Primary key ID missing from row or is null');
145
        }
146
    }
147
148
    public function testNullPrimaryKey() {
149
        try {
150
            $widget = ORM::for_table('widget')
151
                ->use_id_column('primary')
152
                ->select('foo')
153
                ->where('primary', 1)
154
                ->find_one()
155
            ;
156
157
            $widget->id(true);
158
159
            throw new Exception('Test did not throw expected exception');
160
        } catch (Exception $e) {
161
            $this->assertEquals($e->getMessage(), 'Primary key ID missing from row or is null');
162
        }
163
    }
164
165
    public function testNullPrimaryKeyPart() {
166
        try {
167
            $widget = ORM::for_table('widget')
168
                ->use_id_column(array('id', 'primary'))
169
                ->select('foo')
170
                ->where('id', 1)
171
                ->where('primary', 1)
172
                ->find_one()
173
            ;
174
175
            $widget->id(true);
176
177
            throw new Exception('Test did not throw expected exception');
178
        } catch (Exception $e) {
179
            $this->assertEquals($e->getMessage(), 'Primary key ID contains null value(s)');
180
        }
181
    }
182
}