mirror of
https://github.com/torrentpier/torrentpier
synced 2025-08-22 22:33:55 -07:00
Merge pull request #89 from diolektor/add-new-tests
Add new tests and refactoring
This commit is contained in:
commit
d36e5deadf
11 changed files with 413 additions and 21 deletions
|
@ -8,6 +8,6 @@ php:
|
|||
|
||||
before_script:
|
||||
- travis_retry composer self-update
|
||||
- travis_retry composer install --no-interaction --prefer-source
|
||||
- travis_retry composer install --no-interaction
|
||||
|
||||
script: phpunit --configuration phpunit.xml --coverage-text
|
|
@ -10,9 +10,4 @@ abstract class Entity
|
|||
{
|
||||
return $this->table;
|
||||
}
|
||||
|
||||
public function initData($data)
|
||||
{
|
||||
return $this;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -24,7 +24,7 @@ class Di extends Container
|
|||
return static::$instance;
|
||||
}
|
||||
|
||||
throw new \Exception('The container has not been initialized');
|
||||
throw new \RuntimeException('The container has not been initialized');
|
||||
}
|
||||
|
||||
public function __get($id)
|
||||
|
@ -33,6 +33,6 @@ class Di extends Container
|
|||
return $this->offsetGet($id);
|
||||
}
|
||||
|
||||
throw new \Exception("Service '{$id}' is not registered in the container");
|
||||
throw new \RuntimeException("Service '{$id}' is not registered in the container");
|
||||
}
|
||||
}
|
||||
|
|
|
@ -13,6 +13,9 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
|
|||
*/
|
||||
public $config;
|
||||
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
$data = [
|
||||
|
@ -28,23 +31,25 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
|
|||
}
|
||||
|
||||
/**
|
||||
* @covers \TorrentPier\Config::get
|
||||
* Get value from array by key
|
||||
*
|
||||
* @see \TorrentPier\Config::get
|
||||
*/
|
||||
public function testGet()
|
||||
{
|
||||
$this->assertEquals($this->config->get('key1'), 'value1');
|
||||
$this->assertEquals($this->config->get('key2.key3'), 'value2');
|
||||
$this->assertEquals($this->config->get('key2.key4'), 'value1');
|
||||
$this->assertEquals($this->config->get('key2.key5'), 'value1');
|
||||
$this->assertEquals($this->config->get('key2')->get('key3'), 'value2');
|
||||
static::assertEquals($this->config->get('key1'), 'value1');
|
||||
static::assertEquals($this->config->get('key2.key3'), 'value2');
|
||||
static::assertEquals($this->config->get('key2.key4'), 'value1');
|
||||
static::assertEquals($this->config->get('key2.key5'), 'value1');
|
||||
static::assertEquals($this->config->get('key2')->get('key3'), 'value2');
|
||||
|
||||
$this->assertEquals($this->config['key1'], 'value1');
|
||||
$this->assertEquals($this->config['key2.key3'], 'value2');
|
||||
$this->assertEquals($this->config['key2.key4'], 'value1');
|
||||
$this->assertEquals($this->config['key2.key5'], 'value1');
|
||||
$this->assertEquals($this->config['key2']['key3'], 'value2');
|
||||
static::assertEquals($this->config['key1'], 'value1');
|
||||
static::assertEquals($this->config['key2.key3'], 'value2');
|
||||
static::assertEquals($this->config['key2.key4'], 'value1');
|
||||
static::assertEquals($this->config['key2.key5'], 'value1');
|
||||
static::assertEquals($this->config['key2']['key3'], 'value2');
|
||||
|
||||
$this->assertEquals($this->config['key2.key6'], null);
|
||||
static::assertEquals($this->config['key2.key6'], null);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -52,7 +57,7 @@ class ConfigTest extends \PHPUnit_Framework_TestCase
|
|||
*/
|
||||
public function testToArray()
|
||||
{
|
||||
$this->assertEquals($this->config->toArray(), [
|
||||
static::assertEquals($this->config->toArray(), [
|
||||
'key1' => 'value1',
|
||||
'key2' => [
|
||||
'key3' => 'value2',
|
||||
|
|
20
tests/src/Db/EntityTest.php
Normal file
20
tests/src/Db/EntityTest.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace TorrentPier\Db;
|
||||
|
||||
class EntityTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @covers TorrentPier\Db\Entity::table
|
||||
*/
|
||||
public function testGetTableName()
|
||||
{
|
||||
$model = new Model();
|
||||
static::assertEquals('table name', $model->table());
|
||||
}
|
||||
}
|
||||
|
||||
class Model extends Entity
|
||||
{
|
||||
protected $table = 'table name';
|
||||
}
|
55
tests/src/DiTest.php
Normal file
55
tests/src/DiTest.php
Normal file
|
@ -0,0 +1,55 @@
|
|||
<?php
|
||||
|
||||
namespace TorrentPier;
|
||||
|
||||
/**
|
||||
* Class DiTest
|
||||
* @package TorrentPier
|
||||
*/
|
||||
class DiTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @expectedException \RuntimeException
|
||||
* @expectedExceptionMessage The container has not been initialized
|
||||
*/
|
||||
public function testGetInstanceIsNotInitialized()
|
||||
{
|
||||
Di::getInstance();
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \TorrentPier\Di::getInstance
|
||||
* @depends testGetInstanceIsNotInitialized
|
||||
*/
|
||||
public function testGetInstance()
|
||||
{
|
||||
$di = new Di();
|
||||
static::assertEquals($di, Di::getInstance());
|
||||
}
|
||||
|
||||
/**
|
||||
* @depends testGetInstanceIsNotInitialized
|
||||
* @expectedException \RuntimeException
|
||||
* @expectedExceptionMessage Service 'test' is not registered in the container
|
||||
*/
|
||||
public function testGetByPropertyIsNotExists()
|
||||
{
|
||||
$di = new Di();
|
||||
$di->test;
|
||||
}
|
||||
|
||||
/**
|
||||
* @see \TorrentPier\Di::__get
|
||||
* @depends testGetInstanceIsNotInitialized
|
||||
*/
|
||||
public function testGetByProperty()
|
||||
{
|
||||
$di = new Di([
|
||||
'test' => function () {
|
||||
return 'test string';
|
||||
}
|
||||
]);
|
||||
|
||||
static::assertEquals('test string', $di->test);
|
||||
}
|
||||
}
|
218
tests/src/LogTest.php
Normal file
218
tests/src/LogTest.php
Normal file
|
@ -0,0 +1,218 @@
|
|||
<?php
|
||||
|
||||
namespace TorrentPier;
|
||||
|
||||
use Psr\Log\NullLogger;
|
||||
|
||||
/**
|
||||
* Class LogTest
|
||||
* @package TorrentPier
|
||||
*/
|
||||
class LogTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @inheritdoc
|
||||
*/
|
||||
public function setUp()
|
||||
{
|
||||
$mockLogger = $this->getMockBuilder(NullLogger::class)
|
||||
->setMethods(['log', 'debug', 'info', 'notice', 'warning', 'error', 'critical', 'alert', 'emergency'])
|
||||
->getMock();
|
||||
|
||||
new Di([
|
||||
'log' => function () use ($mockLogger) {
|
||||
return $mockLogger;
|
||||
}
|
||||
]);
|
||||
}
|
||||
|
||||
/**
|
||||
* @see \TorrentPier\Log::log
|
||||
*/
|
||||
public function testLog()
|
||||
{
|
||||
$level = 'level';
|
||||
$message = 'log test string';
|
||||
$context = ['key' => 'value'];
|
||||
|
||||
/** @var \PHPUnit_Framework_MockObject_MockObject $mockLogger */
|
||||
$mockLogger = Log::getLogger();
|
||||
$mockLogger
|
||||
->expects(static::once())
|
||||
->method('log')
|
||||
->with(static::equalTo($level), static::equalTo($message), static::equalTo($context))
|
||||
->willReturnCallback(function ($level, $message) {
|
||||
return $level . '::' . $message;
|
||||
});
|
||||
|
||||
static::assertEquals($level . '::' . $message, Log::log($level, $message, $context));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \TorrentPier\Log::debug
|
||||
*/
|
||||
public function testDebug()
|
||||
{
|
||||
$message = 'debug test string';
|
||||
$context = ['key' => 'value'];
|
||||
|
||||
/** @var \PHPUnit_Framework_MockObject_MockObject $mockLogger */
|
||||
$mockLogger = Log::getLogger();
|
||||
$mockLogger
|
||||
->expects(static::once())
|
||||
->method('debug')
|
||||
->with(static::equalTo($message), static::equalTo($context))
|
||||
->willReturnCallback(function ($message) {
|
||||
return $message;
|
||||
});
|
||||
|
||||
static::assertEquals($message, Log::debug($message, $context));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \TorrentPier\Log::info
|
||||
*/
|
||||
public function testInfo()
|
||||
{
|
||||
$message = 'info test string';
|
||||
$context = ['key' => 'value'];
|
||||
|
||||
/** @var \PHPUnit_Framework_MockObject_MockObject $mockLogger */
|
||||
$mockLogger = Log::getLogger();
|
||||
$mockLogger
|
||||
->expects(static::once())
|
||||
->method('info')
|
||||
->with(static::equalTo($message), static::equalTo($context))
|
||||
->willReturnCallback(function ($message) {
|
||||
return $message;
|
||||
});
|
||||
|
||||
static::assertEquals($message, Log::info($message, $context));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \TorrentPier\Log::notice
|
||||
*/
|
||||
public function testNotice()
|
||||
{
|
||||
$message = 'notice test string';
|
||||
$context = ['key' => 'value'];
|
||||
|
||||
/** @var \PHPUnit_Framework_MockObject_MockObject $mockLogger */
|
||||
$mockLogger = Log::getLogger();
|
||||
$mockLogger
|
||||
->expects(static::once())
|
||||
->method('notice')
|
||||
->with(static::equalTo($message), static::equalTo($context))
|
||||
->willReturnCallback(function ($message) {
|
||||
return $message;
|
||||
});
|
||||
|
||||
static::assertEquals($message, Log::notice($message, $context));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \TorrentPier\Log::warning
|
||||
*/
|
||||
public function testWarning()
|
||||
{
|
||||
$message = 'warning test string';
|
||||
$context = ['key' => 'value'];
|
||||
|
||||
/** @var \PHPUnit_Framework_MockObject_MockObject $mockLogger */
|
||||
$mockLogger = Log::getLogger();
|
||||
$mockLogger
|
||||
->expects(static::once())
|
||||
->method('warning')
|
||||
->with(static::equalTo($message), static::equalTo($context))
|
||||
->willReturnCallback(function ($message) {
|
||||
return $message;
|
||||
});
|
||||
|
||||
static::assertEquals($message, Log::warning($message, $context));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \TorrentPier\Log::error
|
||||
*/
|
||||
public function testError()
|
||||
{
|
||||
$message = 'error test string';
|
||||
$context = ['key' => 'value'];
|
||||
|
||||
/** @var \PHPUnit_Framework_MockObject_MockObject $mockLogger */
|
||||
$mockLogger = Log::getLogger();
|
||||
$mockLogger
|
||||
->expects(static::once())
|
||||
->method('error')
|
||||
->with(static::equalTo($message), static::equalTo($context))
|
||||
->willReturnCallback(function ($message) {
|
||||
return $message;
|
||||
});
|
||||
|
||||
static::assertEquals($message, Log::error($message, $context));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \TorrentPier\Log::critical
|
||||
*/
|
||||
public function testCritical()
|
||||
{
|
||||
$message = 'critical test string';
|
||||
$context = ['key' => 'value'];
|
||||
|
||||
/** @var \PHPUnit_Framework_MockObject_MockObject $mockLogger */
|
||||
$mockLogger = Log::getLogger();
|
||||
$mockLogger
|
||||
->expects(static::once())
|
||||
->method('critical')
|
||||
->with(static::equalTo($message), static::equalTo($context))
|
||||
->willReturnCallback(function ($message) {
|
||||
return $message;
|
||||
});
|
||||
|
||||
static::assertEquals($message, Log::critical($message, $context));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \TorrentPier\Log::alert
|
||||
*/
|
||||
public function testAlert()
|
||||
{
|
||||
$message = 'alert test string';
|
||||
$context = ['key' => 'value'];
|
||||
|
||||
/** @var \PHPUnit_Framework_MockObject_MockObject $mockLogger */
|
||||
$mockLogger = Log::getLogger();
|
||||
$mockLogger
|
||||
->expects(static::once())
|
||||
->method('alert')
|
||||
->with(static::equalTo($message), static::equalTo($context))
|
||||
->willReturnCallback(function ($message) {
|
||||
return $message;
|
||||
});
|
||||
|
||||
static::assertEquals($message, Log::alert($message, $context));
|
||||
}
|
||||
|
||||
/**
|
||||
* @covers \TorrentPier\Log::emergency
|
||||
*/
|
||||
public function testEmergency()
|
||||
{
|
||||
$message = 'emergency test string';
|
||||
$context = ['key' => 'value'];
|
||||
|
||||
/** @var \PHPUnit_Framework_MockObject_MockObject $mockLogger */
|
||||
$mockLogger = Log::getLogger();
|
||||
$mockLogger
|
||||
->expects(static::once())
|
||||
->method('emergency')
|
||||
->with(static::equalTo($message), static::equalTo($context))
|
||||
->willReturnCallback(function ($message) {
|
||||
return $message;
|
||||
});
|
||||
|
||||
static::assertEquals($message, Log::emergency($message, $context));
|
||||
}
|
||||
}
|
22
tests/src/ServiceProviders/CaptchaServiceProviderTest.php
Normal file
22
tests/src/ServiceProviders/CaptchaServiceProviderTest.php
Normal file
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
namespace TorrentPier\ServiceProviders;
|
||||
|
||||
use ReCaptcha\ReCaptcha;
|
||||
use TorrentPier\Di;
|
||||
|
||||
class CaptchaServiceProviderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \TorrentPier\ServiceProviders\CaptchaServiceProvider::register
|
||||
*/
|
||||
public function testRegisterService()
|
||||
{
|
||||
$di = new Di();
|
||||
$di->register(new CaptchaServiceProvider, [
|
||||
'config.captcha.secret_key' => 'secret key'
|
||||
]);
|
||||
|
||||
static::assertInstanceOf(ReCaptcha::class, $di->captcha);
|
||||
}
|
||||
}
|
20
tests/src/ServiceProviders/RequestServiceProviderTest.php
Normal file
20
tests/src/ServiceProviders/RequestServiceProviderTest.php
Normal file
|
@ -0,0 +1,20 @@
|
|||
<?php
|
||||
|
||||
namespace TorrentPier\ServiceProviders;
|
||||
|
||||
use Symfony\Component\HttpFoundation\Request;
|
||||
use TorrentPier\Di;
|
||||
|
||||
class RequestServiceProviderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \TorrentPier\ServiceProviders\RequestServiceProvider::register
|
||||
*/
|
||||
public function testRegisterService()
|
||||
{
|
||||
$di = new Di();
|
||||
$di->register(new RequestServiceProvider);
|
||||
|
||||
static::assertInstanceOf(Request::class, $di->request);
|
||||
}
|
||||
}
|
22
tests/src/ServiceProviders/ViewServiceProviderTest.php
Normal file
22
tests/src/ServiceProviders/ViewServiceProviderTest.php
Normal file
|
@ -0,0 +1,22 @@
|
|||
<?php
|
||||
|
||||
namespace TorrentPier\ServiceProviders;
|
||||
|
||||
use TorrentPier\Di;
|
||||
use TorrentPier\View;
|
||||
|
||||
class ViewServiceProviderTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @covers \TorrentPier\ServiceProviders\ViewServiceProvider::register
|
||||
*/
|
||||
public function testRegisterService()
|
||||
{
|
||||
$di = new Di();
|
||||
$di->register(new ViewServiceProvider, [
|
||||
'twig' => new \Twig_Environment()
|
||||
]);
|
||||
|
||||
static::assertInstanceOf(View::class, $di->view);
|
||||
}
|
||||
}
|
35
tests/src/ViewTest.php
Normal file
35
tests/src/ViewTest.php
Normal file
|
@ -0,0 +1,35 @@
|
|||
<?php
|
||||
|
||||
namespace TorrentPier;
|
||||
|
||||
/**
|
||||
* Class ViewTest
|
||||
* @package TorrentPier
|
||||
*/
|
||||
class ViewTest extends \PHPUnit_Framework_TestCase
|
||||
{
|
||||
/**
|
||||
* @see \TorrentPier\View::make
|
||||
*/
|
||||
public function testMake()
|
||||
{
|
||||
$templateFileName = 'template';
|
||||
$templateParam = ['key' => 'value'];
|
||||
|
||||
$mockTwig = $this
|
||||
->getMockBuilder(\Twig_Environment::class)
|
||||
->setMethods(['render'])
|
||||
->getMock();
|
||||
|
||||
$mockTwig
|
||||
->expects(static::once())
|
||||
->method('render')
|
||||
->with(static::equalTo($templateFileName . '.twig'), static::equalTo($templateParam))
|
||||
->willReturnCallback(function () {
|
||||
return 'test render';
|
||||
});
|
||||
|
||||
$view = new View($mockTwig);
|
||||
static::assertEquals('test render', $view->make($templateFileName, $templateParam));
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue