| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391 |
- <?php
- declare(strict_types=1);
- /**
- * CakePHP(tm) : Rapid Development Framework (https://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- *
- * Licensed under The MIT License
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org)
- * @link https://cakephp.org CakePHP(tm) Project
- * @since 5.1.0
- * @license https://opensource.org/licenses/mit-license.php MIT License
- */
- namespace Cake\Test\TestCase\Core;
- use Cake\Core\Configure;
- use Cake\Core\PluginConfig;
- use Cake\TestSuite\TestCase;
- class PluginConfigTest extends TestCase
- {
- protected string $pluginsListPath;
- protected string $pluginsConfigPath;
- protected string $originalPluginsConfigContent = '';
- /**
- * Setup
- */
- public function setUp(): void
- {
- parent::setUp();
- $this->clearPlugins();
- $this->pluginsListPath = ROOT . DS . 'cakephp-plugins.php';
- if (file_exists($this->pluginsListPath)) {
- unlink($this->pluginsListPath);
- }
- $this->pluginsConfigPath = CONFIG . DS . 'plugins.php';
- if (file_exists($this->pluginsConfigPath)) {
- $this->originalPluginsConfigContent = file_get_contents($this->pluginsConfigPath);
- }
- }
- /**
- * Reverts the changes done to the environment while testing
- */
- public function tearDown(): void
- {
- parent::tearDown();
- $this->clearPlugins();
- if (file_exists($this->pluginsListPath)) {
- unlink($this->pluginsListPath);
- }
- file_put_contents($this->pluginsConfigPath, $this->originalPluginsConfigContent);
- }
- public function testSimpleConfig(): void
- {
- $file = <<<PHP
- <?php
- return [
- 'plugins' => [
- 'TestPlugin' => '/config/path',
- 'OtherPlugin' => '/config/path',
- ]
- ];
- PHP;
- file_put_contents($this->pluginsListPath, $file);
- $config = <<<PHP
- <?php
- return [
- 'TestPlugin',
- 'OtherPlugin',
- ];
- PHP;
- file_put_contents($this->pluginsConfigPath, $config);
- Configure::delete('plugins');
- $result = [
- 'TestPlugin' => [
- 'isLoaded' => true,
- 'onlyDebug' => false,
- 'onlyCli' => false,
- 'optional' => false,
- 'bootstrap' => true,
- 'console' => true,
- 'middleware' => true,
- 'routes' => true,
- 'services' => true,
- 'events' => true,
- ],
- 'OtherPlugin' => [
- 'isLoaded' => true,
- 'onlyDebug' => false,
- 'onlyCli' => false,
- 'optional' => false,
- 'bootstrap' => true,
- 'console' => true,
- 'middleware' => true,
- 'routes' => true,
- 'services' => true,
- 'events' => true,
- ],
- ];
- $this->assertSame($result, PluginConfig::getAppConfig());
- }
- public function testOnlyOnePlugin(): void
- {
- $file = <<<PHP
- <?php
- return [
- 'plugins' => [
- 'TestPlugin' => '/config/path',
- 'OtherPlugin' => '/config/path',
- ]
- ];
- PHP;
- file_put_contents($this->pluginsListPath, $file);
- $config = <<<PHP
- <?php
- return [
- 'TestPlugin',
- ];
- PHP;
- file_put_contents($this->pluginsConfigPath, $config);
- $result = [
- 'TestPlugin' => [
- 'isLoaded' => true,
- 'onlyDebug' => false,
- 'onlyCli' => false,
- 'optional' => false,
- 'bootstrap' => true,
- 'console' => true,
- 'middleware' => true,
- 'routes' => true,
- 'services' => true,
- 'events' => true,
- ],
- 'OtherPlugin' => [
- 'isLoaded' => false,
- ],
- ];
- $this->assertSame($result, PluginConfig::getAppConfig());
- }
- public function testSpecificEnvironmentsAndHooks(): void
- {
- $file = <<<PHP
- <?php
- return [
- 'plugins' => [
- 'OtherPlugin' => '/config/path',
- 'AnotherPlugin' => '/config/path'
- ]
- ];
- PHP;
- file_put_contents($this->pluginsListPath, $file);
- $config = <<<PHP
- <?php
- return [
- 'OtherPlugin' => ['onlyDebug' => true, 'onlyCli' => false, 'optional' => true],
- 'AnotherPlugin' => ['bootstrap' => false, 'console' => false, 'middleware' => false, 'routes' => false, 'services' => false, 'events' => false],
- ];
- PHP;
- file_put_contents($this->pluginsConfigPath, $config);
- $result = [
- 'OtherPlugin' => [
- 'isLoaded' => true,
- 'onlyDebug' => true,
- 'onlyCli' => false,
- 'optional' => true,
- 'bootstrap' => true,
- 'console' => true,
- 'middleware' => true,
- 'routes' => true,
- 'services' => true,
- 'events' => true,
- ],
- 'AnotherPlugin' => [
- 'isLoaded' => true,
- 'onlyDebug' => false,
- 'onlyCli' => false,
- 'optional' => false,
- 'bootstrap' => false,
- 'console' => false,
- 'middleware' => false,
- 'routes' => false,
- 'services' => false,
- 'events' => false,
- ],
- ];
- $this->assertSame($result, PluginConfig::getAppConfig());
- }
- public function testUnknownPlugin(): void
- {
- $file = <<<PHP
- <?php
- return [
- 'plugins' => [
- 'TestPlugin' => '/config/path',
- 'OtherPlugin' => '/config/path',
- ]
- ];
- PHP;
- file_put_contents($this->pluginsListPath, $file);
- $config = <<<PHP
- <?php
- return [
- 'UnknownPlugin',
- ];
- PHP;
- file_put_contents($this->pluginsConfigPath, $config);
- $this->assertSame([
- 'TestPlugin' => [
- 'isLoaded' => false,
- ],
- 'OtherPlugin' => [
- 'isLoaded' => false,
- ],
- 'UnknownPlugin' => [
- 'isLoaded' => false,
- 'isUnknown' => true,
- ],
- ], PluginConfig::getAppConfig());
- }
- public function testNoPluginConfig(): void
- {
- $file = <<<PHP
- <?php
- return [
- 'plugins' => [
- 'TestPlugin' => '/config/path',
- 'OtherPlugin' => '/config/path',
- ]
- ];
- PHP;
- file_put_contents($this->pluginsListPath, $file);
- unlink($this->pluginsConfigPath);
- $this->assertSame([
- 'TestPlugin' => [
- 'isLoaded' => false,
- ],
- 'OtherPlugin' => [
- 'isLoaded' => false,
- ],
- ], PluginConfig::getAppConfig());
- }
- public function testGetVersions(): void
- {
- $test = PluginConfig::getVersions(ROOT . DS . 'tests' . DS . 'composer.lock');
- $expected = [
- 'packages' => [
- 'cakephp/chronos' => '3.0.4',
- 'psr/simple-cache' => '3.0.0',
- ],
- 'devPackages' => [
- 'cakephp/cakephp-codesniffer' => '5.1.1',
- 'squizlabs/php_codesniffer' => '3.8.1',
- 'theseer/tokenizer' => '1.2.2',
- ],
- ];
- $this->assertEquals($expected, $test);
- }
- public function testSimpleConfiWithVersions(): void
- {
- $file = <<<PHP
- <?php
- return [
- 'plugins' => [
- 'Chronos' => ROOT . DS . 'vendor' . DS . 'cakephp' . DS . 'chronos',
- 'CodeSniffer' => ROOT . DS . 'vendor' . DS . 'cakephp' . DS . 'cakephp-codesniffer'
- ]
- ];
- PHP;
- file_put_contents($this->pluginsListPath, $file);
- $config = <<<PHP
- <?php
- return [
- 'Chronos',
- 'CodeSniffer'
- ];
- PHP;
- file_put_contents($this->pluginsConfigPath, $config);
- Configure::delete('plugins');
- $pathToRootVendor = ROOT . DS . 'vendor' . DS;
- $result = [
- 'Chronos' => [
- 'isLoaded' => true,
- 'onlyDebug' => false,
- 'onlyCli' => false,
- 'optional' => false,
- 'bootstrap' => true,
- 'console' => true,
- 'middleware' => true,
- 'routes' => true,
- 'services' => true,
- 'events' => true,
- 'packagePath' => $pathToRootVendor . 'cakephp' . DS . 'chronos',
- 'package' => 'cakephp/chronos',
- 'version' => '3.0.4',
- 'isDevPackage' => false,
- ],
- 'CodeSniffer' => [
- 'isLoaded' => true,
- 'onlyDebug' => false,
- 'onlyCli' => false,
- 'optional' => false,
- 'bootstrap' => true,
- 'console' => true,
- 'middleware' => true,
- 'routes' => true,
- 'services' => true,
- 'events' => true,
- 'packagePath' => $pathToRootVendor . 'cakephp' . DS . 'cakephp-codesniffer',
- 'package' => 'cakephp/cakephp-codesniffer',
- 'version' => '5.1.1',
- 'isDevPackage' => true,
- ],
- ];
- $this->assertSame($result, PluginConfig::getAppConfig(ROOT . DS . 'tests' . DS . 'composer.lock'));
- }
- public function testInvalidComposerLock(): void
- {
- $path = ROOT . DS . 'tests' . DS . 'unknown_composer.lock';
- $this->assertSame([], PluginConfig::getAppConfig($path));
- file_put_contents($path, 'invalid-json');
- $this->assertSame([], PluginConfig::getAppConfig($path));
- unlink($path);
- }
- public function testInvalidComposerJson(): void
- {
- $pathToTestPlugin = ROOT . DS . 'tests' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS;
- $file = <<<PHP
- <?php
- return [
- 'plugins' => [
- 'TestPlugin' => ROOT . DS . 'tests' . DS . 'test_app' . DS . 'Plugin' . DS . 'TestPlugin' . DS,
- ]
- ];
- PHP;
- file_put_contents($this->pluginsListPath, $file);
- $config = <<<PHP
- <?php
- return [
- 'TestPlugin',
- ];
- PHP;
- file_put_contents($this->pluginsConfigPath, $config);
- file_put_contents($pathToTestPlugin . 'composer.json', 'invalid-json');
- $this->assertSame([
- 'TestPlugin' => [
- 'isLoaded' => true,
- 'onlyDebug' => false,
- 'onlyCli' => false,
- 'optional' => false,
- 'bootstrap' => true,
- 'console' => true,
- 'middleware' => true,
- 'routes' => true,
- 'services' => true,
- 'events' => true,
- ],
- ], PluginConfig::getAppConfig());
- unlink($pathToTestPlugin . 'composer.json');
- }
- }
|