2014-09-14 18:41:14 +02:00
|
|
|
<?php
|
|
|
|
namespace Szurubooru\Tests\Dao;
|
2014-10-08 14:47:47 +02:00
|
|
|
use Szurubooru\Dao\TokenDao;
|
|
|
|
use Szurubooru\Dao\TransactionManager;
|
|
|
|
use Szurubooru\Entities\Token;
|
|
|
|
use Szurubooru\Tests\AbstractDatabaseTestCase;
|
2014-09-14 18:41:14 +02:00
|
|
|
|
2014-10-08 14:47:47 +02:00
|
|
|
final class TransactionManagerTest extends AbstractDatabaseTestCase
|
2014-09-14 18:41:14 +02:00
|
|
|
{
|
|
|
|
public function testCommit()
|
|
|
|
{
|
|
|
|
$testEntity = $this->getTestEntity();
|
|
|
|
$testDao = $this->getTestDao();
|
|
|
|
|
|
|
|
$transactionManager = $this->getTransactionManager();
|
|
|
|
$transactionManager->commit(function() use ($testDao, &$testEntity)
|
|
|
|
{
|
|
|
|
$testDao->save($testEntity);
|
|
|
|
$this->assertNotNull($testEntity->getId());
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertNotNull($testEntity->getId());
|
2014-09-21 09:35:43 +02:00
|
|
|
$this->assertEntitiesEqual($testEntity, $testDao->findById($testEntity->getId()));
|
2014-09-14 18:41:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public function testRollback()
|
|
|
|
{
|
|
|
|
$testEntity = $this->getTestEntity();
|
|
|
|
$testDao = $this->getTestDao();
|
|
|
|
|
|
|
|
$transactionManager = $this->getTransactionManager();
|
|
|
|
$transactionManager->rollback(function() use ($testDao, &$testEntity)
|
|
|
|
{
|
|
|
|
$testDao->save($testEntity);
|
|
|
|
$this->assertNotNull($testEntity->getId());
|
|
|
|
});
|
|
|
|
|
|
|
|
//ids that could be forged in transaction get left behind after rollback
|
|
|
|
$this->assertNotNull($testEntity->getId());
|
|
|
|
|
2014-11-09 13:08:24 +01:00
|
|
|
$this->databaseConnection->getPDO()->rollBack();
|
|
|
|
$this->databaseConnection->getPDO()->beginTransaction();
|
|
|
|
|
2014-09-14 18:41:14 +02:00
|
|
|
//but entities shouldn't be saved to database
|
|
|
|
$this->assertNull($testDao->findById($testEntity->getId()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public function testNestedTransactions()
|
|
|
|
{
|
|
|
|
$testEntity = $this->getTestEntity();
|
|
|
|
$testDao = $this->getTestDao();
|
|
|
|
|
|
|
|
$transactionManager = $this->getTransactionManager();
|
|
|
|
$transactionManager->commit(function() use ($transactionManager, $testDao, &$testEntity)
|
|
|
|
{
|
|
|
|
$transactionManager->commit(function() use ($testDao, &$testEntity)
|
|
|
|
{
|
|
|
|
$testDao->save($testEntity);
|
|
|
|
$this->assertNotNull($testEntity->getId());
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
$this->assertNotNull($testEntity->getId());
|
2014-09-21 09:35:43 +02:00
|
|
|
$this->assertEntitiesEqual($testEntity, $testDao->findById($testEntity->getId()));
|
2014-09-14 18:41:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getTestEntity()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
$token = new Token();
|
2014-09-14 18:41:14 +02:00
|
|
|
$token->setName('yo');
|
2014-10-08 14:47:47 +02:00
|
|
|
$token->setPurpose(Token::PURPOSE_ACTIVATE);
|
2014-09-14 18:41:14 +02:00
|
|
|
return $token;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function getTestDao()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
return new TokenDao($this->databaseConnection);
|
2014-09-14 18:41:14 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private function getTransactionManager()
|
|
|
|
{
|
2014-10-08 14:47:47 +02:00
|
|
|
return new TransactionManager($this->databaseConnection);
|
2014-09-14 18:41:14 +02:00
|
|
|
}
|
|
|
|
}
|