Skip to content

Commit

Permalink
Merge pull request #62 from alias-mac/unit-test-coverage
Browse files Browse the repository at this point in the history
Increase unit test coverage
  • Loading branch information
jcsmorais committed Feb 3, 2014
2 parents 0caaa5c + db8a9bd commit 643a7e5
Show file tree
Hide file tree
Showing 5 changed files with 181 additions and 43 deletions.
11 changes: 2 additions & 9 deletions src/Console/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -118,11 +118,13 @@ public function doRun(InputInterface $input, OutputInterface $output)
$this->registerCommands();

if (true === $input->hasParameterOption(array('--shell', '-s'))) {
// @codeCoverageIgnoreStart
$shell = new Shell($this);
$shell->setProcessIsolation($input->hasParameterOption(array('--process-isolation')));
$shell->run();

return 0;
// @codeCoverageIgnoreEnd
}

$result = parent::doRun($input, $output);
Expand Down Expand Up @@ -192,15 +194,6 @@ protected function registerCommands()

$searchPath = array_filter($searchPath, 'is_dir');

if (empty($searchPath)) {
throw new \RuntimeException(
sprintf(
'No search path for commands available for run level "%s".',
$this->kernel->getBootedLevel()
)
);
}

$finder = new Finder();
$finder->files()->name('*Command.php')->in($searchPath);

Expand Down
40 changes: 17 additions & 23 deletions src/Console/Kernel.php
Original file line number Diff line number Diff line change
Expand Up @@ -325,17 +325,6 @@ public function getContainer()
return $this->container;
}

/**
* Used internally.
*/
public function setClassCache(array $classes)
{
file_put_contents(
$this->getCacheDir() . '/classes.map',
sprintf('<?php return %s;', var_export($classes, true))
);
}

/**
* {@inheritdoc}
*/
Expand Down Expand Up @@ -469,12 +458,8 @@ protected function initializeContainer()
$fresh = true;
if (!$cache->isFresh()) {
$container = $this->buildContainer();
$this->dumpContainer(
$cache,
$container,
$class,
$this->getContainerBaseClass()
);
$container->compile();
$this->dumpContainer($cache, $container, $class, $this->getContainerBaseClass());

$fresh = false;
}
Expand Down Expand Up @@ -575,7 +560,6 @@ protected function buildContainer()
$container = $this->getContainerBuilder();

$container->addObjectResource($this);
$container->compile();

