Skip to content

Commit

Permalink
OP-545: Behat
Browse files Browse the repository at this point in the history
  • Loading branch information
jkindly committed Sep 20, 2024
1 parent f1ad2ce commit a12fc54
Show file tree
Hide file tree
Showing 24 changed files with 278 additions and 94 deletions.
6 changes: 3 additions & 3 deletions features/admin/adding_block.feature
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,12 @@ Feature: Adding blocks
Then I should be notified that "Code, Name" fields are too long

@ui @javascript
Scenario: Adding block with template
Given there is an existing template named "Homepage" with "Block" type that contains "Textarea, Single media" content elements
Scenario: Adding block with content template
Given there is an existing content template named "Homepage" with "Block" type that contains "Textarea, Single media" content elements
When I go to the create block page
And I fill the code with "intro"
And I fill the name with "Intro"
And I select "Homepage" template
And I select "Homepage" content template
And I confirm that I want to use this template
And I add it
Then I should be notified that the block has been created
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@managing_templates
@managing_content_templates
Feature: Adding cms templates
In order to create templates
As an Administrator
Expand Down
21 changes: 18 additions & 3 deletions features/admin/adding_page.feature
Original file line number Diff line number Diff line change
Expand Up @@ -210,15 +210,30 @@ Feature: Adding new page
And I should see newly created "Multiple media" content element in Content elements section

@ui @javascript
Scenario: Adding page with template
Given there is an existing template named "Homepage" with "Page" type that contains "Textarea, Single media" content elements
Scenario: Adding page with content template
Given there is an existing content template named "Homepage" with "Page" type that contains "Textarea, Single media" content elements
When I go to the create page page
And I fill the code with "my_page"
And I fill the slug with "my_page"
And I fill the name with "My page"
And I select "Homepage" template
And I select "Homepage" content template
And I confirm that I want to use this template
And I add it
Then I should be notified that the page has been created
And I should see newly created "Textarea" content element in Content elements section
And I should see newly created "Single media" content element in Content elements section

@ui @javascript
Scenario: Adding page with with a custom template
Given there is an existing template with "@SyliusCmsPlugin/Shop/Page/custom.html.twig" value
When I go to the create page page
And I fill the code with "my_page"
And I fill the slug with "my-page"
And I fill the name with "My page"
And I select "United States" channel
And I select "@SyliusCmsPlugin/Shop/Page/custom.html.twig" template
And I add it
Then I should be notified that the page has been created
And I go to the "my-page" page
And The rendered page should contain custom layout code

Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
@managing_templates
@managing_content_templates
Feature: Managing cms templates
In order to manage existing templates
As an Administrator
Expand Down
2 changes: 1 addition & 1 deletion features/admin/managing_pages.feature
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Feature: Managing cms pages
@ui
Scenario: Deleting page
Given there is a page in the store
When I go to the pages page
When I go to the cms pages page
And I delete this page
Then I should be notified that the page has been deleted
And I should see empty list of pages
Expand Down
52 changes: 52 additions & 0 deletions spec/Provider/ResourceTemplateProviderSpec.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
<?php

declare(strict_types=1);

namespace spec\Sylius\CmsPlugin\Provider;

use PhpSpec\ObjectBehavior;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Sylius\CmsPlugin\Provider\ResourceTemplateProvider;
use Sylius\CmsPlugin\Provider\ResourceTemplateProviderInterface;

