Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions tests/Unit/autoload_psr4.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
<?php
/**
* Prepares a minimalist PS4 namespace map for unit testing.
*
* @package Joomla.UnitTest
*
* @copyright (C) 2022 Open Source Matters, Inc. <https://www.joomla.org>
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/
defined('_JEXEC') or die;

return [
'Joomla\\Component\\Finder\\Administrator\\' => [JPATH_ADMINISTRATOR . '/components/com_finder/src'],
];
43 changes: 15 additions & 28 deletions tests/Unit/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,35 +95,22 @@
define('JDEBUG', false);
}

// Import the library loader if necessary.
if (!class_exists('JLoader'))
{
require_once JPATH_PLATFORM . '/loader.php';
/**
* Load our cached Namespacemap file for Unit tests.
*
* We need to do this so that unit tests can find the correct files and autoload Joomla CMS classes as needed.
*/
$map = require_once 'autoload_psr4.php';
$loader = include JPATH_LIBRARIES . '/vendor/autoload.php';

// If JLoader still does not exist panic.
if (!class_exists('JLoader'))
{
throw new RuntimeException('Joomla Platform not loaded.');
}
foreach ($map as $namespace => $path)
{
$loader->setPsr4($namespace, $path);
}

// Setup the autoloaders.
JLoader::setup();

// Create the Composer autoloader
/** @var \Composer\Autoload\ClassLoader $loader */
$loader = require JPATH_LIBRARIES . '/vendor/autoload.php';

// We need to pull our decorated class loader into memory before unregistering Composer's loader
class_exists('\\Joomla\\CMS\\Autoload\\ClassLoader');

$loader->unregister();

// Decorate Composer autoloader
spl_autoload_register([new \Joomla\CMS\Autoload\ClassLoader($loader), 'loadClass'], true, true);

// Register the class aliases for Framework classes that have replaced their Platform equivalents
require_once JPATH_LIBRARIES . '/classmap.php';
// Bootstrap Joomla, to keep our test stack as close to a running app as possible.
require_once JPATH_LIBRARIES . '/bootstrap.php';

// Define the Joomla version if not already defined.
defined('JVERSION') or define('JVERSION', (new \Joomla\CMS\Version)->getShortVersion());
// Remove Joomla's internal Error Handler x2 so that PHPUnit handles Warnings/Notices correctly.
restore_error_handler();
restore_error_handler();