PHP Classes

File: test/EntityTest.php

Recommend this page to a friend!
  Classes of Victor Bolshov   Tiny PHP ORM Framework   test/EntityTest.php   Download  
File: test/EntityTest.php
Role: Unit test script
Content type: text/plain
Description: Unit test script
Class: Tiny PHP ORM Framework
Map objects to databases using composed queries
Author: By
Last change: Merge branch 'master' of github.com:crocodile2u/tinyorm
Date: 7 years ago
Size: 2,018 bytes
 

Contents

Class file image Download
<?php

namespace tinyorm\test;

use
tinyorm\Entity;
use
tinyorm\persistence\DbDriver;

class
EntityTest extends BaseTestCase {

   
/**
     * @var DbDriver
     */
   
private $persistenceDriver;

    function
testFind()
    {
       
$id = $this->assertEntitySaved();
       
$entity = TestEntity::find($id);
       
$this->assertInstanceOf(TestEntity::class, $entity);
       
$this->assertEquals($id, $entity->getPK());
       
$this->assertEquals("UNIQUE", $entity->c_unique);
    }

    function
testSave()
    {
       
$entity = new TestEntity([
           
"c_unique" => "UNIQUE"
       
]);
       
$entity->save();
       
$this->assertGreaterThan(0, $entity->getPK());
    }

    function
testIncrement()
    {
       
$id = $this->assertEntitySaved();
       
$memoryEntity = TestEntity::find($id);
       
$this->assertTrue($memoryEntity->increment("c_int", 10));
       
$this->assertEquals(10, $memoryEntity->c_int);

       
$dbEntity = TestEntity::find($id);
       
$this->assertEquals(10, $dbEntity->c_int);
    }

    function
testDelete()
    {
       
$entity = new TestEntity([
           
"c_unique" => "UNIQUE"
       
]);
       
$this->persistenceDriver->save($entity, $rowCount);
       
$this->assertTrue($entity->delete());
       
$this->assertNull(TestEntity::find($entity->getPK()));
    }

   
/**
     * @return int new entity ID
     */
   
function assertEntitySaved()
    {
       
$entity = new TestEntity([
           
"c_unique" => "UNIQUE",
           
"c_int" => 0,
        ]);
       
$this->persistenceDriver->save($entity, $rowCount);
       
$this->assertEquals(1, $rowCount);
        return
$entity->getPK();
    }

    function
setUp()
    {
       
parent::setUp();
       
$this->persistenceDriver = new DbDriver($this->connection);
       
Entity::setDefaultPersistenceDriver($this->persistenceDriver);
    }

    function
tearDown()
    {
       
parent::tearDown();
       
$this->persistenceDriver = null;
       
Entity::unsetDefaultPersistenceDriver();
    }
}