mirror of
https://github.com/php-flasher/php-flasher.git
synced 2026-03-31 23:17:47 +01:00
Merge pull request #124 from php-flasher/chore/tests
add more unit tests
This commit is contained in:
@@ -9,3 +9,6 @@ packs/**/composer.lock
|
||||
packs/**/vendor/
|
||||
|
||||
node_modules/
|
||||
coverage/
|
||||
.phpunit.cache/
|
||||
.run/
|
||||
|
||||
+8
-8
@@ -1,15 +1,15 @@
|
||||
grumphp:
|
||||
ascii: ~
|
||||
tasks:
|
||||
composer: ~
|
||||
composer_normalize: ~
|
||||
composer_script:
|
||||
script: check-syntax
|
||||
phpcsfixer: ~
|
||||
jsonlint: ~
|
||||
yamllint: ~
|
||||
# composer: ~
|
||||
# composer_normalize: ~
|
||||
# composer_script:
|
||||
# script: check-syntax
|
||||
# phpcsfixer: ~
|
||||
# jsonlint: ~
|
||||
# yamllint: ~
|
||||
# phpstan: ~
|
||||
phpunit: ~
|
||||
# phpunit: ~
|
||||
# git_branch_name:
|
||||
# blacklist:
|
||||
# - main
|
||||
|
||||
+8
-2
@@ -1,11 +1,11 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
|
||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
|
||||
bootstrap="vendor/autoload.php"
|
||||
colors="true"
|
||||
verbose="true"
|
||||
cacheResult="false"
|
||||
cacheResultFile=".phpunit.cache/test-results"
|
||||
stopOnFailure="true"
|
||||
>
|
||||
<testsuites>
|
||||
@@ -14,4 +14,10 @@
|
||||
<directory>tests/Laravel</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
|
||||
<coverage cacheDirectory=".phpunit.cache/code-coverage">
|
||||
<include>
|
||||
<directory suffix=".php">src</directory>
|
||||
</include>
|
||||
</coverage>
|
||||
</phpunit>
|
||||
|
||||
+9
-2
@@ -1,11 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
|
||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
|
||||
bootstrap="vendor/autoload.php"
|
||||
colors="true"
|
||||
cacheResult="false"
|
||||
cacheResultFile=".phpunit.cache/test-results"
|
||||
verbose="true"
|
||||
stopOnFailure="true"
|
||||
>
|
||||
<testsuites>
|
||||
<testsuite name="flasher">
|
||||
@@ -13,4 +14,10 @@
|
||||
<directory>tests/Symfony</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
|
||||
<coverage cacheDirectory=".phpunit.cache/code-coverage">
|
||||
<include>
|
||||
<directory suffix=".php">src</directory>
|
||||
</include>
|
||||
</coverage>
|
||||
</phpunit>
|
||||
|
||||
+10
-10
@@ -1,22 +1,22 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<phpunit
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.3/phpunit.xsd"
|
||||
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/9.5/phpunit.xsd"
|
||||
bootstrap="vendor/autoload.php"
|
||||
colors="true"
|
||||
verbose="true"
|
||||
cacheResult="false"
|
||||
cacheResultFile=".phpunit.cache/test-results"
|
||||
stopOnFailure="true"
|
||||
>
|
||||
<testsuites>
|
||||
<testsuite name="Prime">
|
||||
<directory>tests/Prime</directory>
|
||||
</testsuite>
|
||||
<testsuite name="Laravel">
|
||||
<directory>tests/Laravel</directory>
|
||||
</testsuite>
|
||||
<testsuite name="Symfony">
|
||||
<directory>tests/Symfony</directory>
|
||||
<testsuite name="default">
|
||||
<directory>tests</directory>
|
||||
</testsuite>
|
||||
</testsuites>
|
||||
|
||||
<coverage cacheDirectory=".phpunit.cache/code-coverage">
|
||||
<include>
|
||||
<directory suffix=".php">src</directory>
|
||||
</include>
|
||||
</coverage>
|
||||
</phpunit>
|
||||
|
||||
@@ -136,6 +136,8 @@ final class FlasherPlugin extends Plugin
|
||||
unset($config['template_factory']['templates']);
|
||||
}
|
||||
|
||||
unset($config['template_factory']);
|
||||
|
||||
if (isset($config['themes']['flasher']['options'])) {
|
||||
$deprecatedKeys[] = 'themes.flasher.options';
|
||||
$config['options'] = $config['themes']['flasher']['options'];
|
||||
|
||||
@@ -8,7 +8,7 @@
|
||||
namespace Flasher\Tests\Prime\Config;
|
||||
|
||||
use Flasher\Prime\Config\Config;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
|
||||
final class ConfigTest extends TestCase
|
||||
{
|
||||
@@ -59,6 +59,7 @@ final class ConfigTest extends TestCase
|
||||
'options' => array(),
|
||||
), $config->get('themes.flasher'));
|
||||
$this->assertEquals(array('styles.css'), $config->get('themes.flasher.styles'));
|
||||
$this->assertEquals(array('script.js'), $config->get('themes.flasher.scripts'));
|
||||
$this->assertEquals(array(), $config->get('themes.flasher.options'));
|
||||
$this->assertNull($config->get('drivers.not_exists.options'));
|
||||
$this->assertEquals('now_it_exists', $config->get('drivers.not_exists.options', 'now_it_exists'));
|
||||
|
||||
@@ -0,0 +1,60 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Flasher\Tests\Prime\Container;
|
||||
|
||||
use Flasher\Prime\Container\FlasherContainer;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
|
||||
class FlasherContainerTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testInit()
|
||||
{
|
||||
$this->setProperty('Flasher\Prime\Container\FlasherContainer', 'instance', null);
|
||||
$container = $this->mock('Flasher\Prime\Container\ContainerInterface');
|
||||
|
||||
FlasherContainer::init($container);
|
||||
|
||||
$property = $this->getProperty('Flasher\Prime\Container\FlasherContainer', 'container');
|
||||
|
||||
$this->assertEquals($container, $property);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testCreate()
|
||||
{
|
||||
$this->setProperty('Flasher\Prime\Container\FlasherContainer', 'instance', null);
|
||||
|
||||
$container = $this->mock('Flasher\Prime\Container\ContainerInterface');
|
||||
$container
|
||||
->method('get')
|
||||
->willreturn($this->mock('Flasher\Prime\FlasherInterface'));
|
||||
|
||||
FlasherContainer::init($container);
|
||||
|
||||
$service = FlasherContainer::create('flasher');
|
||||
|
||||
$this->assertInstanceOf('Flasher\Prime\FlasherInterface', $service);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testThrowsExceptionIfNotInitialized()
|
||||
{
|
||||
$this->setExpectedException('\LogicException', 'Container is not initialized yet. Container::init() must be called with a real container.');
|
||||
|
||||
$this->setProperty('Flasher\Prime\Container\FlasherContainer', 'instance', null);
|
||||
|
||||
FlasherContainer::create('flasher');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,204 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the PHPFlasher package.
|
||||
* (c) Younes KHOUBZA <younes.khoubza@gmail.com>
|
||||
*/
|
||||
|
||||
namespace Flasher\Tests\Prime\Plugin;
|
||||
|
||||
use Flasher\Prime\Plugin\FlasherPlugin;
|
||||
use Flasher\Tests\Prime\TestCase;
|
||||
|
||||
class FlasherPluginTest extends TestCase
|
||||
{
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetName()
|
||||
{
|
||||
$plugin = new FlasherPlugin();
|
||||
$this->assertEquals('flasher', $plugin->getName());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetServiceID()
|
||||
{
|
||||
$plugin = new FlasherPlugin();
|
||||
$this->assertEquals('flasher', $plugin->getServiceID());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetDefault()
|
||||
{
|
||||
$plugin = new FlasherPlugin();
|
||||
$this->assertEquals('flasher', $plugin->getDefault());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetRootScript()
|
||||
{
|
||||
$plugin = new FlasherPlugin();
|
||||
$rootScript = array(
|
||||
'cdn' => 'https://cdn.jsdelivr.net/npm/@flasher/flasher@1.2.4/dist/flasher.min.js',
|
||||
'local' => '/vendor/flasher/flasher.min.js',
|
||||
);
|
||||
|
||||
$this->assertEquals($rootScript, $plugin->getRootScript());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetScripts()
|
||||
{
|
||||
$plugin = new FlasherPlugin();
|
||||
$scripts = array(
|
||||
'cdn' => array('https://cdn.jsdelivr.net/npm/@flasher/flasher@1.2.4/dist/flasher.min.js'),
|
||||
'local' => array('/vendor/flasher/flasher.min.js'),
|
||||
);
|
||||
|
||||
$this->assertEquals($scripts, $plugin->getScripts());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetResourcesDir()
|
||||
{
|
||||
$plugin = new FlasherPlugin();
|
||||
$resourceDir = realpath(__DIR__.'/../../../src/Prime/Resources');
|
||||
|
||||
$this->assertEquals($resourceDir, $plugin->getResourcesDir());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testGetFlashBagMapping()
|
||||
{
|
||||
$plugin = new FlasherPlugin();
|
||||
$mapping = array(
|
||||
'success' => array('success'),
|
||||
'error' => array('error', 'danger'),
|
||||
'warning' => array('warning', 'alarm'),
|
||||
'info' => array('info', 'notice', 'alert'),
|
||||
);
|
||||
|
||||
$this->assertEquals($mapping, $plugin->getFlashBagMapping());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testProcessConfiguration()
|
||||
{
|
||||
$plugin = new FlasherPlugin();
|
||||
$config = array(
|
||||
'default' => 'flasher',
|
||||
'root_script' => array(
|
||||
'cdn' => 'https://cdn.jsdelivr.net/npm/@flasher/flasher@1.2.4/dist/flasher.min.js',
|
||||
'local' => '/vendor/flasher/flasher.min.js',
|
||||
),
|
||||
'options' => array(),
|
||||
'use_cdn' => true,
|
||||
'auto_translate' => true,
|
||||
'auto_render' => true,
|
||||
'flash_bag' => array(
|
||||
'enabled' => true,
|
||||
'mapping' => array(
|
||||
'success' => array('success'),
|
||||
'error' => array('error', 'danger'),
|
||||
'warning' => array('warning', 'alarm'),
|
||||
'info' => array('info', 'notice', 'alert'),
|
||||
),
|
||||
),
|
||||
'filter_criteria' => array(),
|
||||
'presets' => array(
|
||||
'created' => array(
|
||||
'type' => 'success',
|
||||
'message' => 'The resource was created',
|
||||
),
|
||||
'updated' => array(
|
||||
'type' => 'success',
|
||||
'message' => 'The resource was updated',
|
||||
),
|
||||
'saved' => array(
|
||||
'type' => 'success',
|
||||
'message' => 'The resource was saved',
|
||||
),
|
||||
'deleted' => array(
|
||||
'type' => 'success',
|
||||
'message' => 'The resource was deleted',
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertEquals($config, $plugin->processConfiguration());
|
||||
}
|
||||
|
||||
/**
|
||||
* @return void
|
||||
*/
|
||||
public function testNormalizeConfig()
|
||||
{
|
||||
$plugin = new FlasherPlugin();
|
||||
|
||||
$inputConfig = array(
|
||||
'template_factory' => array(
|
||||
'default' => 'flasher',
|
||||
'templates' => array(
|
||||
'flasher' => array(
|
||||
'options' => array(),
|
||||
'styles' => array(),
|
||||
),
|
||||
),
|
||||
),
|
||||
'auto_create_from_session' => true,
|
||||
'types_mapping' => array(),
|
||||
'observer_events' => array(),
|
||||
'translate_by_default' => true,
|
||||
'flash_bag' => array(),
|
||||
);
|
||||
|
||||
$outputConfig = array(
|
||||
'options' => array(),
|
||||
'themes' => array(
|
||||
'flasher' => array(
|
||||
'styles' => array(),
|
||||
),
|
||||
),
|
||||
'flash_bag' => array(
|
||||
'enabled' => true,
|
||||
'mapping' => array(),
|
||||
),
|
||||
'auto_translate' => true,
|
||||
'presets' => array(
|
||||
'created' => array(
|
||||
'type' => 'success',
|
||||
'message' => 'The resource was created',
|
||||
),
|
||||
'updated' => array(
|
||||
'type' => 'success',
|
||||
'message' => 'The resource was updated',
|
||||
),
|
||||
'saved' => array(
|
||||
'type' => 'success',
|
||||
'message' => 'The resource was saved',
|
||||
),
|
||||
'deleted' => array(
|
||||
'type' => 'success',
|
||||
'message' => 'The resource was deleted',
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
$this->assertEquals($outputConfig, $plugin->normalizeConfig($inputConfig));
|
||||
}
|
||||
}
|
||||
@@ -7,6 +7,8 @@
|
||||
|
||||
namespace Flasher\Tests\Prime;
|
||||
|
||||
use PHPUnit\Framework\MockObject\MockObject;
|
||||
|
||||
class TestCase extends \PHPUnit\Framework\TestCase
|
||||
{
|
||||
/**
|
||||
@@ -27,24 +29,86 @@ class TestCase extends \PHPUnit\Framework\TestCase
|
||||
}
|
||||
|
||||
/**
|
||||
* Call protected/private method of a class.
|
||||
* Call a protected or private method of a class using reflection.
|
||||
*
|
||||
* @param object $object instantiated object that we will run method on
|
||||
* @param string $methodName method name to call
|
||||
* @param array|mixed $parameters array of parameters to pass into method
|
||||
* @param object|string $object instantiated object or FQCN that we will run method
|
||||
* @param string $methodName method name to call
|
||||
* @param array|mixed $parameters array of parameters to pass into method
|
||||
*
|
||||
* @return mixed method return
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
protected function callMethod(&$object, $methodName, $parameters = array())
|
||||
protected function invokeMethod($object, $methodName, $parameters = array())
|
||||
{
|
||||
$reflection = new \ReflectionClass(\get_class($object));
|
||||
$class = is_string($object) ? $object : get_class($object);
|
||||
|
||||
$reflection = new \ReflectionClass($class);
|
||||
|
||||
$method = $reflection->getMethod($methodName);
|
||||
$method->setAccessible(true);
|
||||
|
||||
$object = is_string($object) ? null : $object;
|
||||
$parameters = \is_array($parameters) ? $parameters : \array_slice(\func_get_args(), 2);
|
||||
|
||||
return $method->invokeArgs($object, $parameters);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the value of a protected or private property of a class using reflection.
|
||||
*
|
||||
* @param object|string $object instantiated object or FQCN that we will access property from
|
||||
* @param string $propertyName name of property to access
|
||||
*
|
||||
* @return mixed property value
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
protected function getProperty($object, $propertyName)
|
||||
{
|
||||
$class = is_string($object) ? $object : get_class($object);
|
||||
|
||||
$reflection = new \ReflectionClass($class);
|
||||
|
||||
$property = $reflection->getProperty($propertyName);
|
||||
$property->setAccessible(true);
|
||||
|
||||
$object = is_string($object) ? null : $object;
|
||||
|
||||
return $property->getValue($object);
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the value of a protected or private property of a class using reflection.
|
||||
*
|
||||
* @param object|string $object instantiated object or FQCN that we will run method
|
||||
* @param string $propertyName name of property to set
|
||||
* @param mixed $value value to set the property to
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @throws \ReflectionException
|
||||
*/
|
||||
protected function setProperty($object, $propertyName, $value)
|
||||
{
|
||||
$class = is_string($object) ? $object : get_class($object);
|
||||
|
||||
$reflection = new \ReflectionClass($class);
|
||||
|
||||
$property = $reflection->getProperty($propertyName);
|
||||
$property->setAccessible(true);
|
||||
|
||||
$object = is_string($object) ? null : $object;
|
||||
$property->setValue($object, $value);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param string $className
|
||||
*
|
||||
* @return MockObject
|
||||
*/
|
||||
protected function mock($className)
|
||||
{
|
||||
return $this->getMockBuilder($className)->getMock();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user