Last active
June 21, 2018 07:48
-
-
Save OskarStark/5a3ce2bab27998a32a458377650abaa2 to your computer and use it in GitHub Desktop.
Behat Contexts for https://github.com/liuggio/fastest (+ API)
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 | |
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('[email protected]') | |
->setUsername('[email protected]') | |
->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')); | |
} | |
} | |
} |
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 | |
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