PHP Classes

File: tests/complex-tests/MySQLComplexTest.php

Recommend this page to a friend!
  Classes of Maik Greubel   Caribu ORM   tests/complex-tests/MySQLComplexTest.php   Download  
File: tests/complex-tests/MySQLComplexTest.php
Role: Unit test script
Content type: text/plain
Description: Unit test for MySQL complex tests
Class: Caribu ORM
Map objects to databases records using annotations
Author: By
Last change: Strong type and documentation fixes
Date: 7 years ago
Size: 2,552 bytes
 

Contents

Class file image Download
<?php
namespace Nkey\Caribu\Tests;

require_once
dirname(__FILE__).'/../MySqlAbstractDatabaseTestCase.php';
require_once
dirname(__FILE__).'/../Model/MockedModel.php';
require_once
dirname(__FILE__).'/../Model/GuestBookModel.php';
require_once
dirname(__FILE__).'/../Model/AnnotatedGuestBookModel.php';

use
Nkey\Caribu\Tests\Model\GuestBookModel;
use
Nkey\Caribu\Tests\Model\AnnotatedGuestBookModel;

use
Nkey\Caribu\Orm\Orm;

/**
 * Complex test cases (mysql is used)
 *
 * @author Maik Greubel <greubel@nkey.de>
 * This class is part of Caribu package
 */
class MySQLComplexTest extends MySqlAbstractDatabaseTestCase
{
    public function
__construct()
    {
       
parent::__construct();

       
$this->flatDataSetFile = dirname(__FILE__).'/../_files/guestbook-seed.xml';
    }

   
/**
     * (non-PHPdoc)
     * @see \PHPUnit\DbUnit\TestCase::setUp()
     */
   
protected function setUp()
    {
       
Orm::passivate();

       
$connection = $this->getConnection()->getConnection();
       
$connection->beginTransaction();
       
$connection->exec("DROP TABLE IF EXISTS `guestbook`");
       
$connection->exec("CREATE TABLE `guestbook` (`id` INTEGER PRIMARY KEY AUTO_INCREMENT, `content` TEXT, `user` TEXT, `created` TEXT)");
       
$connection->commit();

       
parent::setUp();
    }

   
/**
     * (non-PHPdoc)
     * @see \PHPUnit\DbUnit\TestCase::tearDown()
     */
   
protected function tearDown()
    {
       
$connection = $this->getConnection()->getConnection();
       
$connection->beginTransaction();
       
$connection->exec("DROP TABLE `guestbook`");
       
$connection->commit();

       
parent::tearDown();
    }

   
/**
     * Test saving entity
     */
   
public function testSave()
    {
       
$entity = GuestBookModel::find(array('user' => 'alice'));
       
$this->assertTrue(is_null($entity));

       
$entity = new GuestBookModel();
       
$entity->setUser('alice');
       
$entity->setCreated(time());
       
$entity->setContent("Another nonsense posting");

       
$entity->persist();

       
$entity = GuestBookModel::find(array('user' => 'alice'));
       
$this->assertFalse(is_null($entity));
       
$this->assertFalse(is_null($entity->getId()));
    }

   
/**
     * Test removing entity
     */
   
public function testRemove()
    {
       
$entity = AnnotatedGuestBookModel::get(1);
       
$this->assertFalse(is_null($entity));
       
$entity->delete();

       
$entity = AnnotatedGuestBookModel::find(array('id' => 1));
       
$this->assertTrue(is_null($entity));
    }
}