szuru/tests/Dao/TransactionManagerTest.php

85 lines
2.6 KiB
PHP
Raw Permalink Normal View History

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