Skip to content

Instantly share code, notes, and snippets.

@JoppeDC
Last active March 27, 2024 07:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JoppeDC/c1614031e574cb075aaf52cca417718a to your computer and use it in GitHub Desktop.
Save JoppeDC/c1614031e574cb075aaf52cca417718a to your computer and use it in GitHub Desktop.
Testing sylius resources with behat - Example Context and Pages
<?php
namespace App\Behat\Context\Admin;
use Behat\Behat\Context\Context;
class BrandContext implements Context
{
public function __construct(
private readonly CreatePage $createPage,
private readonly IndexPage $indexPage,
#[Autowire(service: 'sylius.behat.notification_checker')]
private readonly NotificationCheckerInterface $notificationChecker
) {
}
/**
* @When I want to create a new brand
*/
public function iWantToCreateANewBrand()
{
$this->createPage->open();
}
/**
* @When I specify its name as :name
*/
public function iSpecifyItsNameAs(string $name)
{
$this->createPage->nameIt($name);
}
/**
* @When I add it
*/
public function iAddIt()
{
$this->createPage->add();
}
/**
* @Then I should be notified that it has been successfully created
*/
public function iShouldBeNotifiedThatItHasBeenSuccessfullyCreated()
{
$this->notificationChecker->checkNotification('Brand has been successfully created.', NotificationType::success());
}
/**
* @Then the brand :brand should appear in the registry
*/
public function theBrandShouldAppearInTheRegistry(string $brand)
{
$this->indexPage->open();
Assert::true($this->indexPage->isSingleResourceOnPage(['name' => $brand]));
}
}
@managing_brands
Feature: Managing brands
In order to configure my store
As a store owner
I want to be able to manage brands
Background:
Given the store is available in "English (United States)"
And I am logged in as an administrator
@ui
Scenario: Adding a new brand
When I want to create a new brand
And I specify its name as "Nike"
And I add it
Then I should be notified that it has been successfully created
And the brand "nike" should appear in the registry
<?php
namespace App\Behat\Page\Admin\Brand;
use Sylius\Behat\Page\Admin\Crud\CreatePage as BaseCreatePage;
final class CreatePage extends BaseCreatePage
{
public function nameIt(string $name): void
{
$this->getDocument()->fillField('app_brand_name', $name);
}
public function add()
{
$this->getDocument()->pressButton('Create');
}
}
<?php
namespace App\Behat\Page\Admin\Brand;
use Sylius\Behat\Page\Admin\Crud\IndexPage as BaseIndexPage;
class IndexPage extends BaseIndexPage {}
services:
App\Behat\Page\Admin\Brand\CreatePage:
parent: sylius.behat.page.admin.crud.create
public: false
arguments:
- "app_admin_brand.brand_create" # This is the route of the page
App\Behat\Page\Admin\Brand\IndexPage:
parent: sylius.behat.page.admin.crud.index
public: false
arguments:
- "app_admin_brand.brand_index"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment