Skip to content

Commit f9982c9

Browse files
committed
Update commands and tests
1 parent d0b29be commit f9982c9

File tree

8 files changed

+155
-50
lines changed

8 files changed

+155
-50
lines changed

.travis.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ matrix:
1919
env: 'COMPOSER_FLAGS="--prefer-stable --prefer-lowest"'
2020

2121
before_script:
22+
- npm install -g bower
2223
- travis_retry composer self-update
2324
- travis_retry composer update ${COMPOSER_FLAGS} --no-interaction --prefer-source
2425

src/Commands/InstallDoctrineCommand.php

Lines changed: 1 addition & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -49,27 +49,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
4949
file_put_contents(APPPATH . 'libraries/Doctrine.php', $library);
5050
fclose($file);
5151

52-
$autoload = file_get_contents(APPPATH . 'config/autoload.php');
53-
$lines = explode(PHP_EOL, $autoload);
54-
55-
$pattern = '/\$autoload\[\'libraries\'\] = array\((.*?)\)/';
56-
57-
preg_match_all($pattern, $lines[60], $match);
58-
59-
$libraries = explode(', ', end($match[1]));
60-
61-
if ( ! in_array('\'doctrine\'', $libraries)) {
62-
array_push($libraries, '\'doctrine\'');
63-
64-
$libraries = array_filter($libraries);
65-
66-
$pattern = '/\$autoload\[\'libraries\'\] = array\([^)]*\);/';
67-
$replacement = '$autoload[\'libraries\'] = array(' . implode(', ', $libraries) . ');';
68-
69-
$lines[60] = preg_replace($pattern, $replacement, $lines[60]);
70-
71-
file_put_contents(APPPATH . 'config/autoload.php', implode(PHP_EOL, $lines));
72-
}
52+
$this->addLibrary('doctrine');
7353

7454
if ( ! is_dir(APPPATH . 'models/proxies')) {
7555
mkdir(APPPATH . 'models/proxies');

src/Commands/InstallWildfireCommand.php

Lines changed: 4 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,34 +26,13 @@ class InstallWildfireCommand extends InstallCommand
2626
/**
2727
* Executes the command.
2828
*
29-
* @param \Symfony\Component\Console\Input\InputInterface $input
30-
* @param \Symfony\Component\Console\Output\OutputInterface $output
29+
* @param \Symfony\Component\Console\Input\InputInterface $input
30+
* @param \Symfony\Component\Console\Output\OutputInterface $output
3131
* @return object|\Symfony\Component\Console\Output\OutputInterface
3232
*/
3333
protected function execute(InputInterface $input, OutputInterface $output)
3434
{
35-
// Adds Wildfire.php to the "libraries" directory
36-
$autoload = file_get_contents(APPPATH . 'config/autoload.php');
37-
$lines = explode(PHP_EOL, $autoload);
38-
39-
$pattern = '/\$autoload\[\'libraries\'\] = array\((.*?)\)/';
40-
41-
preg_match_all($pattern, $lines[60], $match);
42-
43-
$libraries = explode(', ', end($match[1]));
44-
45-
if ( ! in_array('\'wildfire\'', $libraries)) {
46-
array_push($libraries, '\'wildfire\'');
47-
48-
$libraries = array_filter($libraries);
49-
50-
$pattern = '/\$autoload\[\'libraries\'\] = array\([^)]*\);/';
51-
$replacement = '$autoload[\'libraries\'] = array(' . implode(', ', $libraries) . ');';
52-
53-
$lines[60] = preg_replace($pattern, $replacement, $lines[60]);
54-
55-
file_put_contents(APPPATH . 'config/autoload.php', implode(PHP_EOL, $lines));
56-
}
35+
$this->addLibrary('wildfire');
5736

5837
$file = fopen(APPPATH . 'libraries/Wildfire.php', 'wb');
5938
$wildfire = $this->renderer->render('Libraries/Wildfire.template');
@@ -65,6 +44,6 @@ protected function execute(InputInterface $input, OutputInterface $output)
6544

6645
$message = 'Wildfire is now installed successfully!';
6746

68-
return $output->writeln('<info>'.$message.'</info>');
47+
return $output->writeln('<info>' . $message . '</info>');
6948
}
7049
}

src/Common/Commands/InstallCommand.php

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,4 +49,35 @@ protected function configure()
4949
->setName('install:' . $this->library)
5050
->setDescription('Installs ' . ucfirst($this->library));
5151
}
52+
53+
/**
54+
* Adds the specified library in the autoload.php.
55+
*
56+
* @param string $library
57+
* @return void
58+
*/
59+
protected function addLibrary($library)
60+
{
61+
$autoload = file_get_contents(APPPATH . 'config/autoload.php');
62+
$lines = explode(PHP_EOL, $autoload);
63+
64+
$pattern = '/\$autoload\[\'libraries\'\] = array\((.*?)\)/';
65+
66+
preg_match_all($pattern, $lines[60], $match);
67+
68+
$libraries = explode(', ', end($match[1]));
69+
70+
if ( ! in_array('\'' . $library . '\'', $libraries)) {
71+
array_push($libraries, '\'' . $library . '\'');
72+
73+
$libraries = array_filter($libraries);
74+
75+
$pattern = '/\$autoload\[\'libraries\'\] = array\([^)]*\);/';
76+
$replacement = '$autoload[\'libraries\'] = array(' . implode(', ', $libraries) . ');';
77+
78+
$lines[60] = preg_replace($pattern, $replacement, $lines[60]);
79+
80+
file_put_contents(APPPATH . 'config/autoload.php', implode(PHP_EOL, $lines));
81+
}
82+
}
5283
}

