Skip to content

Commit

Permalink
Improved tests and autoloading
Browse files Browse the repository at this point in the history
  • Loading branch information
mickaelandrieu committed May 5, 2017
1 parent b106ad3 commit 4508857
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 22 deletions.
2 changes: 1 addition & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ before_install:
- phpenv config-rm xdebug.ini

before_script:
- composer install --dev --prefer-source
- composer install --prefer-dist --no-progress --no-suggest --no-interaction --optimize-autoloader --classmap-authoritative

script: vendor/bin/phpunit

Expand Down
11 changes: 7 additions & 4 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,16 +24,19 @@
"require": {
"php": ">=7.0",
"symfony/console": "~3.2",
"certificationy/certificationy": "dev-master",
"certificationy/certificationy": "~2.0",
"symfony/yaml": "~3.2",
"certificationy/symfony-pack": "dev-master",
"certificationy/php-pack": "dev-master"
"certificationy/symfony-pack": "~1.4",
"certificationy/php-pack": "~1.4"
},
"require-dev": {
"phpunit/phpunit": "~6.0"
},
"autoload": {
"psr-4": {"Certificationy\\Cli\\": ""}
"psr-4": {"Certificationy\\Cli\\": "src"}
},
"autoload-dev": {
"psr-4": { "Tests\\": "tests/" }
},
"extra": {
"branch-alias": {
Expand Down
6 changes: 3 additions & 3 deletions phpunit.xml.dist
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>

<phpunit colors="true" bootstrap="Tests/bootstrap.php">
<phpunit colors="true" bootstrap="tests/bootstrap.php">
<testsuites>
<testsuite name="Certificationy Tests">
<directory suffix="Test.php">./Tests</directory>
<directory suffix="Test.php">./tests</directory>
</testsuite>
</testsuites>

<filter>
<whitelist>
<directory>./</directory>
<exclude>
<directory>./Tests</directory>
<directory>./tests</directory>
<directory>./vendor</directory>
</exclude>
</whitelist>
Expand Down
22 changes: 15 additions & 7 deletions Command/StartCommand.php → src/Command/StartCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
namespace Certificationy\Cli\Command;

use Certificationy\Loaders\YamlLoader as Loader;
use Certificationy\Collections\Questions;
use Certificationy\Set;

use Symfony\Component\Console\Command\Command;
Expand Down Expand Up @@ -45,7 +46,7 @@ protected function configure()
$this
->setName('start')
->setDescription('Starts a new question set')
->addArgument('categories', InputArgument::IS_ARRAY, 'Which categories do you want (separate multiple with a space)', array())
->addArgument('categories', InputArgument::IS_ARRAY, 'Which categories do you want (separate multiple with a space)', [])
->addOption('number', null, InputOption::VALUE_OPTIONAL, 'How many questions do you want?', 20)
->addOption('list', 'l', InputOption::VALUE_NONE, 'List categories')
->addOption("training", null, InputOption::VALUE_NONE, "Training mode: the solution is displayed after each question")
Expand Down Expand Up @@ -125,7 +126,7 @@ protected function askQuestions(Set $set, InputInterface $input, OutputInterface
$set->setUserAnswers($i, $answers);

if ($input->getOption("training")) {
$uniqueSet = new Set(array($i => $question));
$uniqueSet = Set::create(new Questions([$i => $question]));

$uniqueSet->setUserAnswers($i, $answers);

Expand All @@ -145,7 +146,7 @@ protected function askQuestions(Set $set, InputInterface $input, OutputInterface
*/
protected function displayResults(Set $set, OutputInterface $output)
{
$results = array();
$results = [];

$questionCount = 0;

Expand All @@ -155,12 +156,12 @@ protected function displayResults(Set $set, OutputInterface $output)
$label = wordwrap($question->getQuestion(), self::WORDWRAP_NUMBER, "\n");
$help = $question->getHelp();

$results[] = array(
$results[] = [
sprintf('<comment>#%d</comment> %s', $questionCount, $label),
wordwrap(implode(', ', $question->getCorrectAnswersValues()), self::WORDWRAP_NUMBER, "\n"),
$isCorrect ? '<info>✔</info>' : '<error>✗</error>',
(null !== $help) ? wordwrap($help, self::WORDWRAP_NUMBER, "\n") : '',
);
];
}

if ($results) {
Expand All @@ -185,8 +186,15 @@ protected function displayResults(Set $set, OutputInterface $output)
*
* @return String $path The configuration filepath
*/
protected function path($config = null)
protected function path(string $config = null) : string
{
return $config ? $config : dirname(__DIR__).DIRECTORY_SEPARATOR.('config.yml');
$defaultConfig = dirname(__DIR__)
. DIRECTORY_SEPARATOR
. '..'
. DIRECTORY_SEPARATOR
. 'config.yml'
;

return $config ?? $defaultConfig;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
* file that was distributed with this source code.
*/

namespace Certificationy\Cli\Tests\Command;
namespace Tests\Command;

use Certificationy\Loaders\YamlLoader as Loader;
use Certificationy\Cli\Command\StartCommand;
Expand Down Expand Up @@ -46,7 +46,7 @@ public function setUp()
$app = new Application();
$app->add(new StartCommand());
$this->command = $app->find('start');
$this->configFile = __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . 'config.yml';
$this->configFile = $this->getTestsFolder() . 'config_test.yml';
$paths = Yaml::parse(file_get_contents($this->configFile));

$this->yamlLoader = new Loader($paths);
Expand All @@ -57,11 +57,13 @@ public function testCanListCategories()
$commandTester = new CommandTester($this->command);
$commandTester->execute(array(
'command' => $this->command->getName(),
'-l' => true
'-l' => true,
'-c' => $this->configFile
));

$output = $commandTester->getDisplay();
$this->assertRegExp('/Templating/', $output);

$this->assertRegExp('/A/', $output);
$this->assertCount(count($this->yamlLoader->categories()) + 1, explode("\n", $output));
}

Expand All @@ -73,12 +75,13 @@ public function testCanGetQuestions()
$commandTester = new CommandTester($this->command);
$commandTester->execute(array(
'command' => $this->command->getName(),
'categories' => ['Templating'],
'categories' => ['B'],
'-c' => $this->configFile
));

$output = $commandTester->getDisplay();
$this->assertRegExp('/Twig/', $output);
$this->assertRegExp('/Starting a new set of 20 questions/', $commandTester->getDisplay());
$this->assertRegExp('/B/', $output);
$this->assertRegExp('/Starting a new set of 3 questions/', $commandTester->getDisplay());
}

public function testCanHideInformationAboutMultipleChoice()
Expand All @@ -91,12 +94,33 @@ public function testCanHideInformationAboutMultipleChoice()
'command' => $this->command->getName(),
'--hide-multiple-choice' => null,
'--number' => 1,
'-c' => $this->configFile
));

$output = $commandTester->getDisplay();
$this->assertNotRegExp('/This question IS( NOT)? multiple choice/', $output);
}

public function testCanUseTrainingMode()
{
$helper = $this->command->getHelper('question');
$helper->setInputStream($this->getInputStream(str_repeat("0\n", 1)));

$commandTester = new CommandTester($this->command);
$commandTester->execute(array(
'command' => $this->command->getName(),
'--hide-multiple-choice' => null,
'--number' => 1,
'--training' => true,
'-c' => $this->configFile
));

$commandTester->setInputs([0]);
$output = $commandTester->getDisplay();

$this->assertRegExp('/| Question | Correct answer | Result | Help |/', $output);
}

protected function getInputStream($input)
{
$stream = fopen('php://memory', 'r+', false);
Expand All @@ -105,4 +129,9 @@ protected function getInputStream($input)

return $stream;
}

private function getTestsFolder()
{
return __DIR__ . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR;
}
}
23 changes: 23 additions & 0 deletions tests/assets/test-yaml-pack/a.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
category: A
questions:
-
question: 'Foo?'
answers:
- {value: 'foo', correct: false}
- {value: 'bar', correct: false}
- {value: 'baz', correct: true}
- {value: 'bazinga', correct: false}
-
question: 'Bar?'
answers:
- {value: 'bar', correct: true}
- {value: 'bar', correct: false}
- {value: 'baz', correct: false}
- {value: 'bazinga', correct: false}
-
question: 'Baz?'
answers:
- {value: 'foo', correct: false}
- {value: 'bar', correct: false}
- {value: 'baz', correct: true}
- {value: 'bazinga', correct: false}
23 changes: 23 additions & 0 deletions tests/assets/test-yaml-pack/b.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
category: B
questions:
-
question: 'Foo?'
answers:
- {value: 'foo', correct: false}
- {value: 'bar', correct: false}
- {value: 'baz', correct: true}
- {value: 'bazinga', correct: false}
-
question: 'Bar?'
answers:
- {value: 'bar', correct: true}
- {value: 'bar', correct: false}
- {value: 'baz', correct: false}
- {value: 'bazinga', correct: false}
-
question: 'Baz?'
answers:
- {value: 'foo', correct: false}
- {value: 'bar', correct: false}
- {value: 'baz', correct: true}
- {value: 'bazinga', correct: false}
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions tests/config_test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
paths: ["./tests/assets/test-yaml-pack"]

0 comments on commit 4508857

Please sign in to comment.