Created
October 24, 2012 19:51
-
-
Save elvan/3948398 to your computer and use it in GitHub Desktop.
Example of Behat feature context for Zend Framework 2 application
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
<?php | |
use Behat\Behat\Context\Step\Given; | |
use Behat\Behat\Context\Step\Then; | |
use Behat\Behat\Context\Step\When; | |
use Behat\Behat\Event\SuiteEvent; | |
use Behat\Gherkin\Node\TableNode; | |
use Behat\MinkExtension\Context\MinkContext; | |
use Zend\Db\Adapter\Adapter; | |
/** | |
* Features context. | |
*/ | |
class FeatureContext extends MinkContext { | |
private $application; | |
private $serviceManager; | |
private $config; | |
private $adapter; | |
private $project; | |
private $user; | |
private $ticket; | |
/** | |
* Initializes context. | |
* Every scenario gets it's own context object. | |
* | |
* @param array $parameters context parameters (set them up through behat.yml) | |
*/ | |
public function __construct(array $parameters) { | |
// Initialize your context here | |
include 'init_autoloader.php'; | |
$this->application = Zend\Mvc\Application::init(include 'config/test_application.config.php'); | |
$this->serviceManager = $this->application->getServiceManager(); | |
$this->config = array_merge_recursive(include 'config/autoload/test_global.php', include 'config/autoload/test_local.php'); | |
$this->adapter = new Zend\Db\Adapter\Adapter($this->config['db']); | |
} | |
/** | |
* @BeforeSuite | |
*/ | |
public static function prepareSchema(SuiteEvent $event) { | |
$config = array_merge_recursive(include 'config/autoload/test_global.php', include 'config/autoload/test_local.php'); | |
$adapter = new Zend\Db\Adapter\Adapter($config['db']); | |
$adapter->query(file_get_contents('data/zentickets_schema.sql'), Adapter::QUERY_MODE_EXECUTE); | |
} | |
/** | |
* @AfterScenario | |
*/ | |
public function truncateTables($event) { | |
$this->adapter->query("TRUNCATE TABLE `zt_project`;", Adapter::QUERY_MODE_EXECUTE); | |
$this->adapter->query("TRUNCATE TABLE `zt_ticket`;", Adapter::QUERY_MODE_EXECUTE); | |
$this->adapter->query("TRUNCATE TABLE `zt_user`;", Adapter::QUERY_MODE_EXECUTE); | |
} | |
/** | |
* @AfterSuite | |
*/ | |
public static function populateSamples(SuiteEvent $event) { | |
$config = array_merge_recursive(include 'config/autoload/test_global.php', include 'config/autoload/test_local.php'); | |
$adapter = new Zend\Db\Adapter\Adapter($config['db']); | |
$adapter->query(file_get_contents('data/zentickets_data.sql'), Adapter::QUERY_MODE_EXECUTE); | |
} | |
/** | |
* @Given /^I am signed in as "([^"]*)" with password "([^"]*)"$/ | |
*/ | |
public function iAmSignedInAsWithPassword($email, $password) { | |
return array( | |
new Given('I am on homepage'), | |
new When('I fill in "signin_email" with "' . $email . '"'), | |
new When('I fill in "signin_password" with "' . $password . '"'), | |
new When('I press "Sign In"'), | |
new Then('I should be on "/"'), | |
new Then('I should see "' . $email . '" in the "#nav_header_email" element'), | |
); | |
} | |
/** | |
* @Given /^there are the following users:$/ | |
*/ | |
public function thereAreTheFollowingUsers(TableNode $table) { | |
$hash = $table->getHash(); | |
foreach ($hash as $row) { | |
$user = new Tracker\Model\User; | |
$user->first_name = $row['first_name']; | |
$user->last_name = $row['last_name']; | |
$user->email = $row['email']; | |
$user->create_password = $row['password']; | |
$user->confirm_password = $row['password']; | |
$user->confirmed = $row['confirmed']; | |
$user->role = $row['role']; | |
$user->active = 'TRUE'; | |
$this->serviceManager->get('Tracker\Model\UserTable')->save($user); | |
$this->user = $user; | |
} | |
} | |
/** | |
* @Given /^there is a project with the following:$/ | |
*/ | |
public function thereIsAProjectWithTheFollowing(TableNode $table) { | |
$hash = $table->getHash(); | |
foreach ($hash as $row) { | |
$project = new Tracker\Model\Project; | |
$project->name = $row['name']; | |
$project->description = $row['description']; | |
$this->serviceManager->get('Tracker\Model\ProjectTable')->save($project); | |
$this->project = $project; | |
} | |
} | |
/** | |
* @Given /^"([^"]*)" has created a ticket for this project:$/ | |
*/ | |
public function hasCreatedATicketForThisProject($email, TableNode $table) { | |
$hash = $table->getHash(); | |
$user = $this->serviceManager->get('Tracker\Model\UserTable')->findByEmail($email); | |
foreach ($hash as $row) { | |
$ticket = new Tracker\Model\Ticket; | |
$ticket->title = $row['title']; | |
$ticket->description = $row['description']; | |
$ticket->project_id = $this->project->id; | |
$ticket->user_id = $user->id; | |
$this->serviceManager->get('Tracker\Model\TicketTable')->save($ticket); | |
$this->ticket = $ticket; | |
} | |
} | |
/** | |
* @Given /^"(?P<email>[^"]*)" follow the confirmation link in the email$/ | |
*/ | |
public function followTheConfirmationLinkInTheEmail($email) { | |
$user = $this->serviceManager->get('Tracker\Model\UserTable')->findByEmail($email); | |
if ($user) { | |
$this->user = $user; | |
$this->visit('/user/confirm-email/' . $user->email . '/' . $user->confirmation_token); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment