Skip to content

Commit

Permalink
OP-237 - write behat tests, mock api responses, create examples of re…
Browse files Browse the repository at this point in the history
…nderers
  • Loading branch information
BartoszWojdalowicz committed Feb 23, 2024
1 parent bbd2147 commit 254ffc9
Show file tree
Hide file tree
Showing 46 changed files with 1,585 additions and 3 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
},
"require-dev": {
"behat/behat": "^3.6.1",
"behat/mink-selenium2-driver": "^1.4",
"behat/mink-selenium2-driver": "~1.6.0",
"bitbag/coding-standard": "^3.0",
"dmore/behat-chrome-extension": "^1.3",
"dmore/chrome-mink-driver": "^2.7",
Expand Down
50 changes: 50 additions & 0 deletions features/caching_sulu_request.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
@sulu_cache
Feature: Caching sulu page request
In order to see again some page
As a Visitor
I want to be able to see the page without sending request to sulu

Background:
Given the store operates on a single channel in "United States"
And Sulu has defined page "blog_page_with_blocks_and_links" in locale "en_US"
And Cache for sulu not exists


@ui
Scenario: See the featured pages on homepage
When I visit this channel's homepage
And "United States" has enabled sulu localized requests
Then Sulu cache should not exists
And I visit a sulu page "blog_page_with_blocks_and_links" in locale en_US
And Sulu cache should exists for "United States" with locale en_US

@ui
Scenario: Manually clear cache when localized urls are disabled
When I visit a sulu page "blog_page_with_blocks_and_links" in locale en_US
Then Sulu cache should exists
And I am logged in as an administrator
And I browse channels
And I should see button "Purge sulu cache" in "United States"
And I click "Clear Cache" button in "United States"
And I should see success "Successfully purged" flash message
And Sulu cache should exists for "United States" with locale en_US

@ui
Scenario: Manually clear cache when localized urls are enabled
When "United States" has enabled sulu localized requests
And I visit a sulu page "blog_page_with_blocks_and_links" in locale en_US
Then Sulu cache should exists
And I am logged in as an administrator
And I browse channels
And I should see expanded button "Purge sulu cache" in "United States"
And I click expanded "en_US" button in "United States"
And I should see success "Successfully purged" flash message
And Sulu cache should exists for "United States" with locale en_US

@ui
Scenario: Manually clear cache when localized urls are disabled and cache not exists
When I am logged in as an administrator
And I browse channels
Then I should see button "Purge sulu cache" in "United States"
And I click "Purge sulu cache" button in "United States"
And I should see error "Dir with sulu cache not found" flash message
27 changes: 27 additions & 0 deletions features/render_sulu_page.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
@render_sulu_page
Feature: Render sulu page
In order to see a sulu page
As a Visitor
I want to be able to see rendered sulu page

Background:
Given the store operates on a single channel in "United States"
And Sulu has defined page "blog_page_with_properties" in locale "en_US"
And Sulu has defined page "blog_page_with_blocks_and_links" in locale "en_US"
And Page "blog_page_with_blocks_and_links" has block "quote"
And Page "blog_page_with_blocks_and_links" has block "text"
And Page "blog_page_with_blocks_and_links" has block "image"

@ui
Scenario: Rendering a sulu page with properties
When I visit a sulu page "blog_page_with_properties" in locale en_US
Then I should see a "title" with value "E-commerce trends"
And I should see a "content" with value "CONTENT"

@ui
Scenario: Rendering a sulu page with properties and blocks
When I visit a sulu page "blog_page_with_blocks_and_links" in locale en_US
Then I should see a "title" with value "E-commerce trends"
And I should see a block "content" with value "2021 was followed by the time of the 2020 pandemic. During these two years, a lot has changed..."
And I should see a block image with url "https://en.wikipedia.org/wiki/Cat#/media/File:Sheba1.JPG"
And I should see a block "quote" with value "Lorem ipsum dolor sit amet, con"
20 changes: 20 additions & 0 deletions features/show_featured_pages_on_homepage.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
@sulu_page
Feature: Show featured pages on homepage
In order to see featured pages on homepage
As a Visitor
I want to be able to see featured pages on homepage

Background:
Given the store operates on a single channel in "United States"
And Sulu has defined featured pages list featured_pages in locale en_US
And One of the featured page is page "Blog Page 1"
And One of the featured page is page "Blog Page 2"
And One of the featured page is page "Blog Page 3"

@ui
Scenario: See the featured pages on homepage
When I visit this channel's homepage
Then I should see 3 featured pages.
And I should see featured page "Blog Page 1"
And I should see featured page "Blog Page 2"
And I should see featured page "Blog Page 3"
10 changes: 10 additions & 0 deletions src/Entity/SuluChannelConfigurationTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,18 @@

namespace BitBag\SyliusSuluPlugin\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
* @ORM\Entity
*
* @ORM\Table(name="sylius_channel")
*/
#[ORM\Entity]
#[ORM\Table(name: 'sylius_channel')]
trait SuluChannelConfigurationTrait
{
/** @ORM\Column(type="boolean", nullable=false) */
protected bool $suluUseLocalizedUrls = false;

public function isSuluUseLocalizedUrls(): bool
Expand Down
1 change: 1 addition & 0 deletions src/Twig/Extension/SuluPageExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ public function getFunctions(): array
new TwigFunction('bitbag_render_sulu_blocks', [$this->suluRuntime, 'renderSuluBlocks']),
new TwigFunction('bitbag_render_sulu_blocks_with_type', [$this->suluRuntime, 'renderSuluBlocksWithType']),
new TwigFunction('bitbag_render_sulu_block_with_type', [$this->suluRuntime, 'renderSuluBlockWithType']),
new TwigFunction('bitbag_page_has_sulu_block', [$this->suluRuntime, 'hasSuluBlock']),
];
}
}
19 changes: 18 additions & 1 deletion src/Twig/Runtime/SuluRuntime.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,10 +59,10 @@ public function renderSuluBlocksWithType(array $blocks, string $type, ?string $d
public function renderSuluBlockWithType(array $blocks, string $type): string
{
$blocks = array_filter($blocks, fn (array $block) => $block['type'] === $type);

if (count($blocks) > 1) {
$blocks = $blocks[0];
}

$content = '';

foreach ($blocks as $block) {
Expand All @@ -71,4 +71,21 @@ public function renderSuluBlockWithType(array $blocks, string $type): string

return $content;
}

public function hasSuluBlock(array $page, string $type): bool
{
if (!array_key_exists('blocks', $page)) {
return false;
}

$blocks = $page['blocks'];

if (count($blocks) === 0) {
return false;
}

$blocks = array_filter($blocks, fn (array $block) => $block['type'] === $type);

return count($blocks) !== 0;
}
}
2 changes: 2 additions & 0 deletions src/Twig/Runtime/SuluRuntimeInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,6 @@ public function renderSuluBlocks(array $blocks): string;
public function renderSuluBlocksWithType(array $blocks, string $type): string;

public function renderSuluBlockWithType(array $blocks, string $type): string;

public function hasSuluBlock(array $page, string $type): bool;
}
2 changes: 1 addition & 1 deletion templates/Admin/Grid/Action/invalidateSuluCache.html.twig
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{% if true == data.isSuluUseLocalizedUrls %}
<div class="ui labeled icon floating dropdown link button">
<div class="ui labeled icon floating dropdown link button select-locale">
<i class="trash icon"></i>
<span class="text">{{ action.label|trans }}</span>
<div class="menu">
Expand Down
4 changes: 4 additions & 0 deletions tests/Application/.env
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,7 @@ SYLIUS_MESSENGER_TRANSPORT_MAIN_FAILED_DSN=doctrine://default?queue_name=main_fa
SYLIUS_MESSENGER_TRANSPORT_CATALOG_PROMOTION_REMOVAL_DSN=doctrine://default?queue_name=catalog_promotion_removal
SYLIUS_MESSENGER_TRANSPORT_CATALOG_PROMOTION_REMOVAL_FAILED_DSN=doctrine://default?queue_name=catalog_promotion_removal_failed
###< symfony/messenger ###

