Basic authentication behat.yml dosyası içinde tanımlanan "symfony2" session ile yapılır. Daha fazla bilgi için buraya tıklayın. Aşağıdaki örnek security.yml dosyası içinde tanımlanan in_memory security versiyonu üzerine kurulmuştur, bu nedenle kullanıcı ekranda çıkacak olan login kutusu aracılığı ile login olur.


Security.yml


Aşağıdaki ayarlaya bağlı olarak, tüm kullanıcılar ana sayfayı görebilirler ama sadece basic kullanıcılar login işleminden sonra "/country" sayfasını görebilirler.


security:
encoders:
Symfony\Component\Security\Core\User\User:
algorithm: bcrypt
cost: 12

providers:
in_memory:
memory:
users:
basic:
password: $2a$12$Mnp6eUx1AJ5YABwmprcu/OHo21klIpQ/PA7D7PdKx5SA4urdav6/e #basic
roles: ROLE_USER

firewalls:
dev:
pattern: ^/(_(profiler|wdt|error)|css|images|js)/
security: false

default:
anonymous: ~
http_basic: ~ #Pops up a login popup in browser

role_hierarchy:
ROLE_ADMIN: ROLE_USER

access_control:
- { path: ^/country, role: ROLE_USER }
- { path: ^/, role: IS_AUTHENTICATED_ANONYMOUSLY }

Behat.yml


default:
extensions:
Behat\Symfony2Extension: ~
Behat\MinkExtension:
base_url: http://football.local/app_test.php
sessions:
symfony2:
symfony2: ~
suites:
frontend:
type: symfony_bundle
bundle: ApplicationFrontendBundle
mink_session: symfony2
contexts:
- Application\FrontendBundle\Features\Context\FeatureContext: ~

Feature file


Feature: Dummy feature

Scenario: Home uri
Given I am on "/"
Then I should see "Welcome home"

Scenario: Country uri
Given I am authenticated with username "basic" password "basic"
When I am on "/country"
And I should see "Welcome to country"

FeatureContext


namespace Application\FrontendBundle\Features\Context;

use Behat\MinkExtension\Context\MinkContext;

class FeatureContext extends MinkContext implements KernelAwareContext
{
/**
* @Given /^I am authenticated with username "([^"]*)" password "([^"]*)"$/
*/
public function iAmAuthenticatedWith($uid, $psw)
{
$this->getSession()->setBasicAuth($uid, $psw);
}
}

Test


Inanzzz-MacBook-Pro:football inanzzz$ bin/behat --suite=frontend
Feature: Dummy feature

Scenario: Home uri
Given I am on "/"
Then I should see "Welcome home"

Scenario: Country uri
Given I am authenticated with username "basic" password "basic"
When I am on "/country"
And I should see "Welcome to country"

2 scenarios (2 passed)
5 steps (5 passed)
0m0.53s (27.10Mb)

Headers


Non-authenticated



Authenticated