szurubooru/tests/AbstractTestCase.php

106 lines
2.4 KiB
PHP
Raw Normal View History

2014-09-01 20:51:59 +02:00
<?php
namespace Szurubooru\Tests;
use Szurubooru\DatabaseConnection;
use Szurubooru\Injector;
2014-09-01 20:51:59 +02:00
abstract class AbstractTestCase extends \PHPUnit_Framework_TestCase
{
2014-09-27 21:33:31 +02:00
protected function setUp()
{
Injector::init();
2014-10-02 19:19:55 +02:00
date_default_timezone_set('UTC');
2014-09-27 21:33:31 +02:00
}
protected function tearDown()
{
$this->cleanTestDirectory();
}
protected function mock($className)
2014-09-01 20:51:59 +02:00
{
return $this->getMockBuilder($className)->disableOriginalConstructor()->getMock();
}
2014-09-05 20:08:54 +02:00
2014-09-27 21:33:31 +02:00
protected function mockConfig($dataPath = null, $publicDataPath = null)
2014-09-05 20:08:54 +02:00
{
return new ConfigMock($dataPath, $publicDataPath);
2014-09-05 20:08:54 +02:00
}
2014-09-27 21:33:31 +02:00
protected function mockTransactionManager()
2014-09-14 18:41:14 +02:00
{
return new TransactionManagerMock($this->mock(DatabaseConnection::class));
2014-09-14 18:41:14 +02:00
}
2014-09-27 21:33:31 +02:00
protected function createTestDirectory()
{
$path = $this->getTestDirectoryPath();
if (!file_exists($path))
mkdir($path, 0777, true);
return $path;
}
2014-09-27 21:33:31 +02:00
protected function getTestFile($fileName)
2014-09-15 11:38:24 +02:00
{
return file_get_contents($this->getTestFilePath($fileName));
}
2014-09-27 21:33:31 +02:00
protected function getTestFilePath($fileName)
2014-09-15 11:38:24 +02:00
{
return __DIR__ . DIRECTORY_SEPARATOR . 'test_files' . DIRECTORY_SEPARATOR . $fileName;
}
2014-09-27 21:33:31 +02:00
protected function assertEntitiesEqual($expected, $actual)
{
2014-09-27 21:33:31 +02:00
if (!is_array($expected))
{
$expected = [$expected];
$actual = [$actual];
}
$this->assertEquals(count($expected), count($actual), 'Unmatching array sizes');
$this->assertEquals(array_keys($expected), array_keys($actual), 'Unmatching array keys');
foreach (array_keys($expected) as $key)
{
if ($expected[$key] === null)
{
$this->assertNull($actual[$key]);
}
else
{
$this->assertNotNull($actual[$key]);
$expected[$key]->resetLazyLoaders();
$expected[$key]->resetMeta();
$actual[$key]->resetLazyLoaders();
$actual[$key]->resetMeta();
$this->assertEquals($expected[$key], $actual[$key]);
}
}
}
private function getTestDirectoryPath()
{
return __DIR__ . DIRECTORY_SEPARATOR . 'files';
}
private function cleanTestDirectory()
{
if (!file_exists($this->getTestDirectoryPath()))
return;
$dirIterator = new \RecursiveDirectoryIterator(
$this->getTestDirectoryPath(),
\RecursiveDirectoryIterator::SKIP_DOTS);
$files = new \RecursiveIteratorIterator(
$dirIterator,
\RecursiveIteratorIterator::CHILD_FIRST);
foreach ($files as $fileInfo)
{
if ($fileInfo->isDir())
rmdir($fileInfo->getRealPath());
else
unlink($fileInfo->getRealPath());
}
}
2014-09-01 20:51:59 +02:00
}