###< sulu ###
SULU_BASE_URI=https://127.0.0.1:8001/
###< sulu ###
2 changes: 2 additions & 0 deletions tests/Application/config/bundles.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
<?php

declare(strict_types=1);

return [
Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
Symfony\Bundle\MonologBundle\MonologBundle::class => ['all' => true],
Expand Down
12 changes: 12 additions & 0 deletions tests/Application/config/doctrine/Channel.Channel.orm.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
<?xml version="1.0" encoding="UTF-8"?>

<doctrine-mapping
xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping
http://doctrine-project.org/schemas/orm/doctrine-mapping.xsd"
>
<entity name="Tests\BitBag\SyliusSuluPlugin\Application\src\Entity\Channel\Channel" table="sylius_channel">
<field name="suluUseLocalizedUrls" column="sulu_use_localized_url" type="boolean"/>
</entity>
</doctrine-mapping>
32 changes: 32 additions & 0 deletions tests/Application/config/packages/_sylius.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,35 @@ sylius_shop:

sylius_api:
enabled: true

sylius_channel:
resources:
channel:
classes:
model: Tests\BitBag\SyliusSuluPlugin\Application\src\Entity\Channel\Channel


sylius_ui:
events:
sylius.shop.homepage:
blocks:
featured_pages:
template: "@SyliusShop/Homepage/_featuredPages.html.twig"
priority: 20
sylius_grid:
grids:
sylius_admin_channel:
actions:
item:
invalidate_sulu_cache:
type: invalidate_sulu_cache
label: bitbag.sulu_plugin.purge_channel_cache
icon: sync
options:
link:
route: bitbag_purge_sulu_cache_for_channel
parameters:
id: resource.id
templates:
action:
invalidate_sulu_cache: "@BitBagSyliusSuluPlugin/Admin/Grid/Action/invalidateSuluCache.html.twig"
11 changes: 11 additions & 0 deletions tests/Application/config/packages/doctrine.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,14 @@ doctrine:
charset: UTF8

url: '%env(resolve:DATABASE_URL)%'

orm:
auto_generate_proxy_classes: '%kernel.debug%'
naming_strategy: doctrine.orm.naming_strategy.underscore
auto_mapping: true
mappings:
App:
is_bundle: false
type: xml
dir: '%kernel.project_dir%/config/doctrine'
prefix: 'Tests\BitBag\SyliusSuluPlugin\Application\src\Entity'
5 changes: 5 additions & 0 deletions tests/Application/config/packages/framework.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,8 @@ framework:
serializer:
mapping:
paths: [ '%kernel.project_dir%/config/serialization' ]
cache:
pools:
sulu:
adapter: cache.adapter.filesystem
default_lifetime: 3600
12 changes: 12 additions & 0 deletions tests/Application/config/routes/sylius_admin.yaml
Original file line number Diff line number Diff line change
@@ -1,3 +1,15 @@
sylius_admin:
resource: "@SyliusAdminBundle/Resources/config/routing.yml"
prefix: /admin

bitbag_purge_sulu_cache_for_channel:
path: /invalidate-sulu-cache/{id}/{locale}
methods: [GET]
prefix: /admin
defaults:
locale: ''
_controller: bitbag.sylius_sulu_plugin.controller.action.purge_sulu_cache_action
_sylius:
section: admin
redirect: referer
permission: true
16 changes: 16 additions & 0 deletions tests/Application/config/routes/sylius_shop.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,19 @@ sylius_shop_default_locale:
methods: [GET]
defaults:
_controller: sylius.controller.shop.locale_switch::switchAction

bitbag_render_sulu_page:
path: /{locale}/page/{slug}
methods: [GET]
defaults:
_controller: bitbag.sylius_sulu_plugin.controller.action.render_page_action
_sylius:
permission: false

bitbag_render_sulu_sub_page:
path: /{locale}/page/{slug}/{second_slug}
methods: [GET]
defaults:
_controller: bitbag.sylius_sulu_plugin.controller.action.render_page_action
_sylius:
permission: false
2 changes: 2 additions & 0 deletions tests/Application/config/services.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@
# https://symfony.com/doc/current/best_practices/configuration.html#application-related-configuration
parameters:
locale: en_US
bitbag.sulu_base_uri: "%env(string:SULU_BASE_URI)%"
bitbag.sulu_cache_dir: "%kernel.cache_dir%/sulu"
30 changes: 30 additions & 0 deletions tests/Application/config/services_test.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,33 @@ imports:
# see https://github.com/FriendsOfBehat/SymfonyExtension/issues/88
services:
Symfony\Component\BrowserKit\AbstractBrowser: '@test.client'

tests.bitbag.sylius_sulu_plugin.application.src.renderer.page.featured_pages_renderer:
class: Tests\BitBag\SyliusSuluPlugin\Application\src\Renderer\Page\FeaturedPagesRenderer
arguments: [ '@twig' ]
tags:
- { name: bitbag.sylius_sulu_plugin.strategy.page_renderer}

tests.bit_bag.sylius_sulu_plugin.application.src.renderer.page.blog_page_renderer:
class: Tests\BitBag\SyliusSuluPlugin\Application\src\Renderer\Page\BlogPageRenderer
arguments: [ '@twig' ]
tags:
- { name: bitbag.sylius_sulu_plugin.strategy.page_renderer }

tests.bitbag.sylius_sulu_plugin.application.src.renderer.block.text_renderer:
class: Tests\BitBag\SyliusSuluPlugin\Application\src\Renderer\Block\TextRenderer
arguments: [ '@twig' ]
tags:
- { name: bitbag.sylius_sulu_plugin.strategy.block_renderer }

tests.bitbag.sylius_sulu_plugin.application.src.renderer.block.quote_renderer:
class: Tests\BitBag\SyliusSuluPlugin\Application\src\Renderer\Block\QuoteRenderer
arguments: [ '@twig' ]
tags:
- { name: bitbag.sylius_sulu_plugin.strategy.block_renderer }

tests.bitbag.sylius_sulu_plugin.application.src.renderer.block.image_renderer:
class: Tests\BitBag\SyliusSuluPlugin\Application\src\Renderer\Block\ImageRenderer
arguments: [ '@twig' ]
tags:
- { name: bitbag.sylius_sulu_plugin.strategy.block_renderer }
13 changes: 13 additions & 0 deletions tests/Application/src/Entity/Channel/Channel.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

namespace Tests\BitBag\SyliusSuluPlugin\Application\src\Entity\Channel;

use BitBag\SyliusSuluPlugin\Entity\SuluChannelConfigurationTrait;
use Sylius\Component\Core\Model\Channel as BaseChannel;

class Channel extends BaseChannel
{
use SuluChannelConfigurationTrait;
}
Loading

0 comments on commit 254ffc9

Please sign in to comment.