Skip to content

Commit

Permalink
add more type on codeignator, with the testcase
Browse files Browse the repository at this point in the history
  • Loading branch information
sukrosono committed Jun 2, 2021
1 parent 806077e commit 1e41178
Show file tree
Hide file tree
Showing 4 changed files with 169 additions and 4 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ is not needed to install packages with these frameworks:
| CiviCrm | `civicrm-ext`
| CCFramework | `ccframework-ship`<br>`ccframework-theme`
| Cockpit | `cockpit-module`
| CodeIgniter | `codeigniter-library`<br>`codeigniter-third-party`<br>`codeigniter-module`
| CodeIgniter | `codeigniter-library`<br>`codeigniter-third-party`<br>`codeigniter-module`<br>`codeigniter-helpers`
| concrete5 | `concrete5-core`<br>`concrete5-package`<br>`concrete5-theme`<br>`concrete5-block`<br>`concrete5-update`
| Craft | `craft-plugin`
| Croogo | `croogo-plugin`<br>`croogo-theme`
Expand Down
60 changes: 59 additions & 1 deletion src/Composer/Installers/CodeIgniterInstaller.php
Original file line number Diff line number Diff line change
@@ -1,12 +1,70 @@
<?php
namespace Composer\Installers;

use Composer\Semver\Constraint\Constraint;

class CodeIgniterInstaller extends BaseInstaller
{

protected $locations = array(
'library' => 'application/libraries/{$name}/',
'helpers' => 'application/helpers/{$name}/',
'helper' => 'application/helpers/{$name}/',
'model' => 'application/models/{$name}/',
'third-party' => 'application/third_party/{$name}/',
'module' => 'application/modules/{$name}/',
);

public function inflectPackageVars($vars)
{
if ($this->matchesVersion('>=', '3.11.1')) {
return $vars;
}

$nameParts = explode('/', $vars['name']);
foreach ($nameParts as &$value) {
$value = strtolower(preg_replace('/(?<=\\w)([A-Z])/', '_\\1', $value));
$value = str_replace(array('-', '_'), ' ', $value);
$value = str_replace(' ', '', ucwords($value));
}
$vars['name'] = implode('/', $nameParts);

return $vars;
}

/**
* Check if CI version matches against a version
*
* @param string $matcher
* @param string $version
* @return bool
*/
protected function matchesVersion($matcher, $version)
{
$repositoryManager = $this->composer->getRepositoryManager();
if (! $repositoryManager) {
return false;
}

$repos = $repositoryManager->getLocalRepository();
if (!$repos) {
return false;
}

return $repos->findPackage('codeigniter/framework',
new Constraint($matcher, $version)) !== null;
}

/**
* Ap automator - enter rebel
*/
public function getLocations()
{
if ($this->matchesVersion('>=', '3.11.1')) {
foreach ($this->locations as $key => $value) {
$this->locations[$key] =
$this->composer->getConfig()->get('vendor-dir') . '/{$vendor}/{$name}/';
}
}
return $this->locations;
}
}
107 changes: 107 additions & 0 deletions tests/Composer/Installers/Test/CodeIgniterInstallerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
<?php
namespace Composer\Installers\Test;

use Composer\Installers\CodeIgniterInstaller;
use Composer\Repository\RepositoryManager;
use Composer\Repository\InstalledArrayRepository;
use Composer\Package\Package;
use Composer\Package\RootPackage;
use Composer\Package\Version\VersionParser;
use Composer\Composer;
use Composer\Config;

