Skip to content

Commit

Permalink
Autoload for tests. Configuration exception.
Browse files Browse the repository at this point in the history
  • Loading branch information
niksamokhvalov committed Mar 21, 2016
1 parent 7efbc4a commit 6695941
Show file tree
Hide file tree
Showing 4 changed files with 82 additions and 69 deletions.
67 changes: 64 additions & 3 deletions src/Application/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use Bitrix\Main\ModuleManager;
use Notamedia\ConsoleJedi\Agent\Command\OnCronCommand;
use Notamedia\ConsoleJedi\Agent\Command\RunCommand;
use Notamedia\ConsoleJedi\Application\Exception\ConfigurationException;
use Notamedia\ConsoleJedi\Cache\Command\ClearCommand;
use Notamedia\ConsoleJedi\Environment\Command\InitCommand;
use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -209,7 +210,7 @@ protected function getModulesCommands()
*
* @return bool
*
* @throws \Exception
* @throws ConfigurationException
*/
public function loadConfiguration($path = self::CONFIG_DEFAULT_FILE)
{
Expand All @@ -222,7 +223,7 @@ public function loadConfiguration($path = self::CONFIG_DEFAULT_FILE)

if (!is_array($this->configuration))
{
throw new \Exception('Configuration file ' . $path . ' must return an array');
throw new ConfigurationException('Configuration file ' . $path . ' must return an array');
}

$filesystem = new Filesystem();
Expand Down Expand Up @@ -261,7 +262,11 @@ public function getConfiguration()
*/
public function initializeBitrix()
{
if (!$this->checkBitrix())
if ($this->bitrixStatus === static::BITRIX_STATUS_COMPLETE)
{
return static::BITRIX_STATUS_COMPLETE;
}
elseif (!$this->checkBitrix())
{
return static::BITRIX_STATUS_UNAVAILABLE;
}
Expand Down Expand Up @@ -324,6 +329,62 @@ public function isBitrixLoaded()
return $this->bitrixStatus === static::BITRIX_STATUS_COMPLETE;
}

public function autoloadTests()
{
if ($this->getConfiguration() === null)
{
$this->loadConfiguration();
}

$this->initializeBitrix();

spl_autoload_register(function ($className) {
$file = ltrim($className, "\\");
$file = strtr($file, Loader::ALPHA_UPPER, Loader::ALPHA_LOWER);
$file = str_replace('\\', '/', $file);

if (substr($file, -5) === 'table')
{
$file = substr($file, 0, -5);
}

$arFile = explode('/', $file);

if (preg_match("#[^\\\\/a-zA-Z0-9_]#", $file))
{
return false;
}
elseif ($arFile[0] === 'bitrix')
{
return false;
}
elseif ($arFile[2] !== 'tests')
{
return false;
}

$module = array_shift($arFile) . '.' . array_shift($arFile);

if (!Loader::includeModule($module))
{
return false;
}

$file = $module . '/' . implode('/', $arFile) . '.php';
$bitrixPath = \Bitrix\Main\Application::getDocumentRoot() . '/' . Loader::BITRIX_HOLDER . '/modules/' . $file;
$localPath = \Bitrix\Main\Application::getDocumentRoot() . '/' . Loader::LOCAL_HOLDER . '/modules/' . $file;

if (file_exists($bitrixPath))
{
include_once $bitrixPath;
}
elseif (file_exists($localPath))

This comment has been minimized.

Copy link
@nook-ru

nook-ru Mar 21, 2016

Contributor

Не другой порядок должен быть? Битрикс сначала ищет в папке local, только потом в bitrix: getLocalPath

This comment has been minimized.

Copy link
@niksamokhvalov

niksamokhvalov Mar 22, 2016

Author Member

Ага, спасибо, надо поменять на getLocalPath().

{
include_once $localPath;
}
});
}

/**
* Gets root directory from which are running Console Jedi.
*
Expand Down
17 changes: 17 additions & 0 deletions src/Application/Exception/ConfigurationException.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?php
/**
* For the full copyright and license information, please view the LICENSE.md
* file that was distributed with this source code.
*/

namespace Notamedia\ConsoleJedi\Application\Exception;

/**
* Configuration exception.
*
* @author Nik Samokhvalov <[email protected]>
*/
class ConfigurationException extends \ErrorException
{

}
61 changes: 0 additions & 61 deletions src/Loader.php

This file was deleted.

6 changes: 1 addition & 5 deletions tests/bootstrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,4 @@
require_once __DIR__ . '/../../../autoload.php';

$app = new \Notamedia\ConsoleJedi\Application\Application();
$app->loadConfiguration();
$app->initializeBitrix();

$loader = new \Notamedia\ConsoleJedi\Loader();
$loader->loadTests();
$app->autoloadTests();

0 comments on commit 6695941

Please sign in to comment.