Пример #1
0
 public function testOtherFind()
 {
     $pq = EQM::query(Project::class)->where('companyId = ?', 2)->result();
     $pm = Project::findByCompanyId(2);
     $this->assertEquals($pq->count(), $pm->count());
     $this->assertEquals($pq->one()->name, $pm->one()->name);
 }
Пример #2
0
 public function testAliasing()
 {
     $result = EQM::query()->select('*')->from('Company Firma')->where('Firma.id = ?', 2)->result()->one();
     $this->assertEquals(2, $result->id);
     $result = EQM::query('Company Firma')->innerJoin('Project Projekt', 'Firma.id = Projekt.companyId')->innerJoin('ProjectActivity Aktivitaet', 'Projekt.id = Aktivitaet.projectId')->where('Aktivitaet.id = :id', ['id' => 100])->groupBy('Firma.id')->result();
     $this->assertEquals(27, $result->count());
 }
Пример #3
0
 public function testResult()
 {
     $result = EQM::query(Company::class)->result();
     $count = $result->count();
     $all = $result->all();
     $this->assertEquals($all, $all->all());
     $this->assertInstanceOf(Company::class, $all->one());
     $this->assertEquals($all->count(), $count);
 }
Пример #4
0
 public function testUpdateError()
 {
     $c = new Company();
     try {
         EQM::update($c);
         $r = true;
     } catch (EQMException $e) {
         $r = false;
     }
     $this->assertFalse($r);
 }
Пример #5
0
 /**
  * saves a record with a auto increment primary key which means if the key is null
  * it will be inserted and if a key exists it will be updated
  *
  * @return bool|object
  * @throws EQMException
  */
 public function save()
 {
     $tableMeta = EQM::tableMeta(get_class($this));
     if ($tableMeta->hasAutoIncrement()) {
         if (empty($this->{$tableMeta->getAutoIncrement()})) {
             return $this->insert();
         } else {
             return $this->update();
         }
     } else {
         throw new EQMException('save() is not possible for entities without auto increment primary key', 10001);
     }
 }
Пример #6
0
 public function testInitialize()
 {
     try {
         EQM::initialize(null, [], 'test0');
     } catch (EQMException $e) {
         $this->assertEquals($e->getMessage(), 'The given paramter is not a valid PDO connection object');
     }
     EQM::initialize(new \PDO('mysql:host=localhost;dbname=orm_test', 'root', 'root'), [], 'test1');
     $this->assertTrue(EQM::isInitialized('test1'));
     EQM::initialize(new \PDO('mysql:host=localhost;dbname=orm_test', 'root', 'root'), [EQM::CONVENTION_HANDLER => new ClassicConventionHandler()], 'test2');
     $this->assertTrue(EQM::isInitialized('test2'));
     EQM::initialize(new \PDO('mysql:host=localhost;dbname=orm_test', 'root', 'root'), [EQM::SQL_BUILDER => new MySqlBuilder()], 'test3');
     $this->assertTrue(EQM::isInitialized('test3'));
 }
Пример #7
0
 public function testComplex()
 {
     $companies = EQM::queryByArray(['entity' => 'Company']);
     $count = $companies->count();
     EQM::begin();
     foreach ($companies as $company) {
         $company->remark = 'Transaction remark';
         EQM::update($company);
         $projects = EQM::queryByArray(['entity' => Project::class, 'query' => 'id = ?', 'params' => $company->id]);
         foreach ($projects as $project) {
             $project->value = 999.99;
             EQM::update($project);
         }
     }
     EQM::rollBack();
     $companyCount = EQM::queryByArray(['entity' => 'Company'])->count();
     $this->assertEquals($count, $companyCount);
     $companies = EQM::queryByArray(['entity' => 'Company', 'query' => 'remark = ?', 'params' => 'Transaction remark']);
     $this->assertEquals(0, $companies->count());
 }
