Skip to content

Commit

Permalink
Adapt cache clean logic, change some app logic to fit expected behavi…
Browse files Browse the repository at this point in the history
…or, improve docker scripts
  • Loading branch information
bytes-commerce committed Aug 2, 2023
1 parent 482e019 commit 3b68221
Show file tree
Hide file tree
Showing 10 changed files with 18 additions and 66 deletions.
20 changes: 0 additions & 20 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,6 @@ services:
env_file:
- .env

php-8.1:
build:
context: .
dockerfile: ./docker/php/Dockerfile
args:
PHP_VERSION: php:8.1.20-zts-alpine3.18
MAGENTO_PUBLIC_KEY: ${MAGENTO_PUBLIC_KEY}
MAGENTO_SECRET_KEY: ${MAGENTO_SECRET_KEY}
volumes:
- ./:/var/www/html/vendor/seec/behat-magento2-extension
- ./behat.yml:/var/www/html/behat.yml
- ./features:/var/www/html/features
- ./magento:/var/www/html/vendor/magento-ext
environment:
PHP_IDE_CONFIG: serverName=magento2-behat-extension
extra_hosts:
- "host.docker.internal:host-gateway"
env_file:
- .env

mysql:
image: mariadb:10.6
environment:
Expand Down
4 changes: 2 additions & 2 deletions docker/php/etc/fetch-magento.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

if [ ! -f /var/www/html/composer.json ]; then
rm -rf /var/www/html
composer create-project --no-install --repository=https://repo.magento.com/ magento/project-community-edition=2.4.3-p3 /var/www/html/
composer create-project --no-install --repository=https://repo.magento.com/ magento/project-community-edition=2.4.6 /var/www/html/
cd /var/www/html
composer config --no-plugins allow-plugins.magento/* true
composer config --no-plugins allow-plugins.php-http/discovery true
composer config --no-plugins allow-plugins.laminas/laminas-dependency-plugin true
composer config --no-plugins allow-plugins.dealerdirect/phpcodesniffer-composer-installer true
composer require --dev behat/behat friends-of-behat/mink-extension behat/mink-goutte-driver
composer require --dev behat/behat tkotosz/test-area-magento2
composer install
fi
2 changes: 1 addition & 1 deletion docker/php/etc/run-ecs.sh
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env sh

cd /var/www/html/behat-magento2-extension
cd /var/www/html/vendor/seec/behat-magento2-extension
php vendor/bin/ecs check src/ --fix
php vendor/bin/ecs check features/ --fix
php vendor/bin/ecs check tests/ --fix
Expand Down
1 change: 1 addition & 0 deletions docker/php/etc/run-phpstan.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#!/usr/bin/env sh

cd /var/www/html/vendor/seec/behat-magento2-extension
php vendor/bin/phpstan analyse src/ --level=8
php vendor/bin/phpstan analyse features/ --level=6
php vendor/bin/phpstan analyse tests/ --level=6
4 changes: 2 additions & 2 deletions docker/php/etc/run-tests.sh
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#!/usr/bin/env sh

cd /var/www/html/vendor/seec/behat-magento2-extension
php vendor/bin/phpunit tests/
php vendor/bin/behat --config /var/www/html/behat-magento2-extension/behat.yml --suite without_compiled_di --strict --stop-on-failure
php vendor/bin/behat --config /var/www/html/behat-magento2-extension/behat.yml --suite with_compiled_di --strict --stop-on-failure
php vendor/bin/behat --stop-on-failure
11 changes: 4 additions & 7 deletions features/bootstrap/Context/Tasks/CacheCleaner.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ final class CacheCleaner implements CacheCleanerInterface

public function __construct(
MagentoPathProviderInterface $magentoPathProvider = null,
Filesystem $filesystem = null,
FileSystem $filesystem = null,
Finder $finder = null,
) {
$this->magentoPathProvider = $magentoPathProvider ?? new MagentoPathProvider();
Expand All @@ -30,12 +30,9 @@ public function __construct(
public function clean(bool $cleanObjectManager = true): void
{
$directory = $this->magentoPathProvider->getMagentoRootDirectory();
$cacheFolder = $this->finder->in(sprintf('%s/var/cache/', $directory));
foreach ($cacheFolder as $cacheFolderItem) {
if (str_starts_with($cacheFolderItem->getRelativePathname(), '.') === false) {
$this->fileSystem->remove($cacheFolderItem->getPathname());
}
}
$this->finder->directories();
$cacheFolder = $this->finder->in(sprintf('%s/var/cache', $directory));
$this->fileSystem->remove($cacheFolder);

if ($cleanObjectManager) {
/** @var Config $objectManager */
Expand Down
5 changes: 1 addition & 4 deletions features/bootstrap/Context/Tasks/MagentoPathProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,14 @@

