Skip to content

[PHP-Config] Update main configuration#192

Merged
acoulton merged 1 commit intoBehat:v3.0from
loic425:main-configuration-with-php-config
Mar 17, 2025
Merged

[PHP-Config] Update main configuration#192
acoulton merged 1 commit intoBehat:v3.0from
loic425:main-configuration-with-php-config

Conversation

@loic425
Copy link
Copy Markdown
Contributor

@loic425 loic425 commented Mar 11, 2025

Related to #184
I've tested my examples with that PHPUnit file.

<?php

declare(strict_types=1);

namespace Behat\Tests\Config;

use Behat\Config\Config;
use Behat\Config\Extension;
use Behat\Config\Filter\TagFilter;
use Behat\Config\Formatter\PrettyFormatter;
use Behat\Config\Profile;
use Behat\Config\Suite;
use PHPUnit\Framework\TestCase;

final class ExamplesTest extends TestCase
{
    public function testExample1(): void
    {
        $config = (new Config())
            ->withProfile(
                new Profile('default')
            );

        $this->assertEquals([
            'default' => [],
        ], $config->toArray());
    }

    public function testExample2(): void
    {
        $defaultSuite = (new Suite('default'))
            ->withFilter(new TagFilter('@runthisonlyondefault'))
        ;

        $config = (new Config())
            ->withProfile(
                (new Profile('default'))
                    ->withSuite($defaultSuite)
            )
        ;

        $this->assertEquals([
            'default' => [
                'suites' => [
                    'default' => [
                        'filters' => [
                            'tags' => '@runthisonlyondefault'
                        ],
                    ],
                ],
            ],
        ], $config->toArray());
    }

    public function testExample3(): void
    {
        $defaultSuite = (new Suite('default'))
            ->withFilter(new TagFilter('@runthisonlyondefault'))
        ;

        $profile1DefaultSuite = (new Suite('default'))
            ->withFilter(new TagFilter('@runthisonlyonprofile1'))
        ;

        $config = (new Config())
            ->withProfile(
                (new Profile('default'))
                    ->withSuite($defaultSuite)
            )
            ->withProfile(
                (new Profile('profile1'))
                    ->withSuite($profile1DefaultSuite)
            )
        ;

        $this->assertEquals([
            'default' => [
                'suites' => [
                    'default' => [
                        'filters' => [
                            'tags' => '@runthisonlyondefault'
                        ],
                    ],
                ],
            ],
            'profile1' => [
                'suites' => [
                    'default' => [
                        'filters' => [
                            'tags' => '@runthisonlyonprofile1'
                        ],
                    ],
                ],
            ],
        ], $config->toArray());
    }

    public function testExample4(): void
    {
        $defaultSuite = (new Suite('default'))
            ->withFilter(new TagFilter('@runthisonlyondefault'))
        ;

        $profile1DefaultSuite = new Suite('default', ['filters' => null]);

        $config = (new Config())
            ->withProfile(
                (new Profile('default'))
                    ->withSuite($defaultSuite)
            )
            ->withProfile(
                (new Profile('profile1'))
                    ->withSuite($profile1DefaultSuite)
            )
        ;

        $this->assertEquals([
            'default' => [
                'suites' => [
                    'default' => [
                        'filters' => [
                            'tags' => '@runthisonlyondefault'
                        ],
                    ],
                ],
            ],
            'profile1' => [
                'suites' => [
                    'default' => [
                        'filters' => null,
                    ],
                ],
            ],
        ], $config->toArray());
    }

    public function testImportingConfig(): void
    {
        $config = (new Config())
            ->import([
                'config/base.behat.php',
                'config/ci.behat.php',
            ])
        ;

        $this->assertEquals([
            'imports' => [
                'config/base.behat.php',
                'config/ci.behat.php',
            ],
        ], $config->toArray());
    }

    public function testGlobalConfigurationExample1(): void
    {
        $config = (new Config())
            ->withProfile(
                new Profile('default', [
                    'testers' => [
                        'stop_on_failure' => false,
                        'strict' => false,
                    ],
                ])
            )
        ;

        $this->assertEquals([
            'default' => [
                'testers' => [
                    'stop_on_failure' => false,
                    'strict' => false,
                ],
            ],
        ], $config->toArray());
    }

    public function testGlobalConfigurationExample2(): void
    {
        $config = (new Config())
            ->withProfile(
                new Profile('default', [
                    'testers' => [
                        'stop_on_failure' => true,
                        'strict' => false,
                    ],
                ])
            )
            ->withProfile(
                new Profile('ci', [
                    'testers' => [
                        'stop_on_failure' => false,
                        'strict' => true,
                    ],
                ])
            )
        ;

        $this->assertEquals([
            'default' => [
                'testers' => [
                    'stop_on_failure' => true,
                    'strict' => false,
                ],
            ],
            'ci' => [
                'testers' => [
                    'stop_on_failure' => false,
                    'strict' => true,
                ],
            ],
        ], $config->toArray());
    }

    public function testGlobalFilters(): void
    {
        $config = (new Config())
            ->withProfile(
                (new Profile('default'))
                    ->withFilter(new TagFilter('~@wip'))
            )
        ;

        $this->assertEquals([
            'default' => [
                'gherkin' => [
                    'filters' => [
                        'tags' => '~@wip',
                    ],
                ],
            ],
        ], $config->toArray());
    }

    public function testCustomAutoloadingExample1(): void
    {
        $config = (new Config())
            ->withProfile(
                (new Profile('default', [
                    'autoload' => [
                        '' => '%paths.base%/app/features/bootstrap',
                    ],
                ]))
            )
        ;

        $this->assertEquals([
            'default' => [
                'autoload' => [
                    '' => '%paths.base%/app/features/bootstrap',
                ],
            ],
        ], $config->toArray());
    }

    public function testCustomAutoloadingExample2(): void
    {
        $defaultProfile = new Profile('default', [
            'autoload' => [
                '' => '%paths.base%/app/features/bootstrap',
            ],
        ]);

        $defaultProfile->withSuite(
            (new Suite('default'))
                ->withContexts('My\Application\Namespace\Bootstrap\FeatureContext')
        );

        $config = (new Config())
            ->withProfile($defaultProfile)
        ;

        $this->assertEquals([
            'default' => [
                'autoload' => [
                    '' => '%paths.base%/app/features/bootstrap',
                ],
                'suites' => [
                    'default' => [
                        'contexts' => ['My\Application\Namespace\Bootstrap\FeatureContext'],
                    ],
                ],
            ],
        ], $config->toArray());
    }

    public function testCustomAutoloadingExample3(): void
    {
        $defaultProfile = (new Profile('default'))
            ->withSuite(
                (new Suite('default'))
                    ->withContexts('My\Application\Namespace\Bootstrap\FeatureContext')
            )
        ;

        $config = (new Config())
            ->withProfile($defaultProfile)
        ;

        $this->assertEquals([
            'default' => [
                'suites' => [
                    'default' => [
                        'contexts' => ['My\Application\Namespace\Bootstrap\FeatureContext'],
                    ],
                ],
            ],
        ], $config->toArray());
    }

    public function testFormatters(): void
    {
        $config = (new Config())
            ->withProfile(
                (new Profile('default'))
                    ->withFormatter(
                        new PrettyFormatter()
                    )
            )
        ;

        $this->assertEquals([
            'default' => [
                'formatters' => [
                    'pretty' => [
                        'timer' => true,
                        'expand' => false,
                        'paths' => true,
                        'multiline' => true,
                    ],
                ],
            ],
        ], $config->toArray());
    }

    public function testExtensions(): void
    {
        $config = (new Config())
            ->withProfile(
                (new Profile('default'))
                    ->withExtension(
                        new Extension('Behat\MinkExtension', [
                            'base_url' => 'http://www.example.com',
                            'selenium2' => '~',
                        ])
                    )
            )
        ;

        $this->assertEquals([
            'default' => [
                'extensions' => [
                    'Behat\MinkExtension' => [
                        'base_url' => 'http://www.example.com',
                        'selenium2' => '~',
                    ],
                ],
            ],
        ], $config->toArray());
    }
}

@loic425 loic425 marked this pull request as draft March 11, 2025 08:08
@loic425 loic425 force-pushed the main-configuration-with-php-config branch 3 times, most recently from a16b034 to 9087da1 Compare March 11, 2025 08:26
@loic425
Copy link
Copy Markdown
Contributor Author

loic425 commented Mar 11, 2025

@acoulton @carlos-granados I'm wondering if it would be interesting to add this PHPUnit file in the Behat codebase. WDYT?

@loic425 loic425 force-pushed the main-configuration-with-php-config branch from 9087da1 to 8356b57 Compare March 14, 2025 09:20
@loic425 loic425 marked this pull request as ready for review March 14, 2025 09:20
@loic425 loic425 force-pushed the main-configuration-with-php-config branch 2 times, most recently from f1c4ed2 to 596ac49 Compare March 14, 2025 09:24
Copy link
Copy Markdown
Contributor

@carlos-granados carlos-granados left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking good, just a small comment

return new Config()
->withProfile(
new Profile('default')
)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In the previous example there was a line with #... indicating where you would add additional configuration, maybe we could do the same here with a //... line after the new Profile line

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done :)

@loic425 loic425 force-pushed the main-configuration-with-php-config branch from 596ac49 to 6a6ab53 Compare March 17, 2025 08:55
Copy link
Copy Markdown
Contributor

@acoulton acoulton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great, thanks @loic425

Comment thread user_guide/configuration.rst Outdated
>withExtension(
new Extension('Behat\MinkExtension', [
'base_url' => 'http://www.example.com',
'selenium2' => '~',
Copy link
Copy Markdown
Contributor

@acoulton acoulton Mar 17, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually, shouldn't this be null rather than the '~' string?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should. An unquoted ~ in Yaml is a null value.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, I think so too. I'm gonna change that.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

Copy link
Copy Markdown
Contributor

@acoulton acoulton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just need to review the selenium ~/ null setting

@acoulton
Copy link
Copy Markdown
Contributor

@acoulton @carlos-granados I'm wondering if it would be interesting to add this PHPUnit file in the Behat codebase. WDYT?

It wouldn't hurt to add more unit test coverage for the php config, but this looks like it overlaps a bit with some of the tests that are already there at https://github.com/Behat/Behat/tree/master/tests/Behat/Tests/Config ? Unless I'm misunderstanding?

@loic425 loic425 force-pushed the main-configuration-with-php-config branch from 6a6ab53 to 08ec8a3 Compare March 17, 2025 09:25
Copy link
Copy Markdown
Contributor

@acoulton acoulton left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @loic425

@acoulton acoulton merged commit 7734b7b into Behat:v3.0 Mar 17, 2025
4 checks passed
@loic425 loic425 deleted the main-configuration-with-php-config branch March 17, 2025 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants