Skip to content

Commit

Permalink
Improve PHPSpec coverage.
Browse files Browse the repository at this point in the history
- Changed to use interfaces for type-hinting where necessary.
- Closes #565
  • Loading branch information
jhedstrom committed Jan 16, 2020
1 parent 4808a98 commit c159228
Show file tree
Hide file tree
Showing 16 changed files with 185 additions and 42 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
* [#488](https://github.com/jhedstrom/drupalextension/issues/488) Authenticate user in the backend bootstrap process on login.
### Changed
* [#563](https://github.com/jhedstrom/drupalextension/issues/563) Test on PHP 7.1 through 7.3 (and use Drush 10), remove testing on PHP 7.0.
* [#565](https://github.com/jhedstrom/drupalextension/issues/565) Improved PHPSpec coverage and changed type-hinting to use proper interfaces where necessary.
## [4.0.1] 2019-10-08
### Fixed
* [#552](https://github.com/jhedstrom/drupalextension/issue/552) Remove hard-coded symfony/event-dispatcher requirement.
Expand Down
7 changes: 4 additions & 3 deletions spec/Drupal/DrupalExtension/Context/DrupalContextSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@

namespace spec\Drupal\DrupalExtension\Context;

use Behat\Behat\Context\TranslatableContext;
use Drupal\DrupalExtension\Context\RawDrupalContext;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class DrupalContextSpec extends ObjectBehavior
{
function it_is_drupal_aware()
{
$this->shouldHaveType('Drupal\DrupalExtension\Context\RawDrupalContext');
$this->shouldHaveType(RawDrupalContext::class);
}

function it_is_a_translatable_context()
{
$this->shouldHaveType('Behat\Behat\Context\TranslatableContext');
$this->shouldHaveType(TranslatableContext::class);
}

}
13 changes: 7 additions & 6 deletions spec/Drupal/DrupalExtension/Context/DrushContextSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,26 @@

namespace spec\Drupal\DrupalExtension\Context;

use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

use Behat\Behat\Context\TranslatableContext;
use Drupal\Driver\DrushDriver;
use Drupal\DrupalDriverManager;
use Drupal\DrupalExtension\Context\RawDrupalContext;
use PhpSpec\ObjectBehavior;

class DrushContextSpec extends ObjectBehavior
{
function it_should_be_drupal_aware()
{
$this->shouldHaveType('Drupal\DrupalExtension\Context\RawDrupalContext');
$this->shouldHaveType(RawDrupalContext::class);
}

function it_will_catch_scenarios_without_any_output()
{
$this->shouldThrow('\RuntimeException')->duringReadDrushOutput();
$this->shouldThrow(\RuntimeException::class)->duringReadDrushOutput();
}

function it_is_a_translatable_context()
{
$this->shouldHaveType('Behat\Behat\Context\TranslatableContext');
$this->shouldHaveType(TranslatableContext::class);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Behat\Behat\Context\Context;

use Behat\Behat\Context\Initializer\ContextInitializer;
use Drupal\DrupalDriverManager;
use Drupal\DrupalExtension\Context\DrupalAwareInterface;

Expand Down Expand Up @@ -33,7 +34,7 @@ function let(DrupalDriverManager $drupal, DrupalAuthenticationManagerInterface $

function it_is_a_context_initializer()
{
$this->shouldHaveType('Behat\Behat\Context\Initializer\ContextInitializer');
$this->shouldHaveType(ContextInitializer::class);
}

function it_does_nothing_for_basic_contexts(Context $context)
Expand Down
36 changes: 35 additions & 1 deletion spec/Drupal/DrupalExtension/Context/RandomContextSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,48 @@

namespace spec\Drupal\DrupalExtension\Context;

use Behat\Behat\Hook\Scope\ScenarioScope;
use Behat\Gherkin\Node\FeatureNode;
use Behat\Gherkin\Node\ScenarioInterface;
use Behat\Gherkin\Node\StepNode;
use Drupal\Component\Utility\Random;
use Drupal\Driver\Cores\CoreInterface;
use Drupal\Driver\DrupalDriver;
use Drupal\DrupalDriverManager;
use Drupal\DrupalExtension\Context\RandomContext;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class RandomContextSpec extends ObjectBehavior
{
function let(DrupalDriverManager $drupal, DrupalDriver $driver, CoreInterface $core, Random $random)
{
$random->name(10)->willReturn('known_replacement');
$driver->getRandom()->willReturn($random);
$driver->getCore()->willReturn($core);
$drupal->getDriver(NULL)->willReturn($driver);
$this->setDrupal($drupal);
}

function it_is_initializable()
{
$this->shouldHaveType(RandomContext::class);
}

function it_converts_placeholders_to_random_strings(ScenarioScope $scope, ScenarioInterface $scenario, FeatureNode $feature, StepNode $step1, StepNode $step2)
{
$step1->getText()->willReturn('Given a <?random 123> string');
$step2->getText()->willReturn('Then the <?random 123> placeholder will be replaced');
$step1->getArguments()->shouldBeCalled();
$step2->getArguments()->shouldBeCalled();
$steps = [$step1, $step2];
$scenario->getSteps()->willReturn($steps);
$scope->getScenario()->willReturn($scenario);
$scope->getFeature()->willReturn($feature);
$this->beforeScenarioSetVariables($scope);

$this->transformVariables('Given a <?random 123> string')
->shouldBe('Given a known_replacement string');
$this->transformVariables('Then the <?random 123> placeholder will be replaced')
->shouldBe('Then the known_replacement placeholder will be replaced');
}
}
12 changes: 5 additions & 7 deletions spec/Drupal/DrupalExtension/Context/RawDrupalContextSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,23 @@

namespace spec\Drupal\DrupalExtension\Context;

use Drupal\DrupalDriverManagerInterface;
use Drupal\DrupalExtension\Context\DrupalAwareInterface;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

use Behat\Testwork\Hook\HookDispatcher;
use Behat\Testwork\Hook\HookRepository;

use Drupal\DrupalDriverManager;

class RawDrupalContextSpec extends ObjectBehavior
{
function it_should_be_drupal_aware()
{
$this->shouldHaveType('Drupal\DrupalExtension\Context\DrupalAwareInterface');
$this->shouldHaveType(DrupalAwareInterface::class);
}

function it_can_set_and_get_drupal_manager(DrupalDriverManager $drupal)
function it_can_set_and_get_drupal_manager(DrupalDriverManagerInterface $drupal)
{
$this->setDrupal($drupal);
$this->getDrupal()->shouldBeAnInstanceOf('Drupal\DrupalDriverManager');
$this->getDrupal()->shouldBeAnInstanceOf(DrupalDriverManagerInterface::class);
}

function it_can_set_and_get_drupal_parameters()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,43 @@
use Behat\Behat\Context\Context;
use Behat\Testwork\Environment\Environment;

use Behat\Testwork\Suite\Suite;
use Drupal\DrupalExtension\Hook\Scope\BeforeNodeCreateScope;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class BeforeNodeCreateScopeSpec extends ObjectBehavior
{
function let(Environment $environment, Context $context)
function let(Environment $environment, Context $context, Suite $suite)
{
$node = new \stdClass();
$environment->getSuite()->willReturn($suite);
$this->beConstructedWith($environment, $context, $node);
}

function it_is_initializable()
{
$this->shouldHaveType('Drupal\DrupalExtension\Hook\Scope\BeforeNodeCreateScope');
$this->shouldHaveType(BeforeNodeCreateScope::class);
}

function it_should_return_context()
{
$context = $this->getContext();
$context->shouldBeAnInstanceOf('Behat\Behat\Context\Context');
$context->shouldBeAnInstanceOf(Context::class);
}

function it_should_return_a_node()
{
$this->getEntity()->shouldBeAnInstanceOf('stdClass');
$this->getEntity()->shouldBeAnInstanceOf(\stdClass::class);
}

function it_should_return_environment()
{
$this->getEnvironment()->shouldBeAnInstanceOf(Environment::class);
}

function it_should_return_suite()
{
$this->getSuite()->shouldBeAnInstanceOf(Suite::class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

namespace spec\Drupal\DrupalExtension\Manager;

use Behat\Mink\Element\DocumentElement;
use Behat\Mink\Mink;
use Behat\Mink\Session;
use Drupal\DrupalDriverManagerInterface;
use Drupal\DrupalExtension\Manager\DrupalAuthenticationManager;
use Drupal\DrupalExtension\Manager\DrupalUserManagerInterface;
use PhpSpec\ObjectBehavior;

class DrupalAuthenticationManagerSpec extends ObjectBehavior
{
function let(Mink $mink, DrupalUserManagerInterface $userManager, DrupalDriverManagerInterface $driverManager, Session $session)
{
$mink->getSession(null)->willReturn($session);
$this->beConstructedWith($mink, $userManager, $driverManager, [], []);
}

function it_is_initializable()
{
$this->shouldHaveType(DrupalAuthenticationManager::class);
}

function it_can_check_login_status(Session $session, DocumentElement $page)
{
$this->loggedIn()->shouldBe(false);

$page->has('css', '.a-class')->willReturn(true);
$session->isStarted()->willReturn(true);
$session->getPage()->willReturn($page);
$this->setDrupalParameters(['selectors' => ['logged_in_selector' => '.a-class']]);
$this->loggedIn()->shouldBe(true);
}
}
61 changes: 61 additions & 0 deletions spec/Drupal/DrupalExtension/Manager/DrupalUserManagerSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
<?php

namespace spec\Drupal\DrupalExtension\Manager;

use Drupal\DrupalExtension\Manager\DrupalUserManager;
use PhpSpec\ObjectBehavior;

class DrupalUserManagerSpec extends ObjectBehavior
{
function it_is_initializable()
{
$this->shouldHaveType(DrupalUserManager::class);
}

function it_can_set_and_get_the_current_user()
{
$user = new \stdClass();
$user->name = 'some_name';
$this->setCurrentUser($user);
$this->getCurrentUser()->shouldBe($user);
}

function it_can_add_and_remove_users()
{
$user = new \stdClass();
$user->name = 'some_name';
$this->addUser($user);
$this->getUser('some_name')->shouldBe($user);
$this->removeUser('some_name');
$this->shouldThrow(\InvalidArgumentException::class)->duringGetUser('some_name');
}

function it_can_get_all_registered_users()
{
$this->hasUsers()->shouldBe(false);
$user = new \stdClass();
$user->name = 'some_name';
$this->addUser($user);
$this->hasUsers()->shouldBe(true);
$this->getUsers()->shouldBe(['some_name' => $user]);
}

function it_can_determine_anonymous_users()
{
$this->currentUserIsAnonymous()->shouldBe(true);
$user = new \stdClass();
$user->name = 'some_name';
$this->setCurrentUser($user);
$this->currentUserIsAnonymous()->shouldBe(false);
}

function it_can_check_roles()
{
$this->currentUserHasRole('some_role')->shouldBe(false);
$user = new \stdClass();
$user->name = 'some_name';
$user->role = 'some_role';
$this->setCurrentUser($user);
$this->currentUserHasRole('some_role')->shouldBe(true);
}
}
13 changes: 6 additions & 7 deletions spec/Drupal/DrupalExtension/Selector/RegionSelectorSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@

namespace spec\Drupal\DrupalExtension\Selector;

use Behat\Mink\Selector\SelectorInterface;
use Behat\Mink\Selector\CssSelector;

use Drupal\DrupalExtension\Selector\RegionSelector;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class RegionSelectorSpec extends ObjectBehavior
{
Expand All @@ -15,22 +13,23 @@ function let(CssSelector $selector)
$regionMap = array(
'Left sidebar' => '#left-sidebar',
);
$selector->translateToXPath('#left-sidebar')->willReturn('some xpath');

$this->beConstructedWith($selector, $regionMap);
}

function it_is_initializable()
{
$this->shouldHaveType('Drupal\DrupalExtension\Selector\RegionSelector');
$this->shouldHaveType(RegionSelector::class);
}

function it_should_translate_to_xpath()
{
// @todo this is not returning properly for some reason.
$xpath = $this->translateToXPath('Left sidebar');
$this->translateToXPath('Left sidebar')->shouldBe('some xpath');
}

function it_should_not_accept_invalid_regions()
{
$this->shouldThrow('\InvalidArgumentException')->duringTranslateToXPath('Invalid region');
$this->shouldThrow(\InvalidArgumentException::class)->duringTranslateToXPath('Invalid region');
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@

namespace spec\Drupal\DrupalExtension\ServiceContainer;

use Behat\Testwork\ServiceContainer\Extension;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;

class DrupalExtensionSpec extends ObjectBehavior
{
function it_is_a_testwork_extension()
{
$this->shouldHaveType('Behat\Testwork\ServiceContainer\Extension');
$this->shouldHaveType(Extension::class);
}

function it_is_named_drupal()
Expand Down
4 changes: 2 additions & 2 deletions src/Drupal/DrupalExtension/Context/DrupalAwareInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use Behat\Behat\Context\Context;
use Behat\Testwork\Hook\HookDispatcher;

use Drupal\DrupalDriverManager;
use Drupal\DrupalDriverManagerInterface;
use Drupal\DrupalExtension\Manager\DrupalAuthenticationManagerInterface;
use Drupal\DrupalExtension\Manager\DrupalUserManagerInterface;

Expand All @@ -15,7 +15,7 @@ interface DrupalAwareInterface extends Context
/**
* Sets Drupal instance.
*/
public function setDrupal(DrupalDriverManager $drupal);
public function setDrupal(DrupalDriverManagerInterface $drupal);

/**
* Set event dispatcher.
Expand Down
Loading

0 comments on commit c159228

Please sign in to comment.