Skip to content

Commit

Permalink
Merge pull request andres-montanez#197 from edpauto/command-tests
Browse files Browse the repository at this point in the history
Add commands tests
  • Loading branch information
eps90 committed Mar 9, 2015
2 parents cb965a2 + c690ea3 commit 0406c7d
Show file tree
Hide file tree
Showing 13 changed files with 854 additions and 16 deletions.
2 changes: 1 addition & 1 deletion Mage/Command/AbstractCommand.php
Expand Up @@ -31,7 +31,7 @@ abstract class AbstractCommand
* @return integer exit code
* @throws \Exception
*/
public abstract function run();
abstract public function run();

/**
* Sets the Loaded Configuration.
Expand Down
31 changes: 27 additions & 4 deletions Mage/Command/BuiltIn/CompileCommand.php
Expand Up @@ -21,20 +21,43 @@
*/
class CompileCommand extends AbstractCommand
{
/**
* @var Compiler
*/
private $compiler;

public function __construct(Compiler $compiler = null)
{
if ($compiler === null) {
$compiler = new Compiler();
}

$this->compiler = $compiler;
}

/**
* @see \Mage\Compile::compile()
*/
public function run()
{
if (ini_get('phar.readonly')) {
Console::output('The <purple>php.ini</purple> variable <light_red>phar.readonly</light_red> must be <yellow>Off</yellow>.', 1, 2);
Console::output(
'The <purple>php.ini</purple> variable <light_red>phar.readonly</light_red>'
. ' must be <yellow>Off</yellow>.',
1,
2
);

return 200;
}

$compiler = new Compiler;
$compiler->compile();
$this->compiler->compile();

Console::output('<light_purple>mage.phar</light_purple> compiled <light_green>successfully</light_green>', 0, 2);
Console::output(
'<light_purple>mage.phar</light_purple> compiled <light_green>successfully</light_green>',
0,
2
);

return 0;
}
Expand Down
2 changes: 1 addition & 1 deletion Mage/Command/BuiltIn/ListCommand.php
Expand Up @@ -40,7 +40,7 @@ public function run()
$exitCode = $this->listEnvironments();
break;

default;
default:
throw new Exception('The Type of Elements to List is needed.');
break;
}
Expand Down
20 changes: 16 additions & 4 deletions Mage/Command/BuiltIn/LockCommand.php
Expand Up @@ -35,14 +35,26 @@ public function run()
$reason = Console::readInput();

$lockmsg = PHP_EOL;
if ($name) $lockmsg .= 'Locked by ' . $name . ' ';
if ($email) $lockmsg .= '(' . $email . ')';
if ($reason) $lockmsg .= PHP_EOL . $reason . PHP_EOL;
if ($name) {
$lockmsg .= 'Locked by ' . $name . ' ';
}
if ($email) {
$lockmsg .= '(' . $email . ')';
}
if ($reason) {
$lockmsg .= PHP_EOL . $reason . PHP_EOL;
}

$lockFile = getcwd() . '/.mage/' . $this->getConfig()->getEnvironment() . '.lock';
file_put_contents($lockFile, 'Locked environment at date: ' . date('Y-m-d H:i:s') . $lockmsg);

Console::output('Locked deployment to <light_purple>' . $this->getConfig()->getEnvironment() . '</light_purple> environment', 1, 2);
Console::output(
'Locked deployment to <light_purple>'
. $this->getConfig()->getEnvironment()
. '</light_purple> environment',
1,
2
);