Пример #8
0
 public function testInsertError()
 {
     $c = new Company();
     try {
         EQM::insert($c);
         $r = true;
     } catch (EQMException $e) {
         $r = false;
     }
     $this->assertFalse($r);
     $c = new Bootstrap\Company();
     try {
         EQM::insert($c);
         $r = true;
     } catch (EQMException $e) {
         $r = false;
     }
     $this->assertFalse($r);
     $c->name = null;
     $c->remark = 'no no not good';
     try {
         EQM::insert($c);
         $r = true;
     } catch (EQMException $e) {
         $r = false;
     }
     $this->assertFalse($r);
     $p = new Project();
     try {
         EQM::insert($p);
         $r = true;
     } catch (EQMException $e) {
         $r = false;
     }
     $this->assertFalse($r);
     $p->id = null;
     $p->name = 'not good as I said';
     $p->value = 999;
     try {
         EQM::insert($p);
         $r = true;
     } catch (EQMException $e) {
         $r = false;
     }
     $this->assertFalse($r);
     $p->companyId = 1;
     $p->name = 'not good as I said';
     $p->value = 9999;
     try {
         EQM::insert($p);
         $r = true;
     } catch (EQMException $e) {
         $r = false;
     }
     $this->assertFalse($r);
 }
Пример #9
0
 /**
  * @return \troba\EQM\Query query object of the called class
  */
 public static function query()
 {
     return EQM::query(get_called_class());
 }
Пример #10
0
 public function testQueryWithOrder()
 {
     $this->assertEquals(CNT_COMPANY, EQM::queryByArray(['entity' => \Bootstrap\Project::class, 'order' => 'companyId DESC'])->one()->companyId);
     $this->assertEquals(CNT_COMPANY, EQM::queryByArray(['entity' => AnotherCompany::class, 'order' => 'AnotherCompany.id DESC'])->one()->id);
     $this->assertEquals(1, EQM::queryByArray(['entity' => \Bootstrap\Project::class, 'order' => 'companyId'])->one()->companyId);
 }
Пример #11
0
function generate($max_i, $max_j, $max_k)
{
    $c = new Company();
    for ($i = 0; $i < $max_i; $i++) {
        $c->name = (isset($c->id) ? $c->id + 1 : 1) . ' A Company Name';
        $c->remark = 'A remark for a company with the name ' . $c->name;
        EQM::insert($c);
        $p = new Project();
        for ($j = 0; $j < $max_j; $j++) {
            $p->id = $c->id . '_' . ($j + 1) . '_PROJECT';
            $p->companyId = $c->id;
            $p->name = 'A project with the id ' . $p->id;
            $p->value = $j % 10 * 100 + $c->id % $j / $j;
            EQM::insert($p);
            $pa = new ProjectActivity();
            for ($k = 0; $k < $max_k; $k++) {
                $pa->id = 100 + $k;
                $pa->projectId = $p->id;
                $pa->name = 'Activity for ' . $p->id . ' with ' . $pa->id;
                EQM::insert($pa);
            }
        }
    }
}
Пример #12
0
 public function testDeleteError()
 {
     $company = new Company();
     try {
         EQM::delete($company);
         $r = true;
     } catch (EQMException $e) {
         $r = false;
     }
     $this->assertFalse($r);
     $projectActivity = new ProjectActivity();
     try {
         EQM::delete($projectActivity);
         $r = true;
     } catch (EQMException $e) {
         $r = false;
     }
     $this->assertFalse($r);
     $projectActivity->id = 999;
     try {
         EQM::delete($projectActivity);
         $r = true;
     } catch (EQMException $e) {
         $r = false;
     }
     $this->assertFalse($r);
     $projectActivity->id = 999;
     $projectActivity->projectId = 'WHAT_EVER';
     try {
         EQM::delete($projectActivity);
         # TODO zero result nothing has been deleted
         $r = true;
     } catch (EQMException $e) {
         $r = false;
     }
     $this->assertTrue($r);
 }
Пример #13
0
 /**
  * find an object by it primary key(s)
  *
  * @param array|mixed $primaries a single value or an assoc array
  * @return object the requested entity
  */
 public static function find($primaries)
 {
     return EQM::queryByPrimary(get_called_class(), $primaries);
 }
Пример #14
0
 public function testPostDelete()
 {
     $c = new CCompany();
     $c->name = 'NAME';
     EQM::insert($c);
     EQM::delete($c);
     $this->assertEquals($c->remark, 'DELETED');
 }