Skip to content

[Test] Guidelines proposal to test the commands interactive mode. #2972

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 11 commits into from
Dec 30, 2016
Merged
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
219 changes: 186 additions & 33 deletions Test/Command/Generate/AuthenticationProviderCommandTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,52 +7,205 @@
namespace Drupal\Console\Test\Command\Generate;

use Drupal\Console\Command\Generate\AuthenticationProviderCommand;
use Drupal\Console\Command\Generate\Questions\AuthenticationProviderQuestions;
use Drupal\Console\Command\Generate\Questions\ConfirmGeneration;
use Drupal\Console\Test\Builders\a as an;
use Drupal\Console\Test\Command\GenerateCommandTest;
use Drupal\Console\Utils\StringConverter;
use Prophecy\Argument;
use Symfony\Component\Console\Tester\CommandTester;
use Drupal\Console\Test\DataProvider\AuthenticationProviderDataProviderTrait;

class AuthenticationProviderCommandTest extends GenerateCommandTest
{
use AuthenticationProviderDataProviderTrait;

/**
* AuthenticationProvider generator test
*
* @param string $module
* @param string $class
* @param int $providerId
*
* @dataProvider commandData
*/
public function testGenerateAuthenticationProvider(
$module,
$class,
$providerId
) {
$generator = an::authenticationProviderGenerator();
$command = new AuthenticationProviderCommand(
an::extensionManager(),
$generator->reveal(),
new StringConverter()
/** @test */
public function it_generates_an_authentication_provider_without_interaction()
{
// Given
$this->userConfirmsGeneration();

// When
$code = $this->tester->execute(
$this->withAllOptions(),
$this->nonInteractive
);

// Then
$this
->generator
->generate($this->module, $this->class, $this->providerId)
->shouldHaveBeenCalled()
;
$this->assertEquals(0, $code);
}

/** @test */
public function it_ask_for_a_module_if_none_is_provided()
{
// Given
$this->userConfirmsGeneration();
$this->userProvidesModule();

// When
$code = $this->tester->execute(
$this->withoutModule(),
$this->interactive
);

// Then
$this
->generator
->generate($this->module, $this->class, $this->providerId)
->shouldHaveBeenCalled()
;
$this->assertEquals(0, $code);
}

/** @test */
public function it_ask_for_a_class_if_none_is_provided()
{
// Given
$this->userConfirmsGeneration();
$this->userProvidesClass();

// When
$code = $this->tester->execute(
$this->withoutClass(),
$this->interactive
);

$commandTester = new CommandTester($command);
// Then
$this
->generator
->generate($this->module, $this->class, $this->providerId)
->shouldHaveBeenCalled()
;
$this->assertEquals(0, $code);
}

/** @test */
public function it_ask_for_a_provider_if_none_is_provided()
{
// Given
$this->userConfirmsGeneration();
$this->userInputsProviderId();

$code = $commandTester->execute(
[
'--module' => $module,
'--class' => $class,
'--provider-id' => $providerId,
],
['interactive' => false]
// When
$code = $this->tester->execute(
$this->withoutProviderId(),
$this->interactive
);

$generator
->generate($module, $class, $providerId)
// Then
$this
->generator
->generate($this->module, $this->class, $this->providerId)
->shouldHaveBeenCalled()
;
$this->assertEquals(0, $code);
}

/** @before */
public function configure()
{
$this->configureCollaborators();
$this->createSUT();
}

private function configureCollaborators()
{
$this->generator = an::authenticationProviderGenerator();
$this->questions = $this->prophesize(AuthenticationProviderQuestions::class);
$this->confirmation = $this->prophesize(ConfirmGeneration::class);
}

private function createSUT()
{
$this->command = new AuthenticationProviderCommand(
$this->generator->reveal(),
$this->questions->reveal(),
$this->confirmation->reveal()
);
$this->tester = new CommandTester($this->command);
}

/** @return array */
private function withAllOptions()
{
return [
'--module' => $this->module,
'--class' => $this->class,
'--provider-id' => $this->providerId,
];
}

/** @return array */
private function withoutModule()
{
return [
'--class' => $this->class,
'--provider-id' => $this->providerId,
];
}

/** @return array */
private function withoutClass()
{
return [
'--module' => $this->module,
'--provider-id' => $this->providerId,
];
}

/** @return array */
private function withoutProviderId()
{
return [
'--module' => $this->module,
'--class' => $this->class,
];
}

private function userConfirmsGeneration()
{
$this->confirmation->confirm()->willReturn(true);
}

private function userProvidesModule()
{
$this->questions->askForModule()->willReturn($this->module);
}

private function userProvidesClass()
{
$this->questions->askForClass()->willReturn($this->class);
}

private function userInputsProviderId()
{
$this
->questions
->askForProviderId(Argument::any())
->willReturn($this->providerId)
;
}

/** @var \Drupal\Console\Generator\AuthenticationProviderGenerator */
private $generator;

/** @var AuthenticationProviderQuestions */
private $questions;

/** @var ConfirmGeneration */
private $confirmation;

/** @var AuthenticationProviderCommand */
private $command;

/** @var CommandTester */
private $tester;

private $module = 'module name';
private $class = 'Console\Classname';
private $providerId = 'Console\Classname';
private $nonInteractive = ['interactive' => false];
private $interactive = ['interactive' => true];
}
Loading