summaryrefslogtreecommitdiff
path: root/test/ORMTest.php
blob: b85b0f65a841502d2a0cbb2f072a37855db3824f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
<?php

class ORMTest extends PHPUnit_Framework_TestCase {

    public function setUp() {
        // Enable logging
        ORM::configure('logging', true);

        // Set up the dummy database connection
        $db = new MockPDO('sqlite::memory:');
        ORM::set_db($db);
    }

    public function tearDown() {
        ORM::reset_config();
        ORM::reset_db();
    }

    public function testStaticAtrributes() {
        $this->assertEquals('0', ORM::CONDITION_FRAGMENT);
        $this->assertEquals('1', ORM::CONDITION_VALUES);
    }

    public function testForTable() {
        $result = ORM::for_table('test');
        $this->assertInstanceOf('ORM', $result);
    }

    public function testCreate() {
        $model = ORM::for_table('test')->create();
        $this->assertInstanceOf('ORM', $model);
        $this->assertTrue($model->is_new());
    }

    public function testIsNew() {
        $model = ORM::for_table('test')->create();
        $this->assertTrue($model->is_new());

        $model = ORM::for_table('test')->create(array('test' => 'test'));
        $this->assertTrue($model->is_new());
    }

    public function testIsDirty() {
        $model = ORM::for_table('test')->create();
        $this->assertFalse($model->is_dirty('test'));

        $model = ORM::for_table('test')->create(array('test' => 'test'));
        $this->assertTrue($model->is_dirty('test'));

        $model->test = null;
        $this->assertTrue($model->is_dirty('test'));

        $model->test = '';
        $this->assertTrue($model->is_dirty('test'));
    }

    public function testArrayAccess() {
        $value = 'test';
        $model = ORM::for_table('test')->create();
        $model['test'] = $value;
        $this->assertTrue(isset($model['test']));
        $this->assertEquals($model['test'], $value);
        unset($model['test']);
        $this->assertFalse(isset($model['test']));
    }

    public function testFindResultSet() {
        $result_set = ORM::for_table('test')->find_result_set();
        $this->assertInstanceOf('IdiormResultSet', $result_set);
        $this->assertSame(count($result_set), 5);
    }

    public function testFindResultSetByDefault() {
        ORM::configure('return_result_sets', true);

        $result_set = ORM::for_table('test')->find_many();
        $this->assertInstanceOf('IdiormResultSet', $result_set);
        $this->assertSame(count($result_set), 5);
        
        ORM::configure('return_result_sets', false);
        
        $result_set = ORM::for_table('test')->find_many();
        $this->assertInternalType('array', $result_set);
        $this->assertSame(count($result_set), 5);
    }

    public function testGetLastPdoStatement() {
        ORM::for_table('widget')->where('name', 'Fred')->find_one();
        $statement = ORM::get_last_statement();
        $this->assertInstanceOf('MockPDOStatement', $statement);
    }

    /**
     * @expectedException IdiormMethodMissingException
     */
    public function testInvalidORMFunctionCallShouldCreateException() {
        $orm = ORM::for_table('test');
        $orm->invalidFunctionCall();
    }

    /**
     * @expectedException IdiormMethodMissingException
     */
    public function testInvalidResultsSetFunctionCallShouldCreateException() {
        $resultSet = ORM::for_table('test')->find_result_set();
        $resultSet->invalidFunctionCall();
    }

    /**
     * These next two tests are needed because if you have select()ed some fields,
     * but not the primary key, then the primary key is not available for the
     * update/delete query - see issue #203.
     * We need to change the primary key here to something other than `id`
     * becuase MockPDOStatement->fetch() always returns an id.
     */
    public function testUpdateNullPrimaryKey() {
        try {
            $widget = ORM::for_table('widget')
                ->use_id_column('primary')
                ->select('foo')
                ->where('primary', 1)
                ->find_one()
            ;

            $widget->foo = 'bar';
            $widget->save();

            throw new Exception('Test did not throw expected exception');
        } catch (Exception $e) {
            $this->assertEquals($e->getMessage(), 'Primary key ID missing from row or is null');
        }
    }

    public function testDeleteNullPrimaryKey() {
        try {
            $widget = ORM::for_table('widget')
                ->use_id_column('primary')
                ->select('foo')
                ->where('primary', 1)
                ->find_one()
            ;

            $widget->delete();

            throw new Exception('Test did not throw expected exception');
        } catch (Exception $e) {
            $this->assertEquals($e->getMessage(), 'Primary key ID missing from row or is null');
        }
    }

    public function testNullPrimaryKey() {
        try {
            $widget = ORM::for_table('widget')
                ->use_id_column('primary')
                ->select('foo')
                ->where('primary', 1)
                ->find_one()
            ;

            $widget->id(true);

            throw new Exception('Test did not throw expected exception');
        } catch (Exception $e) {
            $this->assertEquals($e->getMessage(), 'Primary key ID missing from row or is null');
        }
    }

    public function testNullPrimaryKeyPart() {
        try {
            $widget = ORM::for_table('widget')
                ->use_id_column(array('id', 'primary'))
                ->select('foo')
                ->where('id', 1)
                ->where('primary', 1)
                ->find_one()
            ;

            $widget->id(true);

            throw new Exception('Test did not throw expected exception');
        } catch (Exception $e) {
            $this->assertEquals($e->getMessage(), 'Primary key ID contains null value(s)');
        }
    }
}