Ejemplo n.º 1
0
 /**
  * @depends testSetterGetter
  * @depends testSetMultiple
  */
 public function testGetAndHasId()
 {
     // normal id
     $a = Test_Basic::create();
     $this->assertFalse($a->hasId());
     $a->set('id', 768);
     $this->assertTrue($a->hasId());
     $this->assertEquals(768, $a->getId());
     // custom id
     $a = DifferentIdField::create();
     $this->assertFalse($a->hasId());
     $a->set('custom_id', 98765);
     $this->assertTrue($a->hasId());
     $this->assertEquals(98765, $a->getId());
     // compound keys
     $compoundModel = Test\Compound::create();
     $this->assertFalse($compoundModel->hasId());
     $compoundModel->setData(array('foo_id' => 1, 'bar_id' => 77, 'name' => 'Foo Bar'));
     $this->assertTrue($compoundModel->hasId());
     $this->assertEquals(array('foo_id' => 1, 'bar_id' => 77), $compoundModel->getId());
 }
Ejemplo n.º 2
0
 /**
  * @depends testBasic
  * @depends testNullIfNotFound
  */
 public function testCompound()
 {
     // check if empty
     $result = Test\Compound::findAll();
     $this->assertInternalType('array', $result);
     $this->assertEmpty($result);
     // create
     $model1 = Test\Compound::create();
     $model1->foo_id = 5;
     $model1->bar_id = 10;
     $model1->name = '5 to 10';
     $this->assertTrue($model1->save());
     // create
     $model2 = Test\Compound::create();
     $model2->foo_id = 7;
     $model2->bar_id = 10;
     $model2->name = '7 to 10';
     $this->assertTrue($model2->save());
     // create
     $model3 = Compound::create();
     $model3->foo_id = 11;
     $model3->bar_id = 1;
     $model3->name = '11 to 1';
     $this->assertTrue($model3->save());
     // create and delete
     $model4 = Test\Compound::create();
     $model4->foo_id = 11;
     $model4->bar_id = 8;
     $model4->name = '11 to 8';
     $this->assertTrue($model4->save());
     $this->assertTrue($model4->delete());
     // query one
     $model = Test\Compound::find(5, 10);
     $this->assertInstanceOf('\\RormTest\\Test\\Compound', $model);
     $this->assertEquals(5, $model->foo_id);
     $this->assertEquals(10, $model->bar_id);
     // query many
     $query = Test\Compound::query();
     $query->whereGt('foo_id', 6);
     $query->orderByAsc('foo_id');
     $result = $query->findMany();
     $this->assertInstanceOf('\\Rorm\\QueryIterator', $result);
     foreach ($result as $model) {
         /** @var Compound $model */
         // check if correct model
         $this->assertInstanceOf('\\RormTest\\Test\\Compound', $model);
         // check if not filtered item
         $this->assertNotEquals($model1->foo_id, $model->foo_id);
     }
     // query buffered
     $result = Test\Compound::findAll();
     $this->assertInternalType('array', $result);
     $this->assertNotEmpty($result);
     $this->assertContainsOnlyInstancesOf('\\RormTest\\Test\\Compound', $result);
     $this->assertEquals(3, count($result));
 }
Ejemplo n.º 3
0
 /**
  * @depends testCompound
  * @expectedException Exception
  */
 public function testQueryRewind()
 {
     $result = Compound::query()->findMany();
     $this->assertNotEmpty($result);
     foreach ($result as $model) {
         $this->assertInstanceOf('\\RormTest\\Test\\Compound', $model);
     }
     // here the exception should get thrown
     foreach ($result as $model) {
         $this->assertInstanceOf('\\RormTest\\Test\\Compound', $model);
     }
 }
Ejemplo n.º 4
0
 /**
  * @depends testWhereMethods
  * @depends testBuild
  */
 public function testBuildWhere()
 {
     // basic
     $queryBasic = QueryModel::query();
     $queryBasic->where('id', 1);
     $this->assertEquals('SELECT * FROM `test` WHERE `id` = ?', $queryBasic->build()->getQuery());
     $this->assertEquals(array(1), $queryBasic->getParams());
     // multiple
     $queryMultiple = QueryModel::query()->where('id', 1)->where('name', 'loremipsum')->build();
     $this->assertEquals('SELECT * FROM `test` WHERE `id` = ? AND `name` = ?', $queryMultiple->getQuery());
     $this->assertEquals(array(1, 'loremipsum'), $queryMultiple->getParams());
     // id params
     $queryId = QueryModel::query();
     $queryId->whereId(10)->build();
     $this->assertEquals('SELECT * FROM `test` WHERE `id` = ?', $queryId->getQuery());
     $this->assertEquals(array(10), $queryId->getParams());
     // id compound
     $queryIdCompound = Compound::query()->whereId(5, 75)->build();
     $this->assertEquals('SELECT * FROM `rormtest_test_compound` WHERE `foo_id` = ? AND `bar_id` = ?', $queryIdCompound->getQuery());
     $this->assertEquals(array(5, 75), $queryIdCompound->getParams());
     // expression
     $queryExpression = QueryModel::query()->whereExpr('id', '10 + 20')->whereExpr('modified', 'NOW()')->build();
     $this->assertEquals('SELECT * FROM `test` WHERE `id` = 10 + 20 AND `modified` = NOW()', $queryExpression->getQuery());
     // raw
     $queryRaw = QueryModel::query()->whereRaw('1 < ?', array(20))->whereRaw('`modified` <= NOW()')->whereRaw('SUM(`number`) < ? AND YEAR(NOW()) <= ?', array(100, 2010))->build();
     $this->assertEquals('SELECT * FROM `test` WHERE 1 < ? AND `modified` <= NOW() AND SUM(`number`) < ? AND YEAR(NOW()) <= ?', $queryRaw->getQuery());
     $this->assertEquals(array(20, 100, 2010), $queryRaw->getParams());
     // lt and gt
     $queryCompare = QueryModel::query();
     $queryCompare->whereLt('id', 10)->whereLte('number', 20)->whereGt('id', 0)->whereGte('number', 75)->build();
     $this->assertEquals('SELECT * FROM `test` WHERE `id` < ? AND `number` <= ? AND `id` > ? AND `number` >= ?', $queryCompare->getQuery());
     $this->assertEquals(array(10, 20, 0, 75), $queryCompare->getParams());
     // not null
     $queryNotNull = QueryModel::query()->whereNotNull('modified')->build();
     $this->assertEquals('SELECT * FROM `test` WHERE `modified` IS NOT NULL', $queryNotNull->getQuery());
     // null
     $queryNotNull = QueryModel::query()->whereNull('field')->build();
     $this->assertEquals('SELECT * FROM `test` WHERE `field` IS NULL', $queryNotNull->getQuery());
     // not
     $queryNotNull = QueryModel::query()->whereNot('field', 1234)->build();
     $this->assertEquals("SELECT * FROM `test` WHERE `field` != ?", $queryNotNull->getQuery());
 }