-
Notifications
You must be signed in to change notification settings - Fork 71
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #66 from 10up/feature/behat-tests
First set of behavioral tests
- Loading branch information
Showing
8 changed files
with
519 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
default: | ||
suites: | ||
default: | ||
contexts: | ||
- FeatureContext | ||
- HooksContext | ||
- FunctionsContext | ||
formatters: | ||
progress: true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,68 @@ | ||
<?php | ||
|
||
use Behat\Behat\Context\Context; | ||
use Behat\Behat\Context\SnippetAcceptingContext; | ||
use Behat\Behat\Hook\Scope\AfterScenarioScope; | ||
use Behat\Behat\Hook\Scope\BeforeScenarioScope; | ||
|
||
/** | ||
* Defines application features from the specific context. | ||
*/ | ||
class FeatureContext implements Context, SnippetAcceptingContext | ||
{ | ||
/** | ||
* Initializes context. | ||
* | ||
* Every scenario gets its own context instance. | ||
* You can also pass arbitrary arguments to the | ||
* context constructor through behat.yml. | ||
*/ | ||
public function __construct() | ||
{ | ||
} | ||
|
||
/** | ||
* @BeforeScenario | ||
*/ | ||
public function setUpWpMock(BeforeScenarioScope $scope) | ||
{ | ||
WP_Mock::setUp(); | ||
} | ||
|
||
/** | ||
* @AfterScenario | ||
*/ | ||
public function tearDownWpMock(AfterScenarioScope $scope) | ||
{ | ||
WP_Mock::tearDown(); | ||
} | ||
|
||
/** | ||
* @Then tearDown should not fail | ||
*/ | ||
public function teardownShouldNotFail() | ||
{ | ||
WP_Mock::tearDown(); | ||
} | ||
|
||
/** | ||
* @When I do nothing | ||
*/ | ||
public function iDoNothing() | ||
{ | ||
// Move along... | ||
} | ||
|
||
/** | ||
* @Then tearDown should fail | ||
*/ | ||
public function teardownShouldFail() | ||
{ | ||
try { | ||
$this->teardownShouldNotFail(); | ||
throw new PHPUnit_Framework_ExpectationFailedException('WP_Mock Teardown should have failed!'); | ||
} catch (\Mockery\Exception\InvalidCountException $e) { | ||
// Move along | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
<?php | ||
|
||
use Behat\Behat\Context\Context; | ||
use Behat\Gherkin\Node\TableNode; | ||
use Mockery\Exception\NoMatchingExpectationException; | ||
|
||
class FunctionsContext implements Context | ||
{ | ||
|
||
/** | ||
* @Given function :function does not exist | ||
*/ | ||
public function functionDoesNotExist($function) | ||
{ | ||
PHPUnit_Framework_Assert::assertFalse(function_exists($function)); | ||
} | ||
|
||
/** | ||
* @Given I mock passthru function :function with args: | ||
*/ | ||
public function iMockPassthruFunctionWithArgs($function, TableNode $args) | ||
{ | ||
WP_Mock::passthruFunction($function, array( | ||
'args' => $args->getRow(0), | ||
)); | ||
} | ||
|
||
/** | ||
* @Given I mock function :function to return :value | ||
*/ | ||
public function iMockFunctionToReturn($function, $value) | ||
{ | ||
WP_Mock::userFunction($function, array('return' => $value)); | ||
} | ||
|
||
/** | ||
* @When I mock function :function | ||
*/ | ||
public function iMockFunction($function) | ||
{ | ||
WP_Mock::userFunction($function); | ||
} | ||
|
||
/** | ||
* @Then function :function should exist | ||
*/ | ||
public function functionShouldExist($function) | ||
{ | ||
PHPUnit_Framework_Assert::assertTrue(function_exists($function)); | ||
} | ||
|
||
/** | ||
* @Then I expect :return when I run :function with args: | ||
*/ | ||
public function iExpectWhenIRunWithArgs($return, $function, TableNode $args) | ||
{ | ||
PHPUnit_Framework_Assert::assertEquals($return, call_user_func_array($function, $args->getRow(0))); | ||
} | ||
|
||
/** | ||
* @Then I expect :return when I run :function | ||
*/ | ||
public function iExcpectWhenIRun($return, $function) | ||
{ | ||
$this->iExpectWhenIRunWithArgs($return, $function, new TableNode(array(array()))); | ||
} | ||
|
||
/** | ||
* @Then I expect an error when I run :function with args: | ||
*/ | ||
public function iExpectAnErrorWhenIRunWithArgs($function, TableNode $args) | ||
{ | ||
try { | ||
$this->iExpectWhenIRunWithArgs(null, $function, $args); | ||
} catch (NoMatchingExpectationException $e) { | ||
// Move along... | ||
} | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,197 @@ | ||
<?php | ||
|
||
use Behat\Behat\Context\Context; | ||
use Behat\Behat\Hook\Scope\AfterScenarioScope; | ||
use Behat\Behat\Hook\Scope\BeforeScenarioScope; | ||
use Behat\Gherkin\Node\TableNode; | ||
|
||
class HooksContext implements Context | ||
{ | ||
|
||
private $filterResults = array(); | ||
|
||
/** | ||
* @BeforeScenario | ||
*/ | ||
public function setUpWpMock(BeforeScenarioScope $scope) | ||
{ | ||
$this->filterResults = array(); | ||
} | ||
|
||
/** | ||
* @AfterScenario | ||
*/ | ||
public function tearDownWpMock(AfterScenarioScope $scope) | ||
{ | ||
$this->filterResults = array(); | ||
} | ||
|
||
/** | ||
* @Given I expect the following actions added: | ||
*/ | ||
public function iExpectTheFollowingActionsAdded(TableNode $table) | ||
{ | ||
foreach ($this->getActionsWithDefaults($table) as $action) { | ||
WP_Mock::expectActionAdded( | ||
$action['action'], | ||
$action['callback'], | ||
$action['priority'], | ||
$action['arguments'] | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @Given I expect the :action action | ||
*/ | ||
public function iExpectTheAction($action) | ||
{ | ||
$this->iExpectTheActionWith($action, new TableNode(array())); | ||
} | ||
|
||
/** | ||
* @When I expect the :action action with: | ||
*/ | ||
public function iExpectTheActionWith($action, TableNode $table) | ||
{ | ||
$args = array($action); | ||
$rows = $table->getRows(); | ||
if (isset( $rows[0] ) && is_array($rows[0])) { | ||
$args = array_merge($args, $rows[0]); | ||
} | ||
call_user_func_array(array('WP_Mock', 'expectAction'), $args); | ||
} | ||
|
||
/** | ||
* @When I add the following actions: | ||
*/ | ||
public function iAddTheFollowingActions(TableNode $table) | ||
{ | ||
foreach ($this->getActionsWithDefaults($table) as $action) { | ||
add_action( | ||
$action['action'], | ||
$action['callback'], | ||
$action['priority'], | ||
$action['arguments'] | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @When I do the :action action | ||
*/ | ||
public function iDoTheAction($action) | ||
{ | ||
$this->iDoTheActionWith($action, new TableNode(array())); | ||
} | ||
|
||
/** | ||
* @When I do the :action action with: | ||
*/ | ||
public function iDoTheActionWith($action, TableNode $table) | ||
{ | ||
$args = array($action); | ||
$rows = $table->getRows(); | ||
if (isset( $rows[0] ) && is_array($rows[0])) { | ||
$args = array_merge($args, $rows[0]); | ||
} | ||
call_user_func_array('do_action', $args); | ||
} | ||
|
||
/** | ||
* @Given I expect the following filters added: | ||
*/ | ||
public function iExpectTheFollowingFiltersAdded(TableNode $table) | ||
{ | ||
$filters = $table->getHash(); | ||
$defaults = array( | ||
'filter' => '', | ||
'callback' => '', | ||
'priority' => 10, | ||
'arguments' => 1, | ||
); | ||
foreach ($filters as $filter) { | ||
$filter += $defaults; | ||
WP_Mock::expectFilterAdded( | ||
$filter['filter'], | ||
$filter['callback'], | ||
$filter['priority'], | ||
$filter['arguments'] | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @When I add the following filters: | ||
*/ | ||
public function iAddTheFollowingFilters(TableNode $table) | ||
{ | ||
$filters = $table->getHash(); | ||
$defaults = array( | ||
'filter' => '', | ||
'callback' => '', | ||
'priority' => 10, | ||
'arguments' => 1, | ||
); | ||
foreach ($filters as $filter) { | ||
$filter += $defaults; | ||
add_filter( | ||
$filter['filter'], | ||
$filter['callback'], | ||
$filter['priority'], | ||
$filter['arguments'] | ||
); | ||
} | ||
} | ||
|
||
/** | ||
* @Given I expect filter :filter to respond to :thing with :response | ||
*/ | ||
public function iExpectFilterToRespondToWith($filter, $thing, $response) | ||
{ | ||
WP_Mock::onFilter($filter)->with($thing)->reply($response); | ||
} | ||
|
||
/** | ||
* @Given I expect filter :filter to respond with :response | ||
*/ | ||
public function iExpectFilterToRespondWith($filter, $response) | ||
{ | ||
$this->iExpectFilterToRespondToWith($filter, null, $response); | ||
} | ||
|
||
/** | ||
* @When I apply the filter :filter with :with | ||
*/ | ||
public function iApplyFilterWith($filter, $with) | ||
{ | ||
$this->filterResults[$filter] = apply_filters($filter, $with); | ||
} | ||
|
||
/** | ||
* @Then The filter :filter should return :value | ||
*/ | ||
public function theFilterShouldReturn($filter, $value) | ||
{ | ||
PHPUnit_Framework_Assert::assertArrayHasKey($filter, $this->filterResults); | ||
PHPUnit_Framework_Assert::assertEquals($this->filterResults[$filter], $value); | ||
} | ||
|
||
private function getActionsWithDefaults(TableNode $table) | ||
{ | ||
$actions = $table->getHash(); | ||
$defaults = array( | ||
'action' => '', | ||
'callback' => '', | ||
'priority' => 10, | ||
'arguments' => 1, | ||
); | ||
foreach ($actions as &$action) { | ||
$action += $defaults; | ||
} | ||
unset( $action ); | ||
|
||
return $actions; | ||
} | ||
|
||
} |
Oops, something went wrong.