PageRenderTime 30ms CodeModel.GetById 2ms app.highlight 23ms RepoModel.GetById 1ms app.codeStats 0ms

/unit-tests/ModelsTest.php

https://github.com/phillipmadsen/cphalcon
PHP | 765 lines | 556 code | 138 blank | 71 comment | 4 complexity | c4b52040389523929bb430194e254b63 MD5 | raw file
  1<?php
  2
  3/*
  4  +------------------------------------------------------------------------+
  5  | Phalcon Framework                                                      |
  6  +------------------------------------------------------------------------+
  7  | Copyright (c) 2011-2012 Phalcon Team (http://www.phalconphp.com)       |
  8  +------------------------------------------------------------------------+
  9  | This source file is subject to the New BSD License that is bundled     |
 10  | with this package in the file docs/LICENSE.txt.                        |
 11  |                                                                        |
 12  | If you did not receive a copy of the license and are unable to         |
 13  | obtain it through the world-wide-web, please send an email             |
 14  | to license@phalconphp.com so we can send you a copy immediately.       |
 15  +------------------------------------------------------------------------+
 16  | Authors: Andres Gutierrez <andres@phalconphp.com>                      |
 17  |          Eduar Carvajal <eduar@phalconphp.com>                         |
 18  +------------------------------------------------------------------------+
 19*/
 20
 21use Phalcon\Mvc\Model\Message as ModelMessage;
 22
 23class Issue_1534 extends \Phalcon\Mvc\Model
 24{
 25}
 26
 27class ModelsTest extends PHPUnit_Framework_TestCase
 28{
 29
 30	public function __construct()
 31	{
 32		spl_autoload_register(array($this, 'modelsAutoloader'));
 33	}
 34
 35	public function __destruct()
 36	{
 37		spl_autoload_unregister(array($this, 'modelsAutoloader'));
 38	}
 39
 40	public function tearDown()
 41	{
 42		Phalcon\Mvc\Model::setup(array(
 43			'phqlLiterals' => true,
 44		));
 45	}
 46
 47	public function modelsAutoloader($className)
 48	{
 49		if (file_exists('unit-tests/models/'.$className.'.php')) {
 50			require 'unit-tests/models/'.$className.'.php';
 51		}
 52	}
 53
 54	protected function _prepareDb($db){
 55		$db->delete("personas", "estado='X'");
 56		$db->delete("personas", "cedula LIKE 'CELL%'");
 57	}
 58
 59	protected function _getDI($dbService)
 60	{
 61
 62		Phalcon\DI::reset();
 63
 64		$di = new Phalcon\DI();
 65
 66		$di->set('modelsManager', function(){
 67			return new Phalcon\Mvc\Model\Manager();
 68		});
 69
 70		$di->set('modelsMetadata', function(){
 71			return new Phalcon\Mvc\Model\Metadata\Memory();
 72		});
 73
 74		$di->set('db', $dbService, true);
 75
 76		return $di;
 77	}
 78
 79	public function testModelsMysql()
 80	{
 81		require 'unit-tests/config.db.php';
 82		if (empty($configMysql)) {
 83			$this->markTestSkipped("Skipped");
 84			return;
 85		}
 86
 87		$di = $this->_getDI(function(){
 88			require 'unit-tests/config.db.php';
 89			$db = new Phalcon\Db\Adapter\Pdo\Mysql($configMysql);
 90		/*
 91			$em = new \Phalcon\Events\Manager();
 92			$em->attach('db', function($event, $connection) {
 93				if ($event->getType() == 'beforeQuery') {
 94					echo $connection->getSQLStatement(), PHP_EOL;
 95					print_r($connection->getSQLVariables());
 96				}
 97			});
 98
 99			$db->setEventsManager($em);
100		*/
101			return $db;
102		});
103
104		$this->_executeTestsNormal($di);
105		$this->_executeTestsRenamed($di);
106
107		$this->issue1534($di);
108		$this->issue886($di);
109	}
110
111	public function testModelsPostgresql()
112	{
113		require 'unit-tests/config.db.php';
114		if (empty($configPostgresql)) {
115			$this->markTestSkipped("Skipped");
116			return;
117		}
118
119		$di = $this->_getDI(function(){
120			require 'unit-tests/config.db.php';
121			return new Phalcon\Db\Adapter\Pdo\Postgresql($configPostgresql);
122		});
123
124		$this->_executeTestsNormal($di);
125		$this->_executeTestsRenamed($di);
126
127		$this->issue886($di);
128	}
129
130	public function testModelsSqlite()
131	{
132		require 'unit-tests/config.db.php';
133		if (empty($configSqlite)) {
134			$this->markTestSkipped("Skipped");
135			return;
136		}
137
138		$di = $this->_getDI(function(){
139			require 'unit-tests/config.db.php';
140			return new Phalcon\Db\Adapter\Pdo\Sqlite($configSqlite);
141		});
142
143		$this->_executeTestsNormal($di);
144		$this->_executeTestsRenamed($di);
145
146		$this->issue886($di);
147	}
148
149	protected function issue1534($di)
150	{
151		$db = $di->getShared('db');
152		$this->_prepareDb($di->getShared('db'));
153
154		$this->assertTrue($db->delete('issue_1534'));
155
156		$product = new Issue_1534();
157		$product->language = new \Phalcon\Db\RawValue('default(language)');
158		$product->name     = 'foo';
159		$product->slug     = 'bar';
160		$product->brand    = new \Phalcon\Db\RawValue('default');
161		$product->sort     = new \Phalcon\Db\RawValue('default');
162		$this->assertTrue($product->save());
163		$this->assertEquals(1, Issue_1534::count());
164
165		$entry = Issue_1534::findFirst();
166		$this->assertEquals('bb', $entry->language);
167		$this->assertEquals('0', $entry->sort);
168		$this->assertTrue($entry->brand === NULL);
169
170		$this->assertTrue($entry->delete());
171
172		$product = new Issue_1534();
173		$product->language = 'en';
174		$product->name     = 'foo';
175		$product->slug     = 'bar';
176		$product->brand    = 'brand';
177		$product->sort     = 1;
178		$this->assertTrue($product->save());
179		$this->assertEquals(1, Issue_1534::count());
180
181		$entry = Issue_1534::findFirst();
182		$entry->brand    = new \Phalcon\Db\RawValue('default');
183		$entry->sort     = new \Phalcon\Db\RawValue('default');
184		$this->assertTrue($entry->save());
185		$this->assertEquals(1, Issue_1534::count());
186
187		$entry = Issue_1534::findFirst();
188		$this->assertEquals('0', $entry->sort);
189		$this->assertTrue($entry->brand === NULL);
190
191/* FIXME: this does not work yet
192		$entry->language = new \Phalcon\Db\RawValue('default(language)');
193		$entry->language = 'es';
194		$this->assertTrue($entry->save());
195		$this->assertEquals(1, Issue_1534::count());
196
197		$entry = Issue_1534::findFirst();
198		$this->assertEquals('es', $entry->language);
199		$this->assertEquals('0', $entry->sort);
200		$this->assertTrue($entry->brand === NULL);
201*/
202
203		$this->assertTrue($db->delete('issue_1534'));
204	}
205
206	protected function issue886($di)
207	{
208		$this->_prepareDb($di->getShared('db'));
209
210		Phalcon\Mvc\Model::setup(array(
211			'phqlLiterals' => false,
212		));
213
214		$people = People::findFirst();
215		$this->assertTrue(is_object($people));
216		$this->assertEquals(get_class($people), 'People');
217	}
218
219	protected function _executeTestsNormal($di){
220
221		$this->_prepareDb($di->getShared('db'));
222
223		//Count tests
224		$this->assertEquals(People::count(), Personas::count());
225
226		$params = array();
227		$this->assertEquals(People::count($params), Personas::count($params));
228
229		$params = array("estado='I'");
230		$this->assertEquals(People::count($params), Personas::count($params));
231
232		$params = "estado='I'";
233		$this->assertEquals(People::count($params), Personas::count($params));
234
235		$params = array("conditions" => "estado='I'");
236		$this->assertEquals(People::count($params), Personas::count($params));
237
238		//Find first
239		$people = People::findFirst();
240		$this->assertTrue(is_object($people));
241		$this->assertEquals(get_class($people), 'People');
242
243		$persona = Personas::findFirst();
244		$this->assertEquals($people->nombres, $persona->nombres);
245		$this->assertEquals($people->estado, $persona->estado);
246
247		$people = People::findFirst("estado='I'");
248		$this->assertTrue(is_object($people));
249
250		$persona = Personas::findFirst("estado='I'");
251		$this->assertTrue(is_object($persona));
252
253		$this->assertEquals($people->nombres, $persona->nombres);
254		$this->assertEquals($people->estado, $persona->estado);
255
256		$people = People::findFirst(array("estado='I'"));
257		$persona = Personas::findFirst(array("estado='I'"));
258		$this->assertEquals($people->nombres, $persona->nombres);
259		$this->assertEquals($people->estado, $persona->estado);
260
261		$params = array("conditions" => "estado='I'");
262		$people = People::findFirst($params);
263		$persona = Personas::findFirst($params);
264		$this->assertEquals($people->nombres, $persona->nombres);
265		$this->assertEquals($people->estado, $persona->estado);
266
267		$params = array("conditions" => "estado='A'", "order" => "nombres");
268		$people = People::findFirst($params);
269		$persona = Personas::findFirst($params);
270		$this->assertEquals($people->nombres, $persona->nombres);
271		$this->assertEquals($people->estado, $persona->estado);
272
273		$params = array("estado='A'", "order" => "nombres DESC", "limit" => 30);
274		$people = People::findFirst($params);
275		$persona = Personas::findFirst($params);
276		$this->assertEquals($people->nombres, $persona->nombres);
277		$this->assertEquals($people->estado, $persona->estado);
278
279		$params = array("estado=?1", "bind" => array(1 => 'A'), "order" => "nombres DESC", "limit" => 30);
280		$people = People::findFirst($params);
281		$persona = Personas::findFirst($params);
282		$this->assertEquals($people->nombres, $persona->nombres);
283		$this->assertEquals($people->estado, $persona->estado);
284
285		$params = array("estado=:estado:", "bind" => array("estado" => 'A'), "order" => "nombres DESC", "limit" => 30);
286		$people = People::findFirst($params);
287		$persona = Personas::findFirst($params);
288		$this->assertEquals($people->nombres, $persona->nombres);
289		$this->assertEquals($people->estado, $persona->estado);
290
291		$robot = Robots::findFirst(1);
292		$this->assertEquals(get_class($robot), 'Robots');
293
294		//Find tests
295		$personas = Personas::find();
296		$people = People::find();
297		$this->assertEquals(count($personas), count($people));
298
299		$personas = Personas::find("estado='I'");
300		$people = People::find("estado='I'");
301		$this->assertEquals(count($personas), count($people));
302
303		$personas = Personas::find(array("estado='I'"));
304		$people = People::find(array("estado='I'"));
305		$this->assertEquals(count($personas), count($people));
306
307		$personas = Personas::find(array("estado='A'", "order" => "nombres"));
308		$people = People::find(array("estado='A'", "order" => "nombres"));
309		$this->assertEquals(count($personas), count($people));
310
311		$personas = Personas::find(array("estado='A'", "order" => "nombres", "limit" => 100));
312		$people = People::find(array("estado='A'", "order" => "nombres", "limit" => 100));
313		$this->assertEquals(count($personas), count($people));
314
315		$params = array("estado=?1", "bind" => array(1 => "A"), "order" => "nombres", "limit" => 100);
316		$personas = Personas::find($params);
317		$people = People::find($params);
318		$this->assertEquals(count($personas), count($people));
319
320		$params = array("estado=:estado:", "bind" => array("estado" => "A"), "order" => "nombres", "limit" => 100);
321		$personas = Personas::find($params);
322		$people = People::find($params);
323		$this->assertEquals(count($personas), count($people));
324
325		$number = 0;
326		$peoples = Personas::find(array("conditions" => "estado='A'", "order" => "nombres", "limit" => 20));
327		foreach($peoples as $people){
328			$number++;
329		}
330		$this->assertEquals($number, 20);
331
332		$persona = new Personas($di);
333		$persona->cedula = 'CELL' . mt_rand(0, 999999);
334		$this->assertFalse($persona->save());
335
336		//Messages
337		$this->assertEquals(count($persona->getMessages()), 4);
338
339		$messages = array(
340			0 => ModelMessage::__set_state(array(
341				'_type' => 'PresenceOf',
342				'_message' => 'tipo_documento_id is required',
343				'_field' => 'tipo_documento_id',
344				'_code' => 0,
345			)),
346			1 => ModelMessage::__set_state(array(
347				'_type' => 'PresenceOf',
348				'_message' => 'nombres is required',
349				'_field' => 'nombres',
350				'_code' => 0,
351			)),
352			2 => ModelMessage::__set_state(array(
353				'_type' => 'PresenceOf',
354				'_message' => 'cupo is required',
355				'_field' => 'cupo',
356				'_code' => 0,
357			)),
358			3 => ModelMessage::__set_state(array(
359				'_type' => 'PresenceOf',
360				'_message' => 'estado is required',
361				'_field' => 'estado',
362				'_code' => 0,
363			)),
364		);
365		$this->assertEquals($persona->getMessages(), $messages);
366
367		//Save
368		$persona = new Personas($di);
369		$persona->cedula = 'CELL' . mt_rand(0, 999999);
370		$persona->tipo_documento_id = 1;
371		$persona->nombres = 'LOST';
372		$persona->telefono = '1';
373		$persona->cupo = 20000;
374		$persona->estado = 'A';
375		$this->assertTrue($persona->save());
376
377		$persona = new Personas($di);
378		$persona->cedula = 'CELL' . mt_rand(0, 999999);
379		$persona->tipo_documento_id = 1;
380		$persona->nombres = 'LOST LOST';
381		$persona->telefono = '2';
382		$persona->cupo = 0;
383		$persona->estado = 'X';
384		$this->assertTrue($persona->save());
385
386		//Check correct save
387		$persona = Personas::findFirst(array("estado='X'"));
388		$this->assertNotEquals($persona, false);
389		$this->assertEquals($persona->nombres, 'LOST LOST');
390		$this->assertEquals($persona->estado, 'X');
391
392		//Update
393		$persona->cupo = 150000;
394		$persona->telefono = '123';
395		$this->assertTrue($persona->update());
396
397		//Checking correct update
398		$persona = Personas::findFirst(array("estado='X'"));
399		$this->assertNotEquals($persona, false);
400		$this->assertEquals($persona->cupo, 150000);
401		$this->assertEquals($persona->telefono, '123');
402
403		//Update
404		$this->assertTrue($persona->update(array(
405			'nombres' => 'LOST UPDATE',
406			'telefono' => '2121'
407		)));
408
409		//Checking correct update
410		$persona = Personas::findFirst(array("estado='X'"));
411		$this->assertNotEquals($persona, false);
412		$this->assertEquals($persona->nombres, 'LOST UPDATE');
413		$this->assertEquals($persona->telefono, '2121');
414
415		//Create
416		$persona = new Personas($di);
417		$persona->cedula = 'CELL' . mt_rand(0, 999999);
418		$persona->tipo_documento_id = 1;
419		$persona->nombres = 'LOST CREATE';
420		$persona->telefono = '1';
421		$persona->cupo = 21000;
422		$persona->estado = 'A';
423		$this->assertTrue($persona->create());
424
425		$persona = new Personas($di);
426		$this->assertTrue($persona->create(array(
427			'cedula' => 'CELL' . mt_rand(0, 999999),
428			'tipo_documento_id' => 1,
429			'nombres' => 'LOST CREATE',
430			'telefono' => '1',
431			'cupo' => 21000,
432			'estado' => 'A'
433		)));
434
435		//Grouping
436		$difEstados = People::count(array("distinct" => "estado"));
437		$this->assertEquals($difEstados, 3);
438
439		$group = People::count(array("group" => "estado"));
440		$this->assertEquals(count($group), 3);
441
442		//Deleting
443		$before = People::count();
444		$this->assertTrue($persona->delete());
445		$this->assertEquals($before - 1, People::count());
446
447		//Assign
448		$persona = new Personas();
449
450		$persona->assign(array(
451			'tipo_documento_id' => 1,
452			'nombres' => 'LOST CREATE',
453			'telefono' => '1',
454			'cupo' => 21000,
455			'estado' => 'A',
456			'notField' => 'SOME VALUE'
457		));
458
459		$expected = array(
460			'cedula' => NULL,
461			'tipo_documento_id' => 1,
462			'nombres' => 'LOST CREATE',
463			'telefono' => '1',
464			'direccion' => NULL,
465			'email' => NULL,
466			'fecha_nacimiento' => NULL,
467			'ciudad_id' => NULL,
468			'creado_at' => NULL,
469			'cupo' => 21000,
470			'estado' => 'A',
471		);
472
473		$this->assertEquals($persona->toArray(), $expected);
474
475		// Issue 1701
476		$expected = array(
477			'nombres' => 'LOST CREATE',
478			'cupo' => 21000,
479			'estado' => 'A',
480		);
481
482		$this->assertEquals($persona->toArray(array('nombres', 'cupo', 'estado')), $expected);
483
484		//Refresh
485		$persona = Personas::findFirst();
486
487		$personaData = $persona->toArray();
488
489		$persona->assign(array(
490			'tipo_documento_id' => 1,
491			'nombres' => 'LOST CREATE',
492			'telefono' => '1',
493			'cupo' => 21000,
494			'estado' => 'A',
495			'notField' => 'SOME VALUE'
496		));
497
498		$persona->refresh();
499		$this->assertEquals($personaData, $persona->toArray());
500
501		// Issue 1314
502		$parts = new Parts2();
503		$parts->save();
504
505		// Issue 1506
506		$persona = Personas::findFirst(array('columns' => 'nombres, telefono, estado', "nombres = 'LOST CREATE'"));
507		$expected = array(
508			'nombres'  => 'LOST CREATE',
509			'telefono' => '1',
510			'estado' => 'A'
511		);
512
513		$this->assertEquals($expected, $persona->toArray());
514	}
515
516	protected function _executeTestsRenamed($di)
517	{
518
519		$this->_prepareDb($di->getShared('db'));
520
521		$params = array();
522		$this->assertTrue(Personers::count($params) > 0);
523
524		$params = array("status = 'I'");
525		$this->assertTrue(Personers::count($params) > 0);
526
527		$params = "status='I'";
528		$this->assertTrue(Personers::count($params) > 0);
529
530		$params = array("conditions" => "status='I'");
531		$this->assertTrue(Personers::count($params) > 0);
532
533		//Find first
534		$personer = Personers::findFirst();
535		$this->assertTrue(is_object($personer));
536		$this->assertEquals(get_class($personer), 'Personers');
537		$this->assertTrue(isset($personer->navnes));
538		$this->assertTrue(isset($personer->status));
539
540		$personer = Personers::findFirst("status = 'I'");
541		$this->assertTrue(is_object($personer));
542		$this->assertTrue(isset($personer->navnes));
543		$this->assertTrue(isset($personer->status));
544
545		$personer = Personers::findFirst(array("status='I'"));
546		$this->assertTrue(is_object($personer));
547		$this->assertTrue(isset($personer->navnes));
548		$this->assertTrue(isset($personer->status));
549
550		$params = array("conditions" => "status='I'");
551		$personer = Personers::findFirst($params);
552		$this->assertTrue(is_object($personer));
553		$this->assertTrue(isset($personer->navnes));
554		$this->assertTrue(isset($personer->status));
555
556		$params = array("conditions" => "status='A'", "order" => "navnes");
557		$personer = Personers::findFirst($params);
558		$this->assertTrue(is_object($personer));
559		$this->assertTrue(isset($personer->navnes));
560		$this->assertTrue(isset($personer->status));
561
562		$params = array("status='A'", "order" => "navnes DESC", "limit" => 30);
563		$personer = Personers::findFirst($params);
564		$this->assertTrue(is_object($personer));
565		$this->assertTrue(isset($personer->navnes));
566		$this->assertTrue(isset($personer->status));
567
568		$params = array("status=?1", "bind" => array(1 => 'A'), "order" => "navnes DESC", "limit" => 30);
569		$personer = Personers::findFirst($params);
570		$this->assertTrue(is_object($personer));
571		$this->assertTrue(isset($personer->navnes));
572		$this->assertTrue(isset($personer->status));
573
574		$params = array("status=:status:", "bind" => array("status" => 'A'), "order" => "navnes DESC", "limit" => 30);
575		$personer = Personers::findFirst($params);
576		$this->assertTrue(is_object($personer));
577		$this->assertTrue(isset($personer->navnes));
578		$this->assertTrue(isset($personer->status));
579
580		$robotter = Robotters::findFirst(1);
581		$this->assertEquals(get_class($robotter), 'Robotters');
582
583		//Find tests
584		$personers = Personers::find();
585		$this->assertTrue(count($personers) > 0);
586
587		$personers = Personers::find("status='I'");
588		$this->assertTrue(count($personers) > 0);
589
590		$personers = Personers::find(array("status='I'"));
591		$this->assertTrue(count($personers) > 0);
592
593		$personers = Personers::find(array("status='I'", "order" => "navnes"));
594		$this->assertTrue(count($personers) > 0);
595
596		$params = array("status='I'", "order" => "navnes", "limit" => 100);
597		$personers = Personers::find($params);
598		$this->assertTrue(count($personers) > 0);
599
600		$params = array("status=?1", "bind" => array(1 => "A"), "order" => "navnes", "limit" => 100);
601		$personers = Personers::find($params);
602		$this->assertTrue(count($personers) > 0);
603
604		$params = array("status=:status:", "bind" => array('status' => "A"), "order" => "navnes", "limit" => 100);
605		$personers = Personers::find($params);
606		$this->assertTrue(count($personers) > 0);
607
608		//Traverse the cursor
609		$number = 0;
610		$personers = Personers::find(array("conditions" => "status='A'", "order" => "navnes", "limit" => 20));
611		foreach($personers as $personer){
612			$number++;
613		}
614		$this->assertEquals($number, 20);
615
616		$personer = new Personers($di);
617		$personer->borgerId = 'CELL'.mt_rand(0, 999999);
618		$this->assertFalse($personer->save());
619
620		//Messages
621		$this->assertEquals(count($personer->getMessages()), 4);
622
623		$messages = array(
624			0 => ModelMessage::__set_state(array(
625				'_type' => 'PresenceOf',
626				'_message' => 'slagBorgerId is required',
627				'_field' => 'slagBorgerId',
628				'_code' => 0,
629			)),
630			1 => ModelMessage::__set_state(array(
631				'_type' => 'PresenceOf',
632				'_message' => 'navnes is required',
633				'_field' => 'navnes',
634				'_code' => 0,
635			)),
636			2 => ModelMessage::__set_state(array(
637				'_type' => 'PresenceOf',
638				'_message' => 'kredit is required',
639				'_field' => 'kredit',
640				'_code' => 0,
641			)),
642			3 => ModelMessage::__set_state(array(
643				'_type' => 'PresenceOf',
644				'_message' => 'status is required',
645				'_field' => 'status',
646				'_code' => 0,
647			)),
648		);
649		$this->assertEquals($personer->getMessages(), $messages);
650
651		//Save
652		$personer = new Personers($di);
653		$personer->borgerId = 'CELL'.mt_rand(0, 999999);
654		$personer->slagBorgerId = 1;
655		$personer->navnes = 'LOST';
656		$personer->telefon = '1';
657		$personer->kredit = 20000;
658		$personer->status = 'A';
659		$this->assertTrue($personer->save());
660
661		$personer = new Personers($di);
662		$personer->borgerId = 'CELL'.mt_rand(0, 999999);
663		$personer->slagBorgerId = 1;
664		$personer->navnes = 'LOST LOST';
665		$personer->telefon = '2';
666		$personer->kredit = 0;
667		$personer->status = 'X';
668		$this->assertTrue($personer->save());
669
670		//Check correct save
671		$personer = Personers::findFirst(array("status='X'"));
672		$this->assertNotEquals($personer, false);
673		$this->assertEquals($personer->navnes, 'LOST LOST');
674		$this->assertEquals($personer->status, 'X');
675
676		//Update
677		$personer->kredit = 150000;
678		$personer->telefon = '123';
679		$this->assertTrue($personer->update());
680
681		//Checking correct update
682		$personer = Personers::findFirst(array("status='X'"));
683		$this->assertNotEquals($personer, false);
684		$this->assertEquals($personer->kredit, 150000);
685		$this->assertEquals($personer->telefon, '123');
686
687		//Update
688		$this->assertTrue($personer->update(array(
689			'navnes' => 'LOST UPDATE',
690			'telefon' => '2121'
691		)));
692
693		//Checking correct update
694		$personer = Personers::findFirst(array("status='X'"));
695		$this->assertNotEquals($personer, false);
696		$this->assertEquals($personer->navnes, 'LOST UPDATE');
697		$this->assertEquals($personer->telefon, '2121');
698
699		//Create
700		$personer = new Personers($di);
701		$personer->borgerId = 'CELL'.mt_rand(0, 999999);
702		$personer->slagBorgerId = 1;
703		$personer->navnes = 'LOST CREATE';
704		$personer->telefon = '2';
705		$personer->kredit = 21000;
706		$personer->status = 'A';
707		$this->assertTrue($personer->save());
708
709		$personer = new Personers($di);
710		$this->assertTrue($personer->create(array(
711			'borgerId' => 'CELL'.mt_rand(0, 999999),
712			'slagBorgerId' => 1,
713			'navnes' => 'LOST CREATE',
714			'telefon' => '1',
715			'kredit' => 21000,
716			'status' => 'A'
717		)));
718
719		//Deleting
720		$before = Personers::count();
721		$this->assertTrue($personer->delete());
722		$this->assertEquals($before - 1, Personers::count());
723
724		//Assign
725		$personer = new Personers();
726
727		$personer->assign(array(
728			'slagBorgerId' => 1,
729			'navnes' => 'LOST CREATE',
730			'telefon' => '1',
731			'kredit' => 21000,
732			'status' => 'A'
733		));
734
735		$expected = array(
736			'borgerId' => NULL,
737			'slagBorgerId' => 1,
738			'navnes' => 'LOST CREATE',
739			'telefon' => '1',
740			'adresse' => NULL,
741			'elektroniskPost' => NULL,
742			'fodtDato' => NULL,
743			'fodebyId' => NULL,
744			'skabtPa' => NULL,
745			'kredit' => 21000,
746			'status' => 'A',
747		);
748		$this->assertEquals($personer->toArray(), $expected);
749
750		//Refresh
751		$personer = Personers::findFirst();
752		$personerData = $personer->toArray();
753
754		$personer->assign(array(
755			'slagBorgerId' => 1,
756			'navnes' => 'LOST CREATE',
757			'telefon' => '1',
758			'kredit' => 21000,
759			'status' => 'A'
760		));
761
762		$personer->refresh();
763		$this->assertEquals($personerData, $personer->toArray());
764	}
765}