From 3dfe475282db6221190240d57b6883a4ee6d2926 Mon Sep 17 00:00:00 2001 From: Khoubza Younes Date: Sun, 22 Jan 2023 21:40:49 +0100 Subject: [PATCH 1/7] chore: update test coverage settings --- .gitignore | 3 +++ phpunit-laravel.xml | 10 ++++++++-- phpunit-symfony.xml | 11 +++++++++-- phpunit.xml | 20 ++++++++++---------- 4 files changed, 30 insertions(+), 14 deletions(-) diff --git a/.gitignore b/.gitignore index e29934be..fdcc8999 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,6 @@ packs/**/composer.lock packs/**/vendor/ node_modules/ +coverage/ +.phpunit.cache/ +.run/ diff --git a/phpunit-laravel.xml b/phpunit-laravel.xml index e087e31a..a0c2882e 100644 --- a/phpunit-laravel.xml +++ b/phpunit-laravel.xml @@ -1,11 +1,11 @@ @@ -14,4 +14,10 @@ tests/Laravel + + + + src + + diff --git a/phpunit-symfony.xml b/phpunit-symfony.xml index c5a9cded..b690128a 100644 --- a/phpunit-symfony.xml +++ b/phpunit-symfony.xml @@ -1,11 +1,12 @@ @@ -13,4 +14,10 @@ tests/Symfony + + + + src + + diff --git a/phpunit.xml b/phpunit.xml index 350f608d..4a1f774d 100644 --- a/phpunit.xml +++ b/phpunit.xml @@ -1,22 +1,22 @@ - - tests/Prime - - - tests/Laravel - - - tests/Symfony + + tests + + + + src + + From a5292eae4574ce0aef294daaa83122c9e13aad45 Mon Sep 17 00:00:00 2001 From: Khoubza Younes Date: Sun, 22 Jan 2023 21:42:29 +0100 Subject: [PATCH 2/7] chore: add setProperty, getProperty, invokeMethod and getMock helper methods --- tests/Prime/TestCase.php | 76 ++++++++++++++++++++++++++++++++++++---- 1 file changed, 70 insertions(+), 6 deletions(-) diff --git a/tests/Prime/TestCase.php b/tests/Prime/TestCase.php index 228e6db2..28eabc82 100644 --- a/tests/Prime/TestCase.php +++ b/tests/Prime/TestCase.php @@ -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 getMock($className) + { + return $this->getMockBuilder($className)->getMock(); + } } From af7994659944cd73074da21ab7b46b971f9ce6de Mon Sep 17 00:00:00 2001 From: Khoubza Younes Date: Sun, 22 Jan 2023 21:43:42 +0100 Subject: [PATCH 3/7] test: update config tests --- tests/Prime/Config/ConfigTest.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/tests/Prime/Config/ConfigTest.php b/tests/Prime/Config/ConfigTest.php index 254c9c62..7cd82410 100644 --- a/tests/Prime/Config/ConfigTest.php +++ b/tests/Prime/Config/ConfigTest.php @@ -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')); From d9ddb381ed14225266b1e2b2d44080d15b18d0b5 Mon Sep 17 00:00:00 2001 From: Khoubza Younes Date: Sun, 22 Jan 2023 21:48:20 +0100 Subject: [PATCH 4/7] test: add FlasherPlugin tests --- src/Prime/Plugin/FlasherPlugin.php | 2 + tests/Prime/Plugin/FlasherPluginTest.php | 204 +++++++++++++++++++++++ 2 files changed, 206 insertions(+) create mode 100644 tests/Prime/Plugin/FlasherPluginTest.php diff --git a/src/Prime/Plugin/FlasherPlugin.php b/src/Prime/Plugin/FlasherPlugin.php index 205b7d04..f8deb7fd 100644 --- a/src/Prime/Plugin/FlasherPlugin.php +++ b/src/Prime/Plugin/FlasherPlugin.php @@ -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']; diff --git a/tests/Prime/Plugin/FlasherPluginTest.php b/tests/Prime/Plugin/FlasherPluginTest.php new file mode 100644 index 00000000..db0bc319 --- /dev/null +++ b/tests/Prime/Plugin/FlasherPluginTest.php @@ -0,0 +1,204 @@ + + */ + +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)); + } +} From df7c886abd4683a6d1737c46c44b7c3c29ee2d52 Mon Sep 17 00:00:00 2001 From: Khoubza Younes Date: Sun, 22 Jan 2023 21:49:13 +0100 Subject: [PATCH 5/7] style: update grumphp config --- grumphp.yml | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/grumphp.yml b/grumphp.yml index 36cd8ee6..c5a71a70 100644 --- a/grumphp.yml +++ b/grumphp.yml @@ -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 From 366787eb80ea6fe2d2d50e8433e7b814b6bc3d76 Mon Sep 17 00:00:00 2001 From: Khoubza Younes Date: Sun, 22 Jan 2023 21:49:31 +0100 Subject: [PATCH 6/7] test: add FlasherContainer tests --- .../Prime/Container/FlasherContainerTest.php | 60 +++++++++++++++++++ 1 file changed, 60 insertions(+) create mode 100644 tests/Prime/Container/FlasherContainerTest.php diff --git a/tests/Prime/Container/FlasherContainerTest.php b/tests/Prime/Container/FlasherContainerTest.php new file mode 100644 index 00000000..595a4079 --- /dev/null +++ b/tests/Prime/Container/FlasherContainerTest.php @@ -0,0 +1,60 @@ + + */ + +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->getMock('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->getMock('Flasher\Prime\Container\ContainerInterface'); + $container + ->method('get') + ->willreturn($this->getMock('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'); + } +} From 48177d6a9c2279a83a615b35ad61d682c384e2c7 Mon Sep 17 00:00:00 2001 From: Khoubza Younes Date: Mon, 23 Jan 2023 00:20:15 +0100 Subject: [PATCH 7/7] test: rename getMock() to mock() --- tests/Prime/Container/FlasherContainerTest.php | 6 +++--- tests/Prime/TestCase.php | 2 +- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/tests/Prime/Container/FlasherContainerTest.php b/tests/Prime/Container/FlasherContainerTest.php index 595a4079..d097fe36 100644 --- a/tests/Prime/Container/FlasherContainerTest.php +++ b/tests/Prime/Container/FlasherContainerTest.php @@ -18,7 +18,7 @@ class FlasherContainerTest extends TestCase public function testInit() { $this->setProperty('Flasher\Prime\Container\FlasherContainer', 'instance', null); - $container = $this->getMock('Flasher\Prime\Container\ContainerInterface'); + $container = $this->mock('Flasher\Prime\Container\ContainerInterface'); FlasherContainer::init($container); @@ -34,10 +34,10 @@ class FlasherContainerTest extends TestCase { $this->setProperty('Flasher\Prime\Container\FlasherContainer', 'instance', null); - $container = $this->getMock('Flasher\Prime\Container\ContainerInterface'); + $container = $this->mock('Flasher\Prime\Container\ContainerInterface'); $container ->method('get') - ->willreturn($this->getMock('Flasher\Prime\FlasherInterface')); + ->willreturn($this->mock('Flasher\Prime\FlasherInterface')); FlasherContainer::init($container); diff --git a/tests/Prime/TestCase.php b/tests/Prime/TestCase.php index 28eabc82..c0734145 100644 --- a/tests/Prime/TestCase.php +++ b/tests/Prime/TestCase.php @@ -107,7 +107,7 @@ class TestCase extends \PHPUnit\Framework\TestCase * * @return MockObject */ - protected function getMock($className) + protected function mock($className) { return $this->getMockBuilder($className)->getMock(); }