final class ResourceTemplateProviderSpec extends ObjectBehavior
{
public function let(ParameterBagInterface $parameterBag): void
{
$parameterBag->get('sylius_cms.templates.pages')->willReturn([
'@CustomTemplate/Page.html.twig' => '@CustomTemplate/Page.html.twig',
]);

$parameterBag->get('sylius_cms.templates.blocks')->willReturn([
'@CustomTemplate/Block.html.twig' => '@CustomTemplate/Block.html.twig',
]);

$this->beConstructedWith($parameterBag);
}

public function it_is_initializable(): void
{
$this->shouldHaveType(ResourceTemplateProvider::class);
}

public function it_implements_resource_template_provider_interface(): void
{
$this->shouldImplement(ResourceTemplateProviderInterface::class);
}

public function it_returns_default_and_custom_page_templates(): void
{
$this->getPageTemplates()->shouldReturn([
'sylius.ui.default' => '@SyliusCmsPlugin/Shop/Page/show.html.twig',
'@CustomTemplate/Page.html.twig' => '@CustomTemplate/Page.html.twig',
]);
}

public function it_returns_default_and_custom_block_templates(): void
{
$this->getBlockTemplates()->shouldReturn([
'sylius.ui.default' => '@SyliusCmsPlugin/Shop/Block/show.html.twig',
'@CustomTemplate/Block.html.twig' => '@CustomTemplate/Block.html.twig',
]);
}
}
86 changes: 86 additions & 0 deletions tests/Behat/Context/Setup/ContentTemplateContext.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php

declare(strict_types=1);

namespace Tests\Sylius\CmsPlugin\Behat\Context\Setup;

use Behat\Behat\Context\Context;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\CmsPlugin\Entity\TemplateInterface;
use Sylius\CmsPlugin\Repository\TemplateRepositoryInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Tests\Sylius\CmsPlugin\Behat\Helpers\ContentElementHelper;

final class ContentTemplateContext implements Context
{
public function __construct(
private FactoryInterface $templateFactory,
private SharedStorageInterface $sharedStorage,
private TemplateRepositoryInterface $templateRepository,
) {
}

/**
* @Given there is a template in the store with :name name
* @Given there is a template in the store with :name name and :type type
*/
public function thereIsATemplate(string $name, ?string $type = null): void
{
$template = $this->createTemplate($name, $type);

$this->saveTemplate($template);
}

/**
* @Given there are :firstContentElement and :secondContentElement content elements in this template
*/
public function thereAreContentElementsInThisTemplate(string $firstContentElement, string $secondContentElement): void
{
/** @var TemplateInterface $template */
$template = $this->sharedStorage->get('template');
$template->setContentElements([
['type' => ContentElementHelper::getContentElementValueByName($firstContentElement)],
['type' => ContentElementHelper::getContentElementValueByName($secondContentElement)],
]);

$this->saveTemplate($template);
}

/**
* @Given there is an existing content template named :templateName with :type type that contains :contentElements content elements
*/
public function thereIsAnExistingTemplateThatContainsContentElements(string $templateName, string $type, string $contentElements): void
{
$template = $this->createTemplate($templateName, $type);

$contentElements = explode(', ', $contentElements);

$contentElementsArray = [];
foreach ($contentElements as $contentElement) {
$contentElementsArray[] = ['type' => ContentElementHelper::getContentElementValueByName($contentElement)];
}

$template->setContentElements($contentElementsArray);

$this->saveTemplate($template);
}

private function createTemplate(string $name, ?string $type = null): TemplateInterface
{
/** @var TemplateInterface $template */
$template = $this->templateFactory->createNew();
$template->setName($name);

if (null !== $type) {
$template->setType($type);
}

return $template;
}

private function saveTemplate(TemplateInterface $template): void
{
$this->templateRepository->add($template);
$this->sharedStorage->set('template', $template);
}
}
91 changes: 36 additions & 55 deletions tests/Behat/Context/Setup/TemplateContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,82 +5,63 @@
namespace Tests\Sylius\CmsPlugin\Behat\Context\Setup;

use Behat\Behat\Context\Context;
use Sylius\Behat\Service\SharedStorageInterface;
use Sylius\CmsPlugin\Entity\TemplateInterface;
use Sylius\CmsPlugin\Repository\TemplateRepositoryInterface;
use Sylius\Component\Resource\Factory\FactoryInterface;
use Tests\Sylius\CmsPlugin\Behat\Helpers\ContentElementHelper;
use Behat\Behat\Tester\Exception\PendingException;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Yaml\Yaml;

final class TemplateContext implements Context
{
public function __construct(
private FactoryInterface $templateFactory,
private SharedStorageInterface $sharedStorage,
private TemplateRepositoryInterface $templateRepository,
) {
private Filesystem $filesystem;
private $tempConfigFile;
private $tempTemplateFile;

public function __construct()
{
$this->filesystem = new Filesystem();
}

/**
* @Given there is a template in the store with :name name
* @Given there is a template in the store with :name name and :type type
* @Given there is an existing template with :template value
*/
public function thereIsATemplate(string $name, ?string $type = null): void
public function thereIsAnExistingTemplateWithValue($template)
{
$template = $this->createTemplate($name, $type);
$this->tempConfigFile = __DIR__ . '/../../../Application/config/packages/sylius_cms_test.yaml';
$config = [
'sylius_cms' => [
'templates' => [
'pages' => [$template]
],
],
];

$this->filesystem->dumpFile($this->tempConfigFile, Yaml::dump($config));

$this->tempTemplateFile = $this->getTemplateFilePath($template);
$dummyTemplateContent = "<div class='custom-layout'>This is a test template for: $template</div>";

$this->saveTemplate($template);
$this->filesystem->dumpFile($this->tempTemplateFile, $dummyTemplateContent);
}

/**
* @Given there are :firstContentElement and :secondContentElement content elements in this template
* Get the real template file path from the given template name.
*/
public function thereAreContentElementsInThisTemplate(string $firstContentElement, string $secondContentElement): void
private function getTemplateFilePath($template): string
{
/** @var TemplateInterface $template */
$template = $this->sharedStorage->get('template');
$template->setContentElements([
['type' => ContentElementHelper::getContentElementValueByName($firstContentElement)],
['type' => ContentElementHelper::getContentElementValueByName($secondContentElement)],
]);

$this->saveTemplate($template);
$templatePath = str_replace('@SyliusCmsPlugin', 'Application/templates/bundles/SyliusCmsPlugin', $template);
return __DIR__ . '/../../../' . $templatePath;
}

/**
* @Given there is an existing template named :templateName with :type type that contains :contentElements content elements
* @AfterScenario
*/
public function thereIsAnExistingTemplateThatContainsContentElements(string $templateName, string $type, string $contentElements): void
public function cleanup(): void
{
$template = $this->createTemplate($templateName, $type);

$contentElements = explode(', ', $contentElements);

$contentElementsArray = [];
foreach ($contentElements as $contentElement) {
$contentElementsArray[] = ['type' => ContentElementHelper::getContentElementValueByName($contentElement)];
if ($this->tempConfigFile && file_exists($this->tempConfigFile)) {
$this->filesystem->remove($this->tempConfigFile);
}

$template->setContentElements($contentElementsArray);

$this->saveTemplate($template);
}

private function createTemplate(string $name, ?string $type = null): TemplateInterface
{
/** @var TemplateInterface $template */
$template = $this->templateFactory->createNew();
$template->setName($name);

if (null !== $type) {
$template->setType($type);
if ($this->tempTemplateFile && file_exists($this->tempTemplateFile)) {
$this->filesystem->remove($this->tempTemplateFile);
}

return $template;
}

private function saveTemplate(TemplateInterface $template): void
{
$this->templateRepository->add($template);
$this->sharedStorage->set('template', $template);
}
}
6 changes: 3 additions & 3 deletions tests/Behat/Context/Ui/Admin/BlockContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -401,11 +401,11 @@ public function iShouldSeeEmptyListOfBlocks(): void
}

/**
* @Then I select :templateName template
* @Then I select :templateName content template
*/
public function iSelectTemplate(string $templateName): void
public function iSelectContentTemplate(string $templateName): void
{
$this->resolveCurrentPage()->selectTemplate($templateName);
$this->resolveCurrentPage()->selectContentTemplate($templateName);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
use Tests\Sylius\CmsPlugin\Behat\Service\RandomStringGeneratorInterface;
use Webmozart\Assert\Assert;

final class TemplateContext implements Context
final class ContentTemplateContext implements Context
{
public function __construct(
private SharedStorageInterface $sharedStorage,
Expand Down
Loading

0 comments on commit a12fc54

Please sign in to comment.