diff --git a/extensions/install.php b/extensions/install.php index b990c39..68eb95e 100644 --- a/extensions/install.php +++ b/extensions/install.php @@ -9,6 +9,8 @@ defined('_JEXEC') || die; +use Joomla\CMS\Factory; + /** * Package installer * @@ -56,7 +58,7 @@ private function enablePlugins($parent) $extName = (string) $node->attributes()->id; $extGroup = (string) $node->attributes()->group; - $db = JFactory::getDbo(); + $db = Factory::getDbo(); $query = $db->getQuery(true); $query->update($db->quoteName("#__extensions")); $query->set("enabled=1"); @@ -111,7 +113,7 @@ protected function loadManifest($parent) // Package manifest found if (file_exists($manifestFile)) { - $this->manifest = JFactory::getXML($manifestFile); + $this->manifest = simplexml_load_file($manifestFile); return; } diff --git a/extensions/libraries/twig/src/Environment.php b/extensions/libraries/twig/src/Environment.php index 1a87d57..8f58dac 100644 --- a/extensions/libraries/twig/src/Environment.php +++ b/extensions/libraries/twig/src/Environment.php @@ -10,6 +10,8 @@ defined('_JEXEC') || die; +use Joomla\CMS\Application\CMSApplication; +use Joomla\CMS\Factory; use Joomla\CMS\Plugin\PluginHelper; use Twig\Loader\LoaderInterface; use Twig\Environment as BaseEnvironment; @@ -21,6 +23,14 @@ */ final class Environment extends BaseEnvironment { + /** + * Application where enviroment is loaded. + * + * @var CMSApplication + * @since __DEPLOY_VERSION__ + */ + private $app; + /** * Plugins connected to the events triggered by this class. * @@ -35,9 +45,12 @@ final class Environment extends BaseEnvironment * * @param LoaderInterface $loader Loader instance * @param array $options An array of options + * @param CMSApplication $app CMSApplication | null active application */ - public function __construct(LoaderInterface $loader, $options = []) + public function __construct(LoaderInterface $loader, $options = [], CMSApplication $app = null) { + $this->app = $app ?: $this->activeApplication(); + $this->trigger('onTwigBeforeLoad', [&$loader, &$options]); parent::__construct($loader, $options); @@ -45,6 +58,18 @@ public function __construct(LoaderInterface $loader, $options = []) $this->trigger('onTwigAfterLoad', [$options]); } + /** + * Get the active Joomla application. + * + * @return CMSApplication + * + * @since __DEPLOY_VERSION__ + */ + private function activeApplication() + { + return Factory::getApplication(); + } + /** * Import available plugins. * @@ -68,13 +93,11 @@ private function importPlugins() */ public function trigger($event, $params = []) { - $dispatcher = \JEventDispatcher::getInstance(); - $this->importPlugins(); // Always send enviroment as first param array_unshift($params, $this); - return $dispatcher->trigger($event, $params); + return $this->app->triggerEvent($event, $params); } } diff --git a/tests/Unit/EnvironmentTest.php b/tests/Unit/EnvironmentTest.php index 805bf80..7aa6ce2 100644 --- a/tests/Unit/EnvironmentTest.php +++ b/tests/Unit/EnvironmentTest.php @@ -11,6 +11,7 @@ use Twig\Loader\LoaderInterface; use Phproberto\Joomla\Twig\Environment; +use Joomla\CMS\Application\CMSApplication; /** * Environment tests. @@ -45,10 +46,16 @@ protected function setUp() { $this->saveFactoryState(); - \JFactory::$session = $this->getMockSession(); + \JFactory::$config = $this->getMockConfig(); + \JFactory::$session = $this->getMockSession(); $this->dispatcher = new \JEventDispatcher; \TestReflection::setValue($this->dispatcher, 'instance', $this->dispatcher); + + $app = $this->getMockForAbstractClass(CMSApplication::class); + $app->loadDispatcher($this->dispatcher); + + \JFactory::$application = $app; } /** @@ -77,8 +84,8 @@ public function testConstructorTriggersEvents() { $this->calledEvents = []; - $this->dispatcher->register('onTwigBeforeLoad', [$this, 'onTwigBeforeLoad']); - $this->dispatcher->register('onTwigAfterLoad', [$this, 'onTwigAfterLoad']); + \JFactory::$application->registerEvent('onTwigBeforeLoad', [$this, 'onTwigBeforeLoad']); + \JFactory::$application->registerEvent('onTwigAfterLoad', [$this, 'onTwigAfterLoad']); $loader = new \Twig_Loader_Array; $options = ['sample' => 'option']; diff --git a/tests/Unit/Plugin/BasePluginTest.php b/tests/Unit/Plugin/BasePluginTest.php index 60124b5..39813a4 100644 --- a/tests/Unit/Plugin/BasePluginTest.php +++ b/tests/Unit/Plugin/BasePluginTest.php @@ -19,6 +19,32 @@ */ class BasePluginTest extends \TestCase { + /** + * Sets up the fixture, for example, opens a network connection. + * This method is called before a test is executed. + * + * @return void + */ + protected function setUp() + { + $this->saveFactoryState(); + + \JFactory::$config = $this->getMockConfig(); + \JFactory::$application = $this->getMockCmsApp(); + } + + /** + * Tears down the fixture, for example, closes a network connection. + * This method is called after a test is executed. + * + * @return void + */ + protected function tearDown() + { + $this->restoreFactoryState(); + parent::tearDown(); + } + /** * pluginPath returns correct path. *