Skip to content
This repository has been archived by the owner on Mar 30, 2018. It is now read-only.

Generalise, document and include a test for custom phpunit path #253

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,27 @@ other than your project's base directory.
"source": {
"directories": [
"src"
],
]
}
}
```

If you use a specific phpunit instance, e.g. a PHAR file or script on a custom path, you
can configure Humbug to use that instance by configuring the absolute path to
the file. Humbug will otherwise perform a standard search for an executable, i.e.
first priority being local vendor directory, next being system PATH. The custom phar or
script file name must start with `phpunit`.

```js
{
"timeout": 10,
"source": {
"directories": [
"src"
]
}
"phpunit": {
"filepath": "/absolute/path/to/phpunit-5.7.phar"
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we allow a relative file path (relative to the config file)?

}
}
```
Expand Down
4 changes: 2 additions & 2 deletions src/Adapter/Phpunit/Process/PhpunitExecutableFinder.php
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ private function getConfig()
private function getExecutableNames()
{
if ($this->getConfig()->isPhpunitConfigured()) {
return [basename($this->getConfig()->getPhpunitConfig()->phar)];
return [basename($this->getConfig()->getPhpunitConfig()->filepath)];
}
return ['phpunit', 'phpunit.phar'];
}
Expand All @@ -139,7 +139,7 @@ private function getExecutableNames()
private function getPhpunitExecutablePath()
{
if ($this->getConfig()->isPhpunitConfigured()) {
return dirname($this->getConfig()->getPhpunitConfig()->phar);
return dirname($this->getConfig()->getPhpunitConfig()->filepath);
}
return getcwd();
}
Expand Down
34 changes: 28 additions & 6 deletions src/Command/Configure.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ protected function execute(InputInterface $input, OutputInterface $output)

$chDir = $this->resolveChDir($input, $output);

$sourcesDirs = $this->getDirs($input, $output, 'What source directories do you want to include? : ');
$sourcesDirs = $this->getDirs($input, $output, 'What source directories do you want to include? [Press enter to skip after one provided] : ');

if (empty($sourcesDirs)) {
$output->writeln('A source directory was not provided. Unable to generate "humbug.json.dist".');
Expand All @@ -55,7 +55,7 @@ protected function execute(InputInterface $input, OutputInterface $output)
$excludeDirs = $this->getDirs(
$input,
$output,
'Any directories to exclude from within your source directories? :'
'Any directories to exclude from within your source directories? [Press enter to skip] :'
);

$timeout = $this->getTimeout($input, $output);
Expand All @@ -64,6 +64,8 @@ protected function execute(InputInterface $input, OutputInterface $output)

$jsonLogFile = $this->getJsonLogFile($input, $output);

$phpunitFilepath = $this->getPhpunitFilepath($input, $output);

if (!$this->isGenerationConfirmed($input, $output)) {
$output->writeln('<fg=red>Aborted.</fg=red>' .PHP_EOL);
return 0;
Expand All @@ -75,7 +77,8 @@ protected function execute(InputInterface $input, OutputInterface $output)
$chDir,
$timeout,
$textLogFile,
$jsonLogFile
$jsonLogFile,
$phpunitFilepath
);

$this->saveConfiguration($configuration);
Expand Down Expand Up @@ -118,6 +121,7 @@ private function isAlreadyConfigured(InputInterface $input)
* @param $timeout
* @param $textLogFile
* @param $jsonLogFile
* @param $phpunitFilepath
*
* @return \stdClass
*/
Expand All @@ -127,7 +131,8 @@ private function createConfiguration(
$chDir,
$timeout,
$textLogFile,
$jsonLogFile
$jsonLogFile,
$phpunitFilepath
) {
$source = new \stdClass();
$source->directories = $sourcesDirs;
Expand All @@ -153,6 +158,11 @@ private function createConfiguration(
$configuration->logs = $logs;
}

if (!empty($phpunitFilepath)) {
$configuration->phpunit = new \stdClass();
$configuration->phpunit->filepath = $phpunitFilepath;
}

return $configuration;
}

Expand Down Expand Up @@ -197,7 +207,7 @@ private function resolveChDir(InputInterface $input, OutputInterface $output)
*/
private function createFrameworkConfigurationQuestion(ConfigurationLocator $configurationLocator)
{
$frameworkConfigurationQuestion = new Question('Where is your phpunit.xml(.dist) configuration located? : ');
$frameworkConfigurationQuestion = new Question('Where is your phpunit.xml(.dist) configuration located? [current dir] : ');
$frameworkConfigurationQuestion->setValidator(function ($answer) use ($configurationLocator) {
$answer = trim($answer);

Expand Down Expand Up @@ -303,11 +313,23 @@ private function getTextLogFile(InputInterface $input, OutputInterface $output)
*/
private function getJsonLogFile(InputInterface $input, OutputInterface $output)
{
$textLogQuestion = new Question('Where do you want to store the json log (if you need it)? : ');
$textLogQuestion = new Question('Where do you want to store the json log (if you need it)? [Press enter to skip] : ');
$textLogFile = $this->getQuestionHelper()->ask($input, $output, $textLogQuestion);
return $textLogFile;
}

/**
* @param InputInterface $input
* @param OutputInterface $output
* @return string
*/
private function getPhpunitFilepath(InputInterface $input, OutputInterface $output)
{
$textPhpunitQuestion = new Question('If you use a non-standard phpunit command/phar, you can provide its filepath here. [Press enter to skip] : ');
$textPhpunitFile = $this->getQuestionHelper()->ask($input, $output, $textPhpunitQuestion);
return $textPhpunitFile;
}

/**
* @param $textLogFile
* @param $jsonLogFile
Expand Down
2 changes: 1 addition & 1 deletion src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public function getPhpunitConfig()
'Phpunit destination is not included in configuration file'
);
}
if (!isset($this->config->phpunit->phar)) {
if (!isset($this->config->phpunit->filepath)) {
throw new JsonConfigException(
'full path of phpunit executable is not included in configuration file'
);
Expand Down
5 changes: 5 additions & 0 deletions tests/Adapter/Phpunit/Process/PhpunitExecutableFinderTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,9 @@ public function testFinderCanLocatePhpunitExecutable()
$result = $finder->find();
$this->assertRegExp('%phpunit.*(\\.bat|\\.phar)?$%i', $result);
}

public function testFinderCanLocateACustomPhpunitNotInVendorOrPath()
{
$this->markTestIncomplete('Design test to inject custom config for this.');
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving this aside for the time being. There's no clean way to inject the config change needed at the moment.

}
}
56 changes: 51 additions & 5 deletions tests/Command/ConfigureTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public function testShouldCreateConfigurationIfUserConfirmsIt()
"\n" .
"\n" .
"\n" .
"\n" .
"Y\n"
);

Expand All @@ -88,11 +89,6 @@ public function testShouldCreateConfigurationIfUserConfirmsIt()
$this->assertHumbugJsonEqualsJson($expectedJson);
}

private function assertHumbugJsonEqualsJson($expectedJson)
{
$this->assertJsonStringEqualsJsonString($expectedJson, file_get_contents('humbug.json.dist'));
}

public function testShouldCreateConfigurationWithDifferentLocationOfFrameworkConfigFile()
{
mkdir('app');
Expand All @@ -107,6 +103,7 @@ public function testShouldCreateConfigurationWithDifferentLocationOfFrameworkCon
"\n" .
"\n" .
"\n" .
"\n" .
Copy link
Collaborator Author

@padraic padraic May 13, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Extra line of input was needed for all tests with the new configure question included.

"Y\n"
);

Expand Down Expand Up @@ -142,6 +139,7 @@ public function testShouldCreateConfigurationWithCustomTextLogging()
"\n" .
"custom-log.txt\n" .
"\n" .
"\n" .
"Y\n"
);

Expand Down Expand Up @@ -176,6 +174,7 @@ public function testShouldCreateConfigurationWithJsonLogging()
"\n" .
"\n" .
"humbuglog.json\n" .
"\n" .
"Y\n"
);

Expand Down Expand Up @@ -215,6 +214,7 @@ public function testShouldCreateConfigurationWithMultipleSourceDirectories()
"\n" .
"\n" .
"\n" .
"\n" .
"Y\n"
);

Expand Down Expand Up @@ -260,6 +260,7 @@ public function testShouldCreateConfigurationWithExcludeDirectories()
"\n" .
"\n" .
"\n" .
"\n" .
"Y\n"
);

Expand Down Expand Up @@ -298,6 +299,7 @@ public function testShouldCreateConfigurationWithTimeout()
"5\n" .
"\n" .
"\n" .
"\n" .
"Y\n"
);

Expand All @@ -320,6 +322,44 @@ public function testShouldCreateConfigurationWithTimeout()
$this->assertHumbugJsonEqualsJson($expectedJson);
}

public function testShouldCreateConfigurationWithCustomPhpunit()
{
mkdir('src');
touch('phpunit.xml');

$this->setUserInput(
"src\n" .
"\n" .
"\n" .
"\n" .
"\n" .
"\n" .
"/path/to/phpunit-2.0.phar\n" .
"Y\n"
);

$this->executeCommand();

$expectedJson = <<<JSON
{
"source": {
"directories": [
"src"
]
},
"timeout": 10,
"logs": {
"text": "humbuglog.txt"
},
"phpunit": {
"filepath": "\/path\/to\/phpunit-2.0.phar"
}
}
JSON;

$this->assertHumbugJsonEqualsJson($expectedJson);
}

public function testShouldNotCreateConfigurationIfSrcDirectoryNotExists()
{
touch('phpunit.xml');
Expand Down Expand Up @@ -348,6 +388,7 @@ public function testShouldExitIfIfUserDoesNotWantToConfigure()
"\n" .
"\n" .
"\n" .
"\n" .
"N\\n"
);

Expand All @@ -365,6 +406,11 @@ public function testShouldNotChangeConfigurationIfItAlreadyExists()
$this->assertStringEqualsFile('humbug.json', '{}');
}

private function assertHumbugJsonEqualsJson($expectedJson)
{
$this->assertJsonStringEqualsJsonString($expectedJson, file_get_contents('humbug.json.dist'));
}

private function getInputStream($input)
{
$stream = fopen('php://memory', 'r+', false);
Expand Down