You are here

class ApigeeEdgeCommandsTest in Apigee Edge 8

Test ApigeeEdgeCommands class.

@group apigee_edge

Hierarchy

Expanded class hierarchy of ApigeeEdgeCommandsTest

File

tests/src/Unit/Commands/ApigeeEdgeCommandsTest.php, line 36

Namespace

Drupal\Tests\apigee_edge\Unit\Commands
View source
class ApigeeEdgeCommandsTest extends UnitTestCase {

  /**
   * The system under test.
   *
   * @var \Drupal\apigee_edge\Commands\ApigeeEdgeCommands
   */
  protected $apigeeEdgeCommands;

  /**
   * The CLI Service mock.
   *
   * @var \Prophecy\Prophecy\ObjectProphecy
   */
  protected $cliService;

  /**
   * The DrushStyle mock.
   *
   * @var \Prophecy\Prophecy\ObjectProphecy
   */
  protected $io;

  /**
   * {@inheritdoc}
   */
  protected function setUp() {
    parent::setUp();
    $this->cliService = $this
      ->prophesize(CliServiceInterface::class);
    $this->apigeeEdgeCommands = new ApigeeEdgeCommands($this->cliService
      ->reveal());

    // Set io in DrushCommands to a mock.
    $apigee_edge_commands_reflection = new ReflectionClass($this->apigeeEdgeCommands);
    $reflection_io_property = $apigee_edge_commands_reflection
      ->getProperty('io');
    $reflection_io_property
      ->setAccessible(TRUE);
    $this->io = $this
      ->prophesize(DrushStyle::class);
    $reflection_io_property
      ->setValue($this->apigeeEdgeCommands, $this->io
      ->reveal());
    $this->io
      ->askHidden(Argument::type('string'), Argument::any())
      ->willReturn('I<3APIS!');
  }

  /**
   * Calls to Drush command should pass through to CLI service.
   */
  public function testCreateEdgeRole() {
    $drush_options = [
      'password' => 'opensesame',
      'base-url' => 'http://api.apigee.com/v1',
      'role-name' => 'portalRole',
      'force' => 'FALSE',
    ];
    $this->apigeeEdgeCommands
      ->createEdgeRole('orgA', 'emailA', $drush_options);
    $this->cliService
      ->createEdgeRoleForDrupal(Argument::type(DrushStyle::class), Argument::type('string'), Argument::type('string'), Argument::type('string'), Argument::type('string'), Argument::type('string'), Argument::type('string'), Argument::type('bool'))
      ->shouldHaveBeenCalledTimes(1);
  }

  /**
   * Test validateCreateEdgeRole function does not prompt for password.
   *
   * When password option is set, do not prompt for password.
   */
  public function testValidatePasswordParam() {
    $command_data_input = $this
      ->prophesize(InputInterface::class);
    $command_data_input
      ->getOption('password')
      ->willReturn('secret');
    $command_data_input
      ->getArgument('email')
      ->willReturn('email.example.com');
    $command_data = $this
      ->prophesize(CommandData::class);
    $command_data
      ->input()
      ->willReturn($command_data_input
      ->reveal());
    $this->apigeeEdgeCommands
      ->validateCreateEdgeRole($command_data
      ->reveal());

    // Make sure password was not prompted to user.
    $command_data_input
      ->getOption('password')
      ->shouldHaveBeenCalled();
    $this->io
      ->askHidden(Argument::type('string'), Argument::any())
      ->shouldNotBeCalled();
    $command_data_input
      ->setOption()
      ->shouldNotHaveBeenCalled();
  }

  /**
   * Test validateCreateEdgeRole prompts for password.
   *
   * When password option not set, password should be inputted by user.
   */
  public function testValidatePasswordParamEmpty() {
    $command_data_input = $this
      ->prophesize(InputInterface::class);
    $command_data_input
      ->getOption('password')
      ->willReturn(NULL);
    $command_data_input
      ->setOption(Argument::type('string'), Argument::type('string'))
      ->willReturn();
    $command_data_input
      ->getArgument('email')
      ->willReturn('email.example.com');
    $command_data = $this
      ->prophesize(CommandData::class);
    $command_data
      ->input()
      ->willReturn($command_data_input
      ->reveal());
    $this->apigeeEdgeCommands
      ->validateCreateEdgeRole($command_data
      ->reveal());

    // Make sure password not requested.
    $command_data_input
      ->getOption('password')
      ->shouldHaveBeenCalled();
    $this->io
      ->askHidden(Argument::type('string'), Argument::any())
      ->shouldBeCalled();
    $command_data_input
      ->setOption('password', 'I<3APIS!')
      ->shouldHaveBeenCalled();
  }

