Skip to content

Instantly share code, notes, and snippets.

@OskarStark
Last active June 21, 2018 07:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save OskarStark/5a3ce2bab27998a32a458377650abaa2 to your computer and use it in GitHub Desktop.
Save OskarStark/5a3ce2bab27998a32a458377650abaa2 to your computer and use it in GitHub Desktop.
Behat Contexts for https://github.com/liuggio/fastest (+ API)
<?php
namespace App\Tests\Behat\Context;
use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behatch\Context\RestContext;
use Lexik\Bundle\JWTAuthenticationBundle\Services\JWTManager;
use Sonata\UserBundle\Model\UserManagerInterface;
use Symfony\Component\Security\Core\User\UserInterface;
class ApiContext implements Context
{
/**
* @var UserManagerInterface
*/
protected $userManager;
/**
* @var JWTManager
*/
protected $jwtManager;
/**
* @var RestContext
*/
protected $restContext;
public function __construct(UserManagerInterface $userManager, JWTManager $jwtManager)
{
$this->userManager = $userManager;
$this->jwtManager = $jwtManager;
}
/**
* @BeforeScenario
*/
public function gatherContexts(BeforeScenarioScope $scope)
{
$environment = $scope->getEnvironment();
$this->restContext = $environment->getContext(RestContext::class);
}
/**
* @Given /^(?:|I )request the API "([^"]*)" via "([^"]*)"$/
*/
public function iRequestTheApiWith($url, $method)
{
if (getenv('ENV_TEST_CHANNEL_READABLE')) {
$this->restContext->iAddHeaderEqualTo('X-FASTEST-ENV-TEST-CHANNEL-READABLE', getenv('ENV_TEST_CHANNEL_READABLE'));
}
$this->restContext->iAddHeaderEqualTo('Content-Type', 'application/ld+json');
$this->restContext->iAddHeaderEqualTo('Accept', 'application/ld+json');
$this->restContext->iSendARequestTo($method, $url);
}
/**
* @Given /^(?:|I )am an authenticated User without API permissions$/
*/
public function iAmAnAuthenticatedUserWithoutApiPermissions()
{
$user = $this->createUser('ROLE_ADMIN');
$this->setToken($user);
}
/**
* @Given /^(?:|I )am an authenticated API-User$/
*/
public function iAmAnAuthenticatedApiUser()
{
$user = $this->createUser('ROLE_API_USER');
$this->setToken($user);
}
private function createUser(string $role): UserInterface
{
$user = $this->userManager->createUser();
$user
->setEmail('api@tausendkind.de')
->setUsername('api@tausendkind.de')
->setPlainPassword('password')
->setRoles([$role])
->setEnabled(true);
$this->userManager->updateUser($user);
return $user;
}
/**
* @param UserInterface $user
*/
private function setToken(UserInterface $user)
{
$token = $this->jwtManager->create($user);
$this->restContext->iAddHeaderEqualTo('Authorization', 'Bearer '.$token);
if (getenv('ENV_TEST_CHANNEL_READABLE')) {
$this->restContext->iAddHeaderEqualTo('X-FASTEST-ENV-TEST-CHANNEL-READABLE', getenv('ENV_TEST_CHANNEL_READABLE'));
}
}
}
<?php
namespace App\Tests\Behat\Context;
use Behat\Behat\Context\Context;
use Behat\Behat\Hook\Scope\BeforeScenarioScope;
use Behat\MinkExtension\Context\MinkContext;
class FastestContext implements Context
{
/**
* @var MinkContext
*/
protected $minkContext;
/**
* @BeforeScenario
*/
public function gatherContexts(BeforeScenarioScope $scope)
{
$environment = $scope->getEnvironment();
$this->minkContext = $environment->getContext(MinkContext::class);
}
/**
* @BeforeScenario
*/
public function addFastestChannelInformation()
{
if (getenv('ENV_TEST_CHANNEL_READABLE')) {
$this->minkContext->visit('/');
$this->minkContext->getSession()->setCookie('ENV_TEST_CHANNEL_READABLE', getenv('ENV_TEST_CHANNEL_READABLE'));
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment