Basic authentication works with "symfony2" session which is defined in behat.yml file. For more info, click here. Example below uses in_memory style symfony security.yml file so login box pops up on the screen for user to login.


Security.yml


Based on the settings below, everyone can see the main page but only basic user can see page "/country" after login.


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