PHP Classes

File: tests/complex-tests/MySQLPersistComplexTypesTest.php

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

 

Contents

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

require_once dirname(__FILE__).'/../MySqlAbstractDatabaseTestCase.php';
require_once dirname(__FILE__).'/../Model/ReferencedGuestBook.php';
require_once dirname(__FILE__).'/../Model/User.php';

use Nkey\Caribu\Orm\Orm;

use Nkey\Caribu\Tests\Model\ReferencedGuestBook;
use Nkey\Caribu\Tests\Model\User;

class MySQLPersistComplexTypesTest extends MySqlAbstractDatabaseTestCase
{
    public function __construct()
    {
        parent::__construct();

        $this->dataSetFile = dirname(__FILE__).'/../_files/referenced-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("DROP TABLE IF EXISTS `user`");
        $connection->exec("CREATE TABLE `user` (`uid` INTEGER PRIMARY KEY AUTO_INCREMENT, `name` VARCHAR(50), `email` VARCHAR(50))");
        $connection->exec("CREATE TABLE `guestbook` (`id` INTEGER PRIMARY KEY AUTO_INCREMENT, `content` TEXT, `user` TEXT, `created` TIMESTAMP)");
        $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->exec("DROP TABLE `user`");
        $connection->commit();

        parent::tearDown();
    }

    public function testPersist()
    {
        $user = User::find(array('name' => 'bob'));

        $entity = new ReferencedGuestBook();
        $entity->setCreated(new \DateTime());
        $entity->setContent("Some test content to persist");
        $entity->setUser($user);

        $entity->persist();

        $this->assertFalse(is_null($entity->getGid()));

        $second = ReferencedGuestBook::get($entity->getGid());

        $this->assertEquals($entity->getCreated()->getTimestamp(), $second->getCreated()->getTimestamp(), '', 1);
    }
}