Exemplo n.º 1
0
 public function testORM()
 {
     include_once __DIR__ . "/../libs/TestModel.php";
     //@\PHPPE\DS::db("mysql:host=localhost;dbname=testdb");
     @(include_once "vendor/phppe/Core/libs/ds_mysql.php");
     \PHPPE\DS::close();
     \PHPPE\DS::db("sqlite::memory:");
     $testModel = new TestModel();
     $this->assertEquals('a:4:{i:0;a:3:{s:2:"id";s:1:"1";s:4:"name";s:5:"first";s:8:"parentId";s:1:"0";}i:1;a:3:{s:2:"id";s:1:"2";s:4:"name";s:6:"second";s:8:"parentId";s:1:"0";}i:2;a:3:{s:2:"id";s:1:"3";s:4:"name";s:5:"third";s:8:"parentId";s:1:"1";}i:3;a:3:{s:2:"id";s:1:"4";s:4:"name";s:6:"fourth";s:8:"parentId";s:1:"0";}}', serialize($testModel->find()), "ORM find #1");
     $this->assertEquals('a:3:{i:0;a:3:{s:2:"id";s:1:"1";s:4:"name";s:5:"first";s:8:"parentId";s:1:"0";}i:1;a:3:{s:2:"id";s:1:"2";s:4:"name";s:6:"second";s:8:"parentId";s:1:"0";}i:2;a:3:{s:2:"id";s:1:"4";s:4:"name";s:6:"fourth";s:8:"parentId";s:1:"0";}}', serialize($testModel->find(0, "parentId=?")), "ORM find #2");
     $this->assertEquals('a:1:{i:0;a:3:{s:2:"id";s:1:"3";s:4:"name";s:5:"third";s:8:"parentId";s:1:"1";}}', serialize($testModel->find(1, "parentId=?")), "ORM find #3");
     $this->assertEquals(5, $testModel->save(), "ORM save insert");
     $this->assertEquals(5, $testModel->id, "ORM save id");
     $testModel->name = "sssss";
     $this->assertEquals(5, $testModel->save(), "ORM save update");
     $testModel->id = 6;
     $this->assertFalse($testModel->save(), "ORM save update fail");
     $this->assertFalse($testModel->load(7), "ORM load fail");
     $this->assertEquals(3, $testModel->load(3), "ORM load success");
     $this->assertEquals(3, $testModel->id, "ORM load id");
     $testModel->name = "sssss";
     $this->assertEquals(3, $testModel->load(3), "ORM load reload");
     $this->assertNotEquals("sssss", $testModel->name, "ORM load name");
     $testModel = new TestModel(1);
     $this->assertEquals(1, $testModel->id, "Creating with id");
     \PHPPE\DS::close();
     $wasExc = false;
     try {
         $testModel->save();
     } catch (\Exception $e) {
         $wasExc = true;
     }
     $this->assertTrue($wasExc, "save exception no ds");
     $testModel = new TestModel(['id' => 1, 'name' => 'one']);
     $this->assertEquals(1, $testModel->id, "Creating with fields");
     $testModel = new BadModel();
     $wasExc = false;
     try {
         $testModel->save();
     } catch (\Exception $e) {
         $wasExc = true;
     }
     $this->assertTrue($wasExc, "save exception no table");
     $wasExc = false;
     try {
         $testModel->find();
     } catch (\Exception $e) {
         $wasExc = true;
     }
     $this->assertTrue($wasExc, "find exception");
     $wasExc = false;
     try {
         $testModel->load();
     } catch (\Exception $e) {
         $wasExc = true;
     }
     $this->assertTrue($wasExc, "load exception");
 }