Skip to content

Commit

Permalink
initial version
Browse files Browse the repository at this point in the history
  • Loading branch information
tom00 committed Dec 13, 2024
0 parents commit 20b6f99
Show file tree
Hide file tree
Showing 14 changed files with 328 additions and 0 deletions.
7 changes: 7 additions & 0 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
Copyright 2024 Kiwee Software sp. z o.o.

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 changes: 20 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# Kiwee Demo Shop Plugin

The plugin renders a bar on the top of the page with the text `This is a demo shop`.
The message can be changed via the administration panel at `Extensions > My Extensions > Kiwee Demo Plugin > (Configure)`.

## Installation

```shell
composer require kiwee/shopware-demo-plugin && \
bin/console plugin:install KiweeDemo --activate
```

## Running integration tests
```shell
composer exec phpunit -- -c vendor/kiwee/shopware-demo-plugin/phpunit.xml
```

## License
This project is licensed under the MIT License.
Copyright 2024 [Kiwee](https://kiwee.eu) sp. z o.o.
29 changes: 29 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
{
"name": "kiwee/shopware-demo-plugin",
"description": "KiweeDemo",
"authors": [
{
"name": "Kiwee Software sp. z o.o.",
"email": "hi@kiwee.eu",
"homepage": "https://kiwee.eu"
}
],
"type": "shopware-platform-plugin",
"license": "MIT",
"version": "0.0.2",
"require": {
"shopware/core": "~6.6.0"
},
"autoload": {
"psr-4": {
"Kiwee\\KiweeDemo\\": "src/"
}
},
"extra": {
"shopware-plugin-class": "Kiwee\\KiweeDemo\\KiweeDemo",
"label": {
"de-DE": "Kiwee Demo Plugin",
"en-GB": "Kiwee Demo plugin"
}
}
}
23 changes: 23 additions & 0 deletions phpunit.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?xml version="1.0" encoding="UTF-8"?>
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/10.4/phpunit.xsd" bootstrap="tests/TestBootstrap.php" executionOrder="random">
<php>
<ini name="error_reporting" value="-1"/>
<env name="APP_ENV" value="test" />
<server name="KERNEL_CLASS" value="Shopware\Core\Kernel"/>
<server name="APP_ENV" value="test"/>
<env name="APP_DEBUG" value="1"/>
<env name="APP_SECRET" value="s$cretf0rt3st"/>
<env name="SHELL_VERBOSITY" value="-1"/>
<env name="SYMFONY_DEPRECATIONS_HELPER" value="weak"/>
</php>
<testsuites>
<testsuite name="all">
<directory>./tests</directory>
</testsuite>
</testsuites>
<coverage>
<include>
<directory suffix=".php">./src/</directory>
</include>
</coverage>
</phpunit>
9 changes: 9 additions & 0 deletions src/KiweeDemo.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?php declare(strict_types=1);

namespace Kiwee\KiweeDemo;

use Shopware\Core\Framework\Plugin;

class KiweeDemo extends Plugin
{
}
4 changes: 4 additions & 0 deletions src/Resources/app/storefront/src/scss/base.scss
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.topbar {
background-color: goldenrod;
width: 100%;
}
13 changes: 13 additions & 0 deletions src/Resources/config/config.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://raw.githubusercontent.com/shopware/platform/master/src/Core/System/SystemConfig/Schema/config.xsd">

<card>
<title>Kiwee Demo Configuration</title>
<input-field>
<name>topbar</name>
<label>Topbar Text</label>
<defaultValue>This is a demo shop.</defaultValue>
</input-field>
</card>
</config>
13 changes: 13 additions & 0 deletions src/Resources/config/services.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="Kiwee\KiweeDemo\Storefront\Pagelet\Header\HeaderPageletEventSubscriber">
<argument type="service" id="Shopware\Core\System\SystemConfig\SystemConfigService"/>
<tag name="kernel.event_subscriber"/>
</service>
</services>
</container>
13 changes: 13 additions & 0 deletions src/Resources/config/services_test.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?xml version="1.0" ?>

<container xmlns="http://symfony.com/schema/dic/services"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://symfony.com/schema/dic/services http://symfony.com/schema/dic/services/services-1.0.xsd">

<services>
<service id="Kiwee\KiweeDemo\Storefront\Pagelet\Header\HeaderPageletEventSubscriber">
<argument type="service" id="Shopware\Core\System\SystemConfig\SystemConfigService"/>
<tag name="kernel.event_subscriber"/>
</service>
</services>
</container>
6 changes: 6 additions & 0 deletions src/Resources/views/storefront/base.html.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{% sw_extends '@Storefront/storefront/base.html.twig' %}

{% block base_header_inner %}
<div class="topbar">&nbsp;{{ page.header.extensions.topbar.topbarText }}</div>
{{ parent() }}
{% endblock %}
30 changes: 30 additions & 0 deletions src/Storefront/Pagelet/Header/HeaderPageletEventSubscriber.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<?php
declare(strict_types=1);
namespace Kiwee\KiweeDemo\Storefront\Pagelet\Header;

use Shopware\Storefront\Pagelet\Header\HeaderPageletLoadedEvent;
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
use Shopware\Core\System\SystemConfig\SystemConfigService;

class HeaderPageletEventSubscriber implements EventSubscriberInterface
{

public function __construct(private SystemConfigService $systemConfigService)
{
}

public function addTopbarText(HeaderPageletLoadedEvent $event): void
{
$value = $this->systemConfigService->get('KiweeDemo.config.topbar');
$topbarConfig = new TopbarConfig();
$topbarConfig->setTopbarText($value);
$event->getPagelet()->addExtension('topbar', $topbarConfig);
}

public static function getSubscribedEvents(): array
{
return [
HeaderPageletLoadedEvent::class => 'addTopbarText'
];
}
}
26 changes: 26 additions & 0 deletions src/Storefront/Pagelet/Header/TopbarConfig.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
<?php
declare(strict_types=1);

namespace Kiwee\KiweeDemo\Storefront\Pagelet\Header;
use Shopware\Core\Framework\Struct\Struct;

class TopbarConfig extends Struct
{
protected string $topbarText;

/**
* @return string
*/
public function getTopbarText(): string
{
return $this->topbarText;
}

/**
* @param string $topbarText
*/
public function setTopbarText(string $topbarText): void
{
$this->topbarText = $topbarText;
}
}
122 changes: 122 additions & 0 deletions tests/Storefront/Pagelet/Header/HeaderPageletEventSubscriberTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,122 @@
<?php
namespace Kiwee\KiweeDemo\Test\Storefront\Pagelet\Header;

use PHPUnit\Framework\TestCase;
use Shopware\Core\Checkout\Cart\CartRuleLoader;
use Shopware\Core\Defaults;
use Shopware\Core\Framework\Api\Util\AccessKeyHelper;
use Shopware\Core\Framework\Context;
use Shopware\Core\Framework\DataAbstractionLayer\EntityRepository;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Criteria;
use Shopware\Core\Framework\DataAbstractionLayer\Search\Filter\EqualsFilter;
use Shopware\Core\Framework\Test\TestCaseBase\IntegrationTestBehaviour;
use Shopware\Core\Framework\Uuid\Uuid;
use Shopware\Core\System\SalesChannel\Context\SalesChannelContextFactory;
use Shopware\Core\System\SalesChannel\SalesChannelCollection;
use Shopware\Core\System\SalesChannel\SalesChannelContext;
use Shopware\Core\System\SystemConfig\SystemConfigService;
use Shopware\Core\Test\TestDefaults;
use Shopware\Storefront\Page\GenericPageLoader;
use Symfony\Component\HttpFoundation\Request;

class HeaderPageletEventSubscriberTest extends TestCase
{
use IntegrationTestBehaviour;

public function testAddTopbarTextDefaultValue()
{
$context = $this->createSalesChannelContext();
$request = new Request();
$page = $this->getPageLoader()->load($request, $context);

$this->assertEquals('This is a demo shop.',
$page->getHeader()->getExtensions()['topbar']->getTopbarText()
);
}

/**
* @return void
* @depends testAddTopbarTextDefaultValue
*/
public function testAddTopbarTextModifiedValue()
{
$context = $this->createSalesChannelContext();
$request = new Request();
$systemConfigService = $this->getContainer()->get(SystemConfigService::class);
$systemConfigService->set('KiweeDemo.config.topbar', 'This is not a demo shop.');
$page = $this->getPageLoader()->load($request, $context);

$this->assertEquals('This is not a demo shop.',
$page->getHeader()->getExtensions()['topbar']->getTopbarText()
);
}

protected function createSalesChannel(array $salesChannelOverride = []): array
{
/** @var EntityRepository<SalesChannelCollection> $salesChannelRepository */
$salesChannelRepository = static::getContainer()->get('sales_channel.repository');
$paymentMethod = $this->getAvailablePaymentMethod();

$criteria = new Criteria();
$criteria->addFilter(new EqualsFilter('domains.url', 'http://localhost'));
$salesChannelIds = $salesChannelRepository->searchIds($criteria, Context::createDefaultContext());

if (!isset($salesChannelOverride['domains']) && $salesChannelIds->firstId() !== null) {
$salesChannelRepository->delete([['id' => $salesChannelIds->firstId()]], Context::createDefaultContext());
}

$salesChannel = array_replace_recursive([
'id' => $salesChannelOverride['id'] ?? Uuid::randomHex(),
'typeId' => Defaults::SALES_CHANNEL_TYPE_STOREFRONT,
'name' => 'API Test case sales channel',
'accessKey' => AccessKeyHelper::generateAccessKey('sales-channel'),
'languageId' => Defaults::LANGUAGE_SYSTEM,
'snippetSetId' => $this->getSnippetSetIdForLocale('en-GB'),
'currencyId' => Defaults::CURRENCY,
'paymentMethodId' => $paymentMethod->getId(),
'paymentMethods' => [['id' => $paymentMethod->getId()]],
'shippingMethodId' => $this->getAvailableShippingMethod()->getId(),
'navigationCategoryId' => $this->getValidCategoryId(),
'countryId' => $this->getValidCountryId(null),
'currencies' => [['id' => Defaults::CURRENCY]],
'languages' => $salesChannelOverride['languages'] ?? [['id' => Defaults::LANGUAGE_SYSTEM]],
'customerGroupId' => TestDefaults::FALLBACK_CUSTOMER_GROUP,
'domains' => [
[
'languageId' => Defaults::LANGUAGE_SYSTEM,
'currencyId' => Defaults::CURRENCY,
'snippetSetId' => $this->getSnippetSetIdForLocale('en-GB'),
'url' => 'http://localhost',
],
],
'countries' => [['id' => $this->getValidCountryId(null)]],
], $salesChannelOverride);

$salesChannelRepository->upsert([$salesChannel], Context::createDefaultContext());

return $salesChannel;
}

protected function getPageLoader(): GenericPageLoader
{
return $this->getContainer()->get(GenericPageLoader::class);
}

private function createSalesChannelContext(): SalesChannelContext
{
$salesChannel = $this->createSalesChannel();

return $this->createContext($salesChannel);
}

private function createContext(array $salesChannel): SalesChannelContext
{
$context = static::getContainer()->get(SalesChannelContextFactory::class)
->create(Uuid::randomHex(), $salesChannel['id']);

$ruleLoader = static::getContainer()->get(CartRuleLoader::class);
$ruleLoader->loadByToken($context, $context->getToken());

return $context;
}
}
13 changes: 13 additions & 0 deletions tests/TestBootstrap.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<?php

declare(strict_types=1);

use Shopware\Core\TestBootstrapper;

$loader = (new TestBootstrapper())
->setForceInstallPlugins(true)
->addCallingPlugin()
->bootstrap()
->getClassLoader();

$loader->addPsr4('Kiwee\KiweeDemo\\Test\\', __DIR__);

0 comments on commit 20b6f99

Please sign in to comment.