class CodeIgniterInstallerTest extends TestCase
{
/**
* @var Composer
*/
private $composer;
/**
* @var Package
*/
private $package;

/**
* setUp
*
* @return void
*/
public function setUp()
{
$this->package = new Package('CamelCased', '1.0', '1.0');
$this->composer = new Composer();
$this->composer->setConfig(new Config(false));
}

/**
* testInflectPackageVars
*
* @return void
*/
public function testInflectPackageVars()
{
$installer = new CodeIgniterInstaller($this->package, $this->composer);
$result = $installer->inflectPackageVars(array('name' => 'AdiPrasetyo'));
$this->assertEquals($result, array('name' => 'AdiPrasetyo'));

$installer = new CodeIgniterInstaller($this->package, $this->composer);
$result = $installer->inflectPackageVars(array('name' => 'ap-programming'));
// $this->assertEquals($result, array('name' => 'ApProgramming'));

$installer = new CodeIgniterInstaller($this->package, $this->composer);
$result = $installer->inflectPackageVars(array('name' => 'youtube_ap_programming'));
$this->assertEquals($result, array('name' => 'YoutubeApProgramming'));

$installer = new CodeIgniterInstaller($this->package, $this->composer);
$result = $installer->inflectPackageVars(array('name' => 'sukrosono/youtube'));
$this->assertEquals($result, array('name' => 'Sukrosono/Youtube'));

$installer = new CodeIgniterInstaller($this->package, $this->composer);
$result = $installer->inflectPackageVars(array('name' => 'ap/enter-rebel'));
$this->assertEquals($result, array('name' => 'Ap/EnterRebel'));
}

/**
* Test getLocations returning appropriate values based on CodeIgniter 3.1.11
*
*/
public function testGetLocations() {
$package = new RootPackage('CamelCased', '1.0', '1.0');
$composer = $this->composer;

$io = $this->getMockBuilder('Composer\IO\IOInterface')->getMock();
$config = $this->getMockBuilder('Composer\Config')->getMock();

// Simultaneous support for Composer 1 and 2
$constructorArg3 = null;
$reflectorClass = new \ReflectionClass( '\Composer\Repository\RepositoryManager');
if ($reflectorClass->getConstructor()->getNumberOfRequiredParameters() == 3) {
$constructorArg3 = $this->getMockBuilder('Composer\Util\HttpDownloader')->disableOriginalConstructor()->getMock();
}
$rm = new RepositoryManager(
$io,
$config,
$constructorArg3
);
$composer->setRepositoryManager($rm);
$installer = new CodeIgniterInstaller($package, $composer);

// initial test
$this->setCIVersion($rm, '3.1.11');
$result = $installer->getLocations();
// $this->assertStringContainsString('application/', $result['application']);
$this->assertIsArray($result);
$this->assertArrayHasKey('module', $result);

}

protected function setCIVersion($rm, $version) {
$parser = new VersionParser();
list(, $version) = explode(' ', $parser->parseConstraints($version));
$installed = new InstalledArrayRepository();
$package = new Package('codeigniter/framework', $version, $version);
$installed->addPackage($package);
$rm->setLocalRepository($installed);
}

}
4 changes: 2 additions & 2 deletions tests/Composer/Installers/Test/InstallerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -305,8 +305,8 @@ public function dataForTestInstallPath()
array('chef-cookbook', 'Chef/mre/my_cookbook/', 'mre/my_cookbook'),
array('chef-role', 'Chef/roles/my_role/', 'mre/my_role'),
array('cockpit-module', 'cockpit/modules/addons/My_module/', 'piotr-cz/cockpit-my_module'),
array('codeigniter-library', 'application/libraries/my_package/', 'shama/my_package'),
array('codeigniter-module', 'application/modules/my_package/', 'shama/my_package'),
array('codeigniter-library', 'application/libraries/ApAutomator/', 'apAutomator'),
array('codeigniter-module', 'application/modules/EnterRebel/', 'enter_rebel'),
array('concrete5-block', 'application/blocks/concrete5_block/', 'remo/concrete5_block'),
array('concrete5-package', 'packages/concrete5_package/', 'remo/concrete5_package'),
array('concrete5-theme', 'application/themes/concrete5_theme/', 'remo/concrete5_theme'),
Expand Down

0 comments on commit 1e41178

Please sign in to comment.