namespace SEEC\Behat\Magento2Extension\Features\Bootstrap\Context\Tasks;

use Webmozart\Assert\Assert;

final class MagentoPathProvider implements MagentoPathProviderInterface
{
public function getMagentoRootDirectory(): string
{
$path = __DIR__;
$maxCount = count(explode('/', $path));
$i = 0;
while (!file_exists(sprintf('%s/app/etc/env.php', $path))) {
Assert::lessThan($i++, $maxCount, 'Could not find Magento root directory');
while (!file_exists(sprintf('%s/app/etc/env.php', $path)) && $i++ < $maxCount) {
$path = sprintf('%s/..', $path);
}

Expand Down
2 changes: 1 addition & 1 deletion features/bootstrap/Context/TestRunnerContext.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ public function __construct(
$this->filesystem = $fileSystem ?: new Filesystem();
$this->processFactory = $processFactory ?: new ProcessFactory();
$this->magentoPathProvider = $magentoPathProvider ?? new MagentoPathProvider();
$this->cacheCleaner = $cacheCleaner ?? new CacheCleaner($this->magentoPathProvider, $this->filesystem);
$this->cacheCleaner = $cacheCleaner ?? new CacheCleaner($this->magentoPathProvider);
parent::__construct($fileSystem, $processFactory, $workingDirectoryService, $workingDirectory);
}

Expand Down
13 changes: 3 additions & 10 deletions src/ServiceContainer/Magento2Extension.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace SEEC\Behat\Magento2Extension\ServiceContainer;

use Behat\Testwork\ServiceContainer\ExtensionManager;
use SEEC\Behat\Magento2Extension\Features\Bootstrap\Context\Tasks\MagentoPathProvider;
use Symfony\Component\Config\Definition\Builder\ArrayNodeDefinition;
use Symfony\Component\Config\FileLocator;
use Symfony\Component\DependencyInjection\ContainerBuilder;
Expand Down Expand Up @@ -55,16 +56,8 @@ public function process(TaggedContainerInterface $container): void

private function getMagentoBootstrapPath(): string
{
$path = sprintf('%s/app/bootstrap.php', getcwd());
$prefix = getcwd() ?: '/';
$limit = count(explode('/', $prefix));
$i = 0;
while (!file_exists($path)) {
Assert::lessThan(++$i, $limit, 'Could not find Magento root directory');
$prefix = sprintf('%s/..', $prefix);
$path = sprintf('%s/app/bootstrap.php', $prefix);
}
$magentoPathProvider = new MagentoPathProvider();

return $path;
return sprintf('%s/app/bootstrap.php', $magentoPathProvider->getMagentoRootDirectory());
}
}
22 changes: 3 additions & 19 deletions tests/Context/Tasks/CacheCleanerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
use SEEC\Behat\Magento2Extension\Features\Bootstrap\Context\Tasks\MagentoPathProviderInterface;
use Symfony\Component\Filesystem\Filesystem;
use Symfony\Component\Finder\Finder;
use Symfony\Component\Finder\SplFileInfo;

final class CacheCleanerTest extends TestCase
{
Expand All @@ -37,29 +36,14 @@ public function test_it_will_attempt_to_clear_the_cache_correctly(): void
->method('getMagentoRootDirectory')
->willReturn('/var/www/html');

$file1 = $this->createMock(SplFileInfo::class);
$file1->expects($this->once())
->method('getRelativePathname')
->willReturn('.');
$file1->expects($this->never())
->method('getPathname');

$file2 = $this->createMock(SplFileInfo::class);
$file2->expects($this->once())
->method('getRelativePathname')
->willReturn('test');
$file2->expects($this->once())
->method('getPathname')
->willReturn('/var/www/html/var/cache/test');

$this->finder->expects($this->once())
->method('in')
->with('/var/www/html/var/cache/')
->willReturn([$file1, $file2]);
->with('/var/www/html/var/cache')
->willReturnSelf();

$this->fileSystem->expects($this->once())
->method('remove')
->with('/var/www/html/var/cache/test');
->with($this->finder);

$this->cacheCleaner->clean(false);
}
Expand Down

0 comments on commit 3b68221

Please sign in to comment.