Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for specification of a preferred profile #1334

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,11 @@ All notable changes to this project will be documented in this file.
The format is based on [Keep a Changelog](http://keepachangelog.com/)
and this project adheres to [Semantic Versioning](http://semver.org/).

## [Unreleased]

### Added
* [1334](https://github.com/Behat/Behat/pull/1334]): Add support for a preferred profile ([@andrewnicols](https://github.com/andrewnicols))

## [3.8.1] - 2020-11-07

### Fixed
Expand Down
144 changes: 144 additions & 0 deletions features/preferred_profile.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,144 @@
Feature: Preferred Profiles
In order to test my preferred profile
As a tester
I need to be able to specify a preferred profile

Background:
Given a file named "features/bootstrap/FeatureContext.php" with:
"""
<?php

use Behat\Behat\Context\Context;

class FeatureContext implements Context
{
private $result;
private $numbers;

/**
* @Given /I have basic calculator/
*/
public function iHaveBasicCalculator() {
$this->result = 0;
$this->numbers = array();
}

/**
* @Given /I have entered (\d+)/
*/
public function iHaveEntered($number) {
$this->numbers[] = intval($number);
}

/**
* @When /I add/
*/
public function iAdd() {
$this->result = array_sum($this->numbers);
$this->numbers = array();
}

/**
* @When /I sub/
*/
public function iSub() {
$this->result = array_shift($this->numbers);
$this->result -= array_sum($this->numbers);
$this->numbers = array();
}

/**
* @Then /The result should be (\d+)/
*/
public function theResultShouldBe($result) {
PHPUnit\Framework\Assert::assertEquals($result, $this->result);
}
}
"""
And a file named "features/math.feature" with:
"""
Feature: Math
Background:
Given I have basic calculator

Scenario Outline:
Given I have entered <number1>
And I have entered <number2>
When I add
Then The result should be <result>

Examples:
| number1 | number2 | result |
| 10 | 12 | 22 |
| 5 | 3 | 8 |
| 5 | 5 | 10 |
"""
And a file named "pretty.yml" with:
"""
pretty_without_paths:
formatters:
progress: false
pretty:
paths: false

"""
And a file named "behat.yml" with:
"""
default:

progress:
formatters:
progress: true
pretty: false

preferredProfileName:
progress

imports:
- pretty.yml
"""

Scenario:
Given I run "behat --no-colors features/math.feature"
Then it should pass with:
"""
...............

3 scenarios (3 passed)
15 steps (15 passed)
"""

Scenario:
Given I run "behat --no-colors features/math.feature --profile progress"
Then it should pass with:
"""
...............

3 scenarios (3 passed)
15 steps (15 passed)
"""

Scenario:
Given I run "behat --no-colors --profile pretty_without_paths"
Then it should pass with:
"""
Feature: Math

Background:
Given I have basic calculator

Scenario Outline:
Given I have entered <number1>
And I have entered <number2>
When I add
Then The result should be <result>

Examples:
| number1 | number2 | result |
| 10 | 12 | 22 |
| 5 | 3 | 8 |
| 5 | 5 | 10 |

3 scenarios (3 passed)
15 steps (15 passed)
"""
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,8 @@ private function loadConfigs($basePath, array $config, $profile)
{
$configs = array();

$profile = $this->getProfileName($config, $profile);

// first load default profile from current config, but only if custom profile requested
if ('default' !== $profile && isset($config['default'])) {
$configs[] = $config['default'];
Expand All @@ -229,9 +231,33 @@ private function loadConfigs($basePath, array $config, $profile)
$this->profileFound = true;
}

if (!$this->profileFound && isset($config['preferredProfileName'])) {
throw new ConfigurationLoadingException(sprintf(
'Can not find configuration for `%s` profile.',
$config['preferredProfileName']
));
}

return $configs;
}

/**
* Get the name of the requested profile, after considering any preferred profile name.
*
* @param array $config
* @param string $profile
*
* @return string
*/
private function getProfileName(array $config, string $profile): string
{
if (isset($config['preferredProfileName']) && 'default' === $profile) {
return $config['preferredProfileName'];
}

return $profile;
}

/**
* Loads all provided imports.
*
Expand Down