zurmo-invoice-example /app/protected/tests/unit/RedBeanTest.php

Language PHP Lines 349
MD5 Hash 603b728268c2423eac29c592366d256b Estimated Cost $6,992 (why?)
Repository https://bitbucket.org/sanbrar/zurmo-invoice-example View Raw File View Project SPDX
  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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
<?php
    /*********************************************************************************
     * Zurmo is a customer relationship management program developed by
     * Zurmo, Inc. Copyright (C) 2012 Zurmo Inc.
     *
     * Zurmo is free software; you can redistribute it and/or modify it under
     * the terms of the GNU General Public License version 3 as published by the
     * Free Software Foundation with the addition of the following permission added
     * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
     * IN WHICH THE COPYRIGHT IS OWNED BY ZURMO, ZURMO DISCLAIMS THE WARRANTY
     * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
     *
     * Zurmo is distributed in the hope that it will be useful, but WITHOUT
     * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
     * FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
     * details.
     *
     * You should have received a copy of the GNU General Public License along with
     * this program; if not, see http://www.gnu.org/licenses or write to the Free
     * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
     * 02110-1301 USA.
     *
     * You can contact Zurmo, Inc. with a mailing address at 113 McHenry Road Suite 207,
     * Buffalo Grove, IL 60089, USA. or at email address contact@zurmo.com.
     ********************************************************************************/

    // HintManager copied from...
    // http://groups.google.com/group/redbeanorm/browse_thread/thread/7eb59797e8478a89/61eae7941cae1970
    // Used in testDateTimeHinting, and maybe other things, below. null test added.
    class HintManager implements RedBean_Observer {                                                 // Not Coding Standard
            public function __construct( $toolbox ) {                                               // Not Coding Standard
                    $this->dateOpt = new RedBean_Plugin_Optimizer_Datetime( $toolbox );             // Not Coding Standard
            }                                                                                       // Not Coding Standard
                                                                                                    // Not Coding Standard
            public function onEvent( $type, $info ) {                                               // Not Coding Standard
                    $hints = $info->getMeta("hint");                                                // Not Coding Standard
                    if ($hints !== null) {                                                          // Not Coding Standard
                            foreach ($hints as $k=>$v) {                                            // Not Coding Standard
                                    if ($v=="date"){ //or select an optimizer based on value in $v  // Not Coding Standard
                                            $this->dateOpt->setTable($info->getMeta("type"));       // Not Coding Standard
                                            $this->dateOpt->setColumn($k);                          // Not Coding Standard
                                            $this->dateOpt->setValue($info->$k);                    // Not Coding Standard
                                            $this->dateOpt->optimize();                             // Not Coding Standard
                                    }                                                               // Not Coding Standard
                            }                                                                       // Not Coding Standard
                    }
            }
    }

    // This is for testing details of how RedBean works.
    class RedBeanTest extends BaseTest
    {
        public function testZeros()
        {
            $thing = R::dispense('thing');
            $thing->zero = 0;
            R::store($thing);
            $id = $thing->id;
            unset($thing);
            $thing = R::load('thing', $id);
            $this->assertEquals(0, $thing->zero);

            //Try saving a second thing.
            $thing = R::dispense('thing');
            $thing->zero = 2;
            R::store($thing);
            $id = $thing->id;
            unset($thing);
            $thing = R::load('thing', $id);
            $this->assertEquals(2, $thing->zero);
        }

        public function testNulls()
        {
            $thing = R::dispense('thing');
            $thing->zero = null;
            R::store($thing);
            $id = $thing->id;
            unset($thing);
            $thing = R::load('thing', $id);
            $this->assertEquals(null, $thing->zero);
        }

        public function testGetAllTableReturnsNullOn5_2()
        {
            $sql = 'select id from atableneverhere';
            $rows = R::getAll($sql);
            if (version_compare(PHP_VERSION, '5.3.0', '>'))
            {
                $this->assertTrue(is_array($rows));
            }
            else
            {
                $this->assertTrue($rows === null);
            }
        }

        public function testStringContainingOnlyNumbers()
        {
            $thing = R::dispense('thing');
            $thing->phoneNumberNumber  = 5551234;
            $thing->phoneNumberString1 = '555-1234';
            $thing->phoneNumberString2 = '5551234';
            R::store($thing);
            $databaseType = R::$toolbox->getDatabaseAdapter()->getDatabase()->getDatabaseType();
            switch ($databaseType)
            {
                case 'mysql':
                    $sql = 'desc thing;';
                    $rows = R::getAll($sql);
                    $this->assertEquals('phoneNumberNumber',   $rows[2]['Field']);
                    $this->assertEquals('int(11) unsigned',    $rows[2]['Type']);
                    $this->assertEquals('phoneNumberString1',  $rows[3]['Field']);
                    $this->assertEquals('varchar(255)',        $rows[3]['Type']);
                    $this->assertEquals('phoneNumberString2',  $rows[4]['Field']);
                    $this->assertEquals('int(11) unsigned',    $rows[4]['Type']);
                    break;

                case 'sqlite':
                    $sql  = 'pragma table_info(\'thing\');';
                    $rows = R::getAll($sql);
                    $this->assertEquals('phoneNumberNumber',   $rows[2]['name']);
                    $this->assertEquals('INTEGER',             $rows[2]['type']);
                    $this->assertEquals('phoneNumberString1',  $rows[3]['name']);
                    $this->assertEquals('TEXT',                $rows[3]['type']);
                    $this->assertEquals('phoneNumberString2',  $rows[4]['name']);
                    $this->assertEquals('INTEGER',             $rows[4]['type']);
                    break;

                case 'pgsql':
                    $sql = 'select column_name, data_type from information_schema.columns where table_name = \'thing\' and column_name like \'phone%\' order by column_name;';
                    $rows = R::getAll($sql);
                    $this->assertEquals('phonenumbernumber',   $rows[0]['column_name']);
                    $this->assertEquals('integer',             $rows[0]['data_type']);
                    $this->assertEquals('phonenumberstring1',  $rows[1]['column_name']);
                    $this->assertEquals('text',                $rows[1]['data_type']);
                    $this->assertEquals('phonenumberstring2',  $rows[2]['column_name']);
                    $this->assertEquals('text',                $rows[2]['data_type']);
                    break;

                default:
                    $this->fail('Test does not support database type: ' . $databaseType);
            }
        }

        public function testRedBeanTypesShowingPDODodginess()
        {
            $wukka = R::dispense('wukka');
            $wukka->integer = 69;
            $wukka->string  = 'xxx';
            R::store($wukka);
            $this->assertEquals('integer', gettype($wukka->integer));
            $this->assertEquals('string',  gettype($wukka->string));
            $this->assertTrue  ($wukka->integer !== $wukka->string);
            $id = $wukka->id;
            unset($wukka);

            $databaseType = R::$toolbox->getDatabaseAdapter()->getDatabase()->getDatabaseType();
            switch ($databaseType)
            {
                case 'mysql':
                    $sql = 'desc wukka;';
                    $rows = R::getAll($sql);
                    $this->assertEquals('integer',             $rows[1]['Field']);
                    $this->assertEquals('tinyint(3) unsigned', $rows[1]['Type']);
                    $this->assertEquals('string',              $rows[2]['Field']);
                    $this->assertEquals('varchar(255)',        $rows[2]['Type']);
                    break;
            }

            $wukka = R::load('wukka', $id);
            $this->assertEquals('string', gettype($wukka->integer)); // Dodgy.
            $this->assertEquals('string', gettype($wukka->string));
            $this->assertTrue  ($wukka->integer !== $wukka->string);
        }

        public function testGetBeanWhenThereIsNoneToGet()
        {
            $bean = R::dispense('a');
            $bean2 = R::getBean($bean, 'b');
            if (!method_exists('R', 'getVersion') ||
                substr(R::getVersion(), 0, 3) == '1.2')
            {
                $this->assertTrue($bean2 !== null);
            }
            else
            {
                $this->assertEquals('1.3', substr(R::getVersion(), 0, 3));
                $this->assertTrue($bean2 === null);
            }
        }

        public function testUniqueMeta()
        {
            $bean = R::dispense('hombre');

            if (!method_exists('R', 'getVersion') ||
                substr(R::getVersion(), 0, 3) == '1.2')
            {
                $bean->setMeta("buildcommand.unique.0", array( "nombre") );
            }
            else
            {
                $this->assertEquals('1.3', substr(R::getVersion(), 0, 3));
                $bean->setMeta("buildcommand.unique", array(array("nombre")));
            }

            $bean->nombre = 'Pablo';
            R::store($bean);

            $bean2 = R::dispense('hombre');
            $bean2->nombre = 'Pablo';

            try
            {
                R::store($bean2);
                $this->fail('Expected a RedBean_Exception_SQL: Integrity constraint violation');
            }
            catch (RedBean_Exception_SQL $e)
            {
                $message = "SQLSTATE[23000]: Integrity constraint violation: 1062 Duplicate entry 'Pablo'";
                $this->assertEquals($message, substr($e->getMessage(), 0, strlen($message)));
            }
        }

        public function testExampleStoredProcedure()
        {
            $wukka = R::dispense('wukka');
            $wukka->integer = 666;
            $wukka->string  = 'yyy';
            R::store($wukka);
            try
            {
                R::exec("drop procedure get_wukka_integer");
            }
            catch (Exception $e)
            {
            }
            R::exec("
                create procedure get_wukka_integer(in the_string varchar(255), out the_integer int(11))
                begin
                    select wukka.integer
                    into the_integer
                    from wukka
                    where wukka.string = the_string;
                end
            ");
            R::exec("call get_wukka_integer('yyy', @the_integer)");
            $this->assertEquals(666, R::getCell("select @the_integer"));
        }

        /**
         * @depends testExampleStoredProcedure
         */
        public function testExampleStoredFunction()
        {
            try
            {
                R::exec("drop function get_wukka_integer2");
            }
            catch (Exception $e)
            {
            }
            R::exec("
                create function get_wukka_integer2(the_string varchar(255))
                returns int(11)
                begin
                    declare the_integer int(11);
                    select wukka.integer
                    into the_integer
                    from wukka
                    where wukka.string = the_string;
                    return the_integer;
                end
            ");
            $this->assertEquals(666, R::getCell("select get_wukka_integer2('yyy')"));
        }

        public function testCascadedDeleteDoesNotWorkForLinkedBeans()
        {
            $person = R::dispense('person');
            $person->name = 'bill';
            R::store($person);

            $phone  = R::dispense('phone');
            $phone->number = '555-1234';
            R::store($phone);

            R::$linkManager->link($phone, $person);
            R::store($phone);

            // Either way this doesn't work.
//            RedBean_Plugin_Constraint::addConstraint($person, $phone);
            RedBean_Plugin_Constraint::addConstraint($phone, $person);

            $id = $phone->id;
            unset($phone);

            R::trash($person);
            unset($person);

            $phone = R::load('phone', $id);
            $this->assertNotNull($phone); // The phone is not deleted.
        }

        public function testDateTimeFields()
        {
            $toolbox = RedBean_Setup::kickstartDev(Yii::app()->db->connectionString,
                                                   Yii::app()->db->username,
                                                   Yii::app()->db->password);
            $optimizer = new RedBean_Plugin_Optimizer($toolbox);
            $optimizer->addOptimizer(new RedBean_Plugin_Optimizer_DateTime($toolbox));
            $redbean = $toolbox->getRedBean();
            $redbean->addEventListener('update', $optimizer);

            for ($i = 1; $i < 10; $i++)
            {
                $person = R::dispense("person");
                $person->name = "bill$i";
                $person->date1 = time();
                $person->date2 = date('Y-m-d H:i:s');
                $redbean->store($person);
            }
            // TODO: to be continued...
        }

        public function testDateTimeHinting()
        {
            $toolbox = RedBean_Setup::kickstartDev(Yii::app()->db->connectionString,
                                                   Yii::app()->db->username,
                                                   Yii::app()->db->password);

            // Copied directly from...
            // http://groups.google.com/group/redbeanorm/browse_thread/thread/7eb59797e8478a89/61eae7941cae1970
            $hint = new HintManager( R::$toolbox );                 // Not Coding Standard
            R::$redbean->addEventListener( "after_update", $hint ); // Not Coding Standard
            R::exec("drop table if exists bean");                   // Not Coding Standard
            $bean = R::dispense("bean");                            // Not Coding Standard
            $bean->setMeta("hint",array("prop"=>"date"));           // Not Coding Standard
            $bean->prop = "2010-01-01 10:00:00";                    // Not Coding Standard
            R::store($bean);                                        // Not Coding Standard

            $rows = R::getAll('desc bean');
            $this->assertEquals('prop',     $rows[1]['Field']);
            $this->assertEquals('datetime', $rows[1]['Type']);
        }
    }
?>
Back to Top