src/Common/Tools.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ public static function ignite()
112112

113113
file_put_contents('.htaccess', $contents);
114114
chmod('.htaccess', 0777);
115+
fclose($file);
115116
}
116117

117118
// Creates a configuration for the Pagination library.
@@ -124,6 +125,7 @@ public static function ignite()
124125

125126
file_put_contents($pagination, $contents);
126127
chmod($pagination, 0664);
128+
fclose($file);
127129
}
128130
}
129131

src/Validator/ViewValidator.php

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,11 +37,10 @@ public function __construct($name)
3737
*/
3838
public function fails()
3939
{
40-
$filePath = APPPATH.'views/'.$this->name;
40+
$filePath = APPPATH . 'views/' . $this->name;
4141

4242
if ( ! @mkdir($filePath, 0775, true)) {
43-
$this->message = 'The "'.$this->name.
44-
'" views folder already exists!';
43+
$this->message = 'The "' . $this->name . '" views folder already exists!';
4544

4645
return true;
4746
}

tests/CombustorTest.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ class CombustorTest extends PHPUnit_Framework_TestCase
1818
protected $appPath;
1919

2020
/**
21-
* @var [type]
21+
* @var array
2222
*/
2323
protected $commands = [
2424
'Rougin\Combustor\Commands\CreateControllerCommand',
Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
<?php
2+
3+
namespace Rougin\Combustor;
4+
5+
use Symfony\Component\Console\Application;
6+
use Symfony\Component\Console\Tester\CommandTester;
7+
8+
use Rougin\Combustor\Fixture\CommandBuilder;
9+
use Rougin\Combustor\Fixture\CodeIgniterHelper;
10+
11+
use PHPUnit_Framework_TestCase;
12+
13+
class CreateScaffoldCommandTest extends PHPUnit_Framework_TestCase
14+
{
15+
/**
16+
* @var string
17+
*/
18+
protected $appPath;
19+
20+
/**
21+
* @var array
22+
*/
23+
protected $commands = [
24+
'Rougin\Combustor\Commands\CreateControllerCommand',
25+
'Rougin\Combustor\Commands\CreateLayoutCommand',
26+
'Rougin\Combustor\Commands\CreateModelCommand',
27+
'Rougin\Combustor\Commands\CreateScaffoldCommand',
28+
'Rougin\Combustor\Commands\CreateViewCommand',
29+
'Rougin\Combustor\Commands\InstallDoctrineCommand',
30+
'Rougin\Combustor\Commands\InstallWildfireCommand',
31+
'Rougin\Combustor\Commands\RemoveDoctrineCommand',
32+
'Rougin\Combustor\Commands\RemoveWildfireCommand',
33+
];
34+
35+
/**
36+
* Sets up the command and the application path.
37+
*
38+
* @return void
39+
*/
40+
public function setUp()
41+
{
42+
$this->appPath = __DIR__ . '/../TestApp/application';
43+
}
44+
45+
/**
46+
* Tests if the initial commands exists.
47+
*
48+
* @return void
49+
*/
50+
public function testCommandsExist()
51+
{
52+
CodeIgniterHelper::setDefaults($this->appPath);
53+
54+
copy(
55+
__DIR__ . '/../../src/Templates/Libraries/Wildfire.template',
56+
$this->appPath . '/libraries/Wildfire.php'
57+
);
58+
59+
mkdir($this->appPath . '/views/layout');
60+
61+
copy(
62+
__DIR__ . '/../../src/Templates/Views/Layout/header.template',
63+
$this->appPath . '/views/layout/header.php'
64+
);
65+
66+
copy(
67+
__DIR__ . '/../../src/Templates/Views/Layout/footer.template',
68+
$this->appPath . '/views/layout/footer.php'
69+
);
70+
71+
$application = $this->getApplication();
72+
73+
$command = $application->find('create:scaffold');
74+
$commandTester = new CommandTester($command);
75+
$commandTester->execute([
76+
'name' => 'users',
77+
'--bootstrap' => true
78+
]);
79+
80+
$this->assertFileExists($this->appPath . '/controllers/Users.php');
81+
$this->assertFileExists($this->appPath . '/models/Users.php');
82+
83+
$create = $file = $this->appPath . '/views/users/create.php';
84+
$edit = $file = $this->appPath . '/views/users/edit.php';
85+
$index = $file = $this->appPath . '/views/users/index.php';
86+
$show = $file = $this->appPath . '/views/users/show.php';
87+
88+
$this->assertFileExists($create);
89+
$this->assertFileExists($edit);
90+
$this->assertFileExists($index);
91+
$this->assertFileExists($show);
92+
93+
CodeIgniterHelper::setDefaults($this->appPath);
94+
}
95+
96+
/**
97+
* Gets the application with the loaded classes.
98+
*
99+
* @return \Symfony\Component\Console\Application
100+
*/
101+
protected function getApplication()
102+
{
103+
$application = new Application;
104+
105+
foreach ($this->commands as $commandName) {
106+
$command = CommandBuilder::create($commandName);
107+
108+
$application->add($command);
109+
}
110+
111+
return $application;
112+
}
113+
}

0 commit comments

Comments
 (0)