return $container;
}
Expand Down Expand Up @@ -632,27 +616,37 @@ protected function dumpContainer(
* @return string
* The PHP string with the comments removed.
*/
protected static function stripComments($source)
public static function stripComments($source)
{
if (!function_exists('token_get_all')) {
return $source;
}

$rawChunk = '';
$output = '';
foreach (token_get_all($source) as $token) {
$tokens = token_get_all($source);
for (reset($tokens); false !== $token = current($tokens); next($tokens)) {
if (is_string($token)) {
$output .= $token;
$rawChunk .= $token;
} elseif (T_START_HEREDOC === $token[0]) {
$output .= preg_replace(array('/\s+$/Sm', '/\n+/S'), "\n", $rawChunk).$token[1];
do {
$token = next($tokens);
$output .= $token[1];
} while ($token[0] !== T_END_HEREDOC);
$rawChunk = '';
} elseif (!in_array($token[0], array(T_COMMENT, T_DOC_COMMENT))) {
$output .= $token[1];
$rawChunk .= $token[1];
}
}

// replace multiple new lines with a single newline
$output = preg_replace(array('/\s+$/Sm', '/\n+/S'), "\n", $output);
$output .= preg_replace(array('/\s+$/Sm', '/\n+/S'), "\n", $rawChunk);

return $output;
}


/**
* Provide a way to serialize a kernel. Currently save the debug status.
*
Expand Down
2 changes: 2 additions & 0 deletions src/Console/Shell.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
* An Insulin Shell wraps an Application to add shell capabilities to it.
*
* This extends from Symfony's Shell default abilities.
*
* @codeCoverageIgnore because we are only applying the logo in the header.
*/
class Shell extends BaseShell
{
Expand Down
39 changes: 33 additions & 6 deletions src/Console/Tests/ApplicationTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,20 +52,47 @@ public function testCoreCommandsAvailableWithBootSugarConfiguration()
$this->assertRegExp('/sugar:version/', $insulinTester->getDisplay());
}

/**
* Confirm that all the core commands with bootstrap level
* `BOOT_SUGAR_ROOT` will be available by default.
*/
public function testDebugOutput()
{
$kernel = $this->getKernel(Kernel::BOOT_INSULIN, null, true);

$insulin = new Application($kernel);
$insulin->setAutoExit(false);

$insulinTester = new ApplicationTester($insulin);
$insulinTester->run(array('command' => 'list'));

$this->assertRegExp('/Memory usage: (.*)MB \(peak: (.*)MB\), time: (.*)s/', $insulinTester->getDisplay());
}

/**
* Gets a mock kernel to test the Insulin Application.
*
* @param $level
* @param integer $level
* The level of the boot reached.
* @param array $methods
* Additional methods to mock (besides the required to boot to the given
* level).
* @param boolean $debug
* Set it to `true` to run the Kernel in debug mode.
*
* @return \Insulin\Console\KernelInterface
*/
private function getKernel($level)
private function getKernel($level, $methods = array(), $debug = false)
{
$kernel = $this->getMock(
'Insulin\Console\Kernel',
array('getBootedLevel', 'getRootDir')
);
$mockMethods = array('getBootedLevel', 'getRootDir');
if (!empty($methods)) {
$mockMethods = array_merge($mockMethods, $methods);
}
$kernel = $this->getMockBuilder('Insulin\Console\Kernel')
->setMethods($mockMethods)
->setConstructorArgs(array($debug))
->getMock();

$kernel->expects($this->any())->method('getBootedLevel')->will(
$this->returnValue($level)
);
Expand Down
132 changes: 127 additions & 5 deletions src/Console/Tests/KernelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public function testPerformanceBoot()
array('getBootstrapLevels', 'bootTo')
);
$kernel->expects($this->once())->method('getBootstrapLevels')->will(
$this->returnValue(array('1'))
$this->returnValue(array(1))
);

/* @var $kernel \Insulin\Console\KernelInterface */
Expand All @@ -93,10 +93,10 @@ public function testPerformanceBoot()
*/
public function testBootFailure()
{
$kernel = $this->getMock(
'Insulin\Console\Kernel',
array('getBootstrapLevels', 'bootTo')
);
$kernel = $this->getMockBuilder('Insulin\Console\Kernel')
->setMethods(array('getBootstrapLevels', 'bootTo'))
->setConstructorArgs(array(true))
->getMock();

$kernel->expects($this->once())->method('getBootstrapLevels')->will(
$this->returnValue(array(1))
Expand Down Expand Up @@ -125,4 +125,126 @@ public function testShutdownsWhenBooted()
$kernel->shutdown();
$this->assertNull($kernel->getContainer());
}

/**
* Confirm that we can't shutdown the Kernel if there is no valid boot.
*/
public function testShutdownsWhenNotBooted()
{
$kernel = $this->getMock(
'Insulin\Console\Kernel',
array('isBooted')
);
$kernel->expects($this->once())->method('isBooted')->will(
$this->returnValue(false)
);

/* @var $kernel \Insulin\Console\Kernel */
$kernel->shutdown();
}

/**
* Confirm that we can get the booted level if kernel is booted.
*/
public function testGetBootedLevel()
{
$kernel = $this->getMock(
'Insulin\Console\Kernel',
array('getBootstrapLevels', 'bootTo')
);

$kernel->expects($this->once())->method('getBootstrapLevels')->will(
$this->returnValue(array(1, 2))
);

$kernel->boot();
$this->assertEquals(2, $kernel->getBootedLevel());

}

/**
* Confirm that the comments are stripped from PHP files correctly.
*/
public function testStripComments()
{
if (!function_exists('token_get_all')) {
$this->markTestSkipped('The function token_get_all() is not available.');

return;
}
$source = <<<'EOF'
<?php
$string = 'string should not be modified';
$heredoc = <<<HD
Heredoc should not be modified
HD;
$nowdoc = <<<'ND'
Nowdoc should not be modified
ND;
/**
* some class comments to strip
*/
class TestClass
{
/**
* some method comments to strip
*/
public function doStuff()
{
// inline comment
}
}
EOF;
$expected = <<<'EOF'
<?php
$string = 'string should not be modified';
$heredoc =
<<<HD
Heredoc should not be modified
HD;
$nowdoc =
<<<'ND'
Nowdoc should not be modified
ND;
class TestClass
{
public function doStuff()
{
}
}
EOF;

$output = Kernel::stripComments($source);

// Heredocs are preserved, making the output mixing unix and windows line
// endings, switching to "\n" everywhere on windows to avoid failure.
if (defined('PHP_WINDOWS_VERSION_MAJOR')) {
$expected = str_replace("\r\n", "\n", $expected);
$output = str_replace("\r\n", "\n", $output);
}

$this->assertEquals($expected, $output);
}

}

0 comments on commit 643a7e5

Please sign in to comment.