  /**
   * Test calling with force function when role already exists.
   */
  public function testCreateEdgeEdgeRoleWithForceParam() {
    $drush_options = [
      'password' => 'opensesame',
      'base-url' => 'http://api.apigee.com/v1',
      'role-name' => 'portalRole',
      'force' => TRUE,
    ];
    $this->apigeeEdgeCommands
      ->createEdgeRole('orgA', 'emailA', $drush_options);
    $this->cliService
      ->createEdgeRoleForDrupal(Argument::type(DrushStyle::class), Argument::type('string'), Argument::type('string'), Argument::type('string'), Argument::type('string'), Argument::type('string'), Argument::type('string'), TRUE)
      ->shouldHaveBeenCalledTimes(1);
  }

  /**
   * Test calling when role exists but force flag not given, should error.
   */
  public function testCreateEdgeEdgeRoleWithoutForceParam() {
    $drush_options = [
      'password' => 'opensesame',
      'base-url' => 'http://api.apigee.com/v1',
      'role-name' => 'portalRole',
      'force' => FALSE,
    ];
    $this->apigeeEdgeCommands
      ->createEdgeRole('orgA', 'emailA', $drush_options);
    $this->cliService
      ->createEdgeRoleForDrupal(Argument::type(DrushStyle::class), Argument::type('string'), Argument::type('string'), Argument::type('string'), Argument::type('string'), Argument::type('string'), Argument::type('string'), FALSE)
      ->shouldHaveBeenCalledTimes(1);
  }

}

Members

Namesort descending Modifiers Type Description Overrides
ApigeeEdgeCommandsTest::$apigeeEdgeCommands protected property The system under test.
ApigeeEdgeCommandsTest::$cliService protected property The CLI Service mock.
ApigeeEdgeCommandsTest::$io protected property The DrushStyle mock.
ApigeeEdgeCommandsTest::setUp protected function Overrides UnitTestCase::setUp
ApigeeEdgeCommandsTest::testCreateEdgeEdgeRoleWithForceParam public function Test calling with force function when role already exists.
ApigeeEdgeCommandsTest::testCreateEdgeEdgeRoleWithoutForceParam public function Test calling when role exists but force flag not given, should error.
ApigeeEdgeCommandsTest::testCreateEdgeRole public function Calls to Drush command should pass through to CLI service.
ApigeeEdgeCommandsTest::testValidatePasswordParam public function Test validateCreateEdgeRole function does not prompt for password.
ApigeeEdgeCommandsTest::testValidatePasswordParamEmpty public function Test validateCreateEdgeRole prompts for password.
PhpunitCompatibilityTrait::getMock Deprecated public function Returns a mock object for the specified class using the available method.
PhpunitCompatibilityTrait::setExpectedException Deprecated public function Compatibility layer for PHPUnit 6 to support PHPUnit 4 code.
UnitTestCase::$randomGenerator protected property The random generator.
UnitTestCase::$root protected property The app root. 1
UnitTestCase::assertArrayEquals protected function Asserts if two arrays are equal by sorting them first.
UnitTestCase::getBlockMockWithMachineName Deprecated protected function Mocks a block with a block plugin. 1
UnitTestCase::getClassResolverStub protected function Returns a stub class resolver.
UnitTestCase::getConfigFactoryStub public function Returns a stub config factory that behaves according to the passed array.
UnitTestCase::getConfigStorageStub public function Returns a stub config storage that returns the supplied configuration.
UnitTestCase::getContainerWithCacheTagsInvalidator protected function Sets up a container with a cache tags invalidator.
UnitTestCase::getRandomGenerator protected function Gets the random generator for the utility methods.
UnitTestCase::getStringTranslationStub public function Returns a stub translation manager that just returns the passed string.
UnitTestCase::randomMachineName public function Generates a unique random string containing letters and numbers.