return 0;
}
Expand Down
10 changes: 7 additions & 3 deletions Mage/Command/BuiltIn/UnlockCommand.php
Expand Up @@ -19,8 +19,7 @@
*
* @author Andrés Montañez <andres@andresmontanez.com>
*/
class UnlockCommand
extends AbstractCommand implements RequiresEnvironment
class UnlockCommand extends AbstractCommand implements RequiresEnvironment
{
/**
* Unlocks an Environment
Expand All @@ -33,7 +32,12 @@ public function run()
@unlink($lockFile);
}

Console::output('Unlocked deployment to <light_purple>' . $this->getConfig()->getEnvironment() . '</light_purple> environment', 1, 2);
Console::output(
'Unlocked deployment to <light_purple>'
. $this->getConfig()->getEnvironment() . '</light_purple> environment',
1,
2
);

return 0;
}
Expand Down
8 changes: 7 additions & 1 deletion composer.json
Expand Up @@ -10,7 +10,8 @@
},
"require-dev": {
"phpunit/phpunit": "4.3.5",
"satooshi/php-coveralls": ">=0.6.1"
"satooshi/php-coveralls": ">=0.6.1",
"malkusch/php-mock": "dev-php-5.3"
},
"autoload": {
"psr-4": {
Expand All @@ -19,6 +20,11 @@
"Command\\": [".mage/tasks", "../../../.mage/commands"]
}
},
"autoload-dev": {
"psr-4": {
"MageTest\\": "./tests/MageTest"
}
},
"config": {
"bin-dir": "bin"
},
Expand Down
54 changes: 52 additions & 2 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

47 changes: 47 additions & 0 deletions tests/MageTest/Command/AbstractCommandTest.php
@@ -0,0 +1,47 @@
<?php

namespace MageTest\Command;

use Mage\Command\AbstractCommand;
use MageTest\TestHelper\BaseTest;
use PHPUnit_Framework_MockObject_MockObject;

/**
* Class AbstractCommandTest
* @package MageTest\Command
* @author Jakub Turek <ja@kubaturek.pl>
* @coversDefaultClass Mage\Command\AbstractCommand
*/
class AbstractCommandTest extends BaseTest
{
/**
* @var AbstractCommand|PHPUnit_Framework_MockObject_MockObject
*/
private $abstractCommand;

/**
* @before
*/
public function before()
{
$this->abstractCommand = $this->getMockForAbstractClass('Mage\Command\AbstractCommand');
}

/**
* @covers ::setConfig
*/
public function testSetConfig()
{
$configMock = $this->getMock('Mage\Config');
$this->doTestSetter($this->abstractCommand, 'config', $configMock);
}

/**
* @covers ::getConfig
*/
public function testGetConfig()
{
$configMock = $this->getMock('Mage\Config');
$this->doTestGetter($this->abstractCommand, 'config', $configMock);
}
}
108 changes: 108 additions & 0 deletions tests/MageTest/Command/BuiltIn/CompileCommandTest.php
@@ -0,0 +1,108 @@
<?php

namespace MageTest\Command\BuiltIn;

use Mage\Command\BuiltIn\CompileCommand;
use MageTest\TestHelper\BaseTest;
use malkusch\phpmock\FixedValueFunction;
use malkusch\phpmock\MockBuilder;

/**
* Class CompileCommandTest
* @package MageTest\Command\BuiltIn
* @coversDefaultClass Mage\Command\BuiltIn\CompileCommand
* @uses malkusch\phpmock\FixedValueFunction
* @uses malkusch\phpmock\Mock
* @uses malkusch\phpmock\MockBuilder
* @uses Mage\Console
* @uses Mage\Console\Colors
*/
class CompileCommandTest extends BaseTest
{
/**
* @var FixedValueFunction
*/
private $iniGetValue;

/**
* @before
*/
public function before()
{
$this->iniGetValue = new FixedValueFunction();
$mockBuilder = new MockBuilder();
$iniGetMock = $mockBuilder->setNamespace('Mage\Command\BuiltIn')
->setName("ini_get")
->setCallableProvider($this->iniGetValue)
->build();
$iniGetMock->disable();
$iniGetMock->enable();

$this->setUpConsoleStatics();
}

/**
* @covers ::__construct
*/
public function testConstruct()
{
$compilerMock = $this->getMock('Mage\Compiler');
$compileCommand = new CompileCommand($compilerMock);

$compilerProperty = $this->getPropertyValue($compileCommand, 'compiler');

$this->assertInstanceOf('Mage\Compiler', $compilerProperty);
$this->assertSame($compilerMock, $compilerProperty);
}

/**
* @covers ::__construct
*/
public function testConstructWithNoParams()
{
$compileCommand = new CompileCommand();
$compilerProperty = $this->getPropertyValue($compileCommand, 'compiler');

$this->assertInstanceOf('Mage\Compiler', $compilerProperty);
}

/**
* @covers ::__construct
* @covers ::run
*/
public function testRun()
{
$expectedOutput = "mage.phar compiled successfully\n\n";
$expectedExitCode = 0;
$this->expectOutputString($expectedOutput);

$this->iniGetValue->setValue(false);

$compilerMock = $this->getMock('Mage\Compiler');
$compilerMock->expects($this->once())
->method('compile');
$compileCommand = new CompileCommand($compilerMock);

$actualExitCode = $compileCommand->run();

$this->assertEquals($expectedExitCode, $actualExitCode);
}

/**
* @covers ::__construct
* @covers ::run
*/
public function testRunWhenPharReadonlyEnabled()
{
$expectedOutput = "\tThe php.ini variable phar.readonly must be Off.\n\n";
$expectedExitCode = 200;
$this->expectOutputString($expectedOutput);
$this->iniGetValue->setValue(true);

$compilerMock = $this->getMock('Mage\Compiler');
$compileCommand = new CompileCommand($compilerMock);
$actualExitCode = $compileCommand->run();

$this->assertEquals($expectedExitCode, $actualExitCode);
}
}

0 comments on commit 0406c7d

Please sign in to comment.