diff --git a/.gitignore b/.gitignore index 0f85d0f5..e721ba65 100644 --- a/.gitignore +++ b/.gitignore @@ -5,4 +5,5 @@ /composer.lock /auth.json /_workdir +/_workdir_cache .phpunit.result.cache diff --git a/codeception.dist.yml b/codeception.dist.yml index e4eabcd9..bd0c532c 100644 --- a/codeception.dist.yml +++ b/codeception.dist.yml @@ -34,6 +34,10 @@ modules: printOutput: false PhpBrowser: url: "%Magento.docker.settings.env.url.base%" + REST: + depends: PhpBrowser + url: "%Magento.docker.settings.env.url.base%" + shortDebugResponse: 300 Magento\CloudDocker\Test\Functional\Codeception\MagentoDb: dsn: "mysql:host=%Magento.docker.settings.db.host%;port=%Magento.docker.settings.db.port%;dbname=%Magento.docker.settings.db.path%" user: "%Magento.docker.settings.db.username%" diff --git a/composer.json b/composer.json index ec33cd6e..7f045afa 100644 --- a/composer.json +++ b/composer.json @@ -23,6 +23,7 @@ "codeception/module-asserts": "^1.2", "codeception/module-db": "^1.0", "codeception/module-phpbrowser": "^1.0", + "codeception/module-rest": "^1.2", "consolidation/robo": "^1.2", "phpmd/phpmd": "@stable", "phpstan/phpstan": "^0.11", diff --git a/src/Command/BuildCompose.php b/src/Command/BuildCompose.php index c4faab7d..407bac1b 100644 --- a/src/Command/BuildCompose.php +++ b/src/Command/BuildCompose.php @@ -171,6 +171,16 @@ protected function configure(): void null, InputOption::VALUE_NONE, 'Disable mailhog' + )->addOption( + Source\CliSource::OPTION_MAILHOG_SMTP_PORT, + null, + InputOption::VALUE_REQUIRED, + 'MailHog SMTP port' + )->addOption( + Source\CliSource::OPTION_MAILHOG_HTTP_PORT, + null, + InputOption::VALUE_REQUIRED, + 'MailHog HTTP port' )->addOption( Source\CliSource::OPTION_SET_DOCKER_HOST_XDEBUG, null, diff --git a/src/Compose/ProductionBuilder.php b/src/Compose/ProductionBuilder.php index fe430480..31dbee32 100644 --- a/src/Compose/ProductionBuilder.php +++ b/src/Compose/ProductionBuilder.php @@ -402,7 +402,13 @@ public function build(Config $config): Manager self::SERVICE_MAILHOG, $this->serviceFactory->create( ServiceInterface::SERVICE_MAILHOG, - $this->serviceFactory->getDefaultVersion(ServiceInterface::SERVICE_MAILHOG) + $this->serviceFactory->getDefaultVersion(ServiceInterface::SERVICE_MAILHOG), + [ + 'ports' => [ + $config->getMailHogSmtpPort() . ':1025', + $config->getMailHogHttpPort() . ':8025', + ] + ] ), [self::NETWORK_MAGENTO], [] diff --git a/src/Config/Config.php b/src/Config/Config.php index 2908a3f7..67cd0093 100644 --- a/src/Config/Config.php +++ b/src/Config/Config.php @@ -454,4 +454,22 @@ public function getDbIncrementOffset(): int 1 ); } + + /** + * @return string|null + * @throws ConfigurationMismatchException + */ + public function getMailHogSmtpPort(): ?string + { + return $this->all()->get(SourceInterface::SYSTEM_MAILHOG_SMTP_PORT); + } + + /** + * @return string|null + * @throws ConfigurationMismatchException + */ + public function getMailHogHttpPort(): ?string + { + return $this->all()->get(SourceInterface::SYSTEM_MAILHOG_HTTP_PORT); + } } diff --git a/src/Config/Source/BaseSource.php b/src/Config/Source/BaseSource.php index df2da38b..202537c7 100644 --- a/src/Config/Source/BaseSource.php +++ b/src/Config/Source/BaseSource.php @@ -27,6 +27,8 @@ class BaseSource implements SourceInterface public const DEFAULT_HOST = 'magento2.docker'; public const DEFAULT_PORT = '80'; public const DEFAULT_TLS_PORT = '443'; + public const DEFAULT_MAILHOG_SMTP_PORT = '1025'; + public const DEFAULT_MAILHOG_HTTP_PORT = '8025'; /** * @var EnvReader @@ -73,7 +75,9 @@ public function read(): Repository self::SYSTEM_HOST => self::DEFAULT_HOST, self::SYSTEM_TLS_PORT => self::DEFAULT_TLS_PORT, self::INSTALLATION_TYPE => self::INSTALLATION_TYPE_COMPOSER, - self::MAGENTO_VERSION => $this->getMagentoVersion() + self::MAGENTO_VERSION => $this->getMagentoVersion(), + self::SYSTEM_MAILHOG_SMTP_PORT => self::DEFAULT_MAILHOG_SMTP_PORT, + self::SYSTEM_MAILHOG_HTTP_PORT => self::DEFAULT_MAILHOG_HTTP_PORT ]); try { diff --git a/src/Config/Source/CliSource.php b/src/Config/Source/CliSource.php index f91860d1..7a0e049a 100644 --- a/src/Config/Source/CliSource.php +++ b/src/Config/Source/CliSource.php @@ -34,6 +34,12 @@ class CliSource implements SourceInterface public const OPTION_NO_ES = 'no-es'; public const OPTION_NO_MAILHOG = 'no-mailhog'; + /** + * MailHog configuration + */ + public const OPTION_MAILHOG_SMTP_PORT = 'mailhog-smtp-port'; + public const OPTION_MAILHOG_HTTP_PORT = 'mailhog-http-port'; + /** * State modifiers. */ @@ -277,6 +283,14 @@ public function read(): Repository $repository->set(SourceInterface::SYSTEM_MARIADB_CONF, true); } + if ($port = $this->input->getOption(self::OPTION_MAILHOG_SMTP_PORT)) { + $repository->set(self::SYSTEM_MAILHOG_SMTP_PORT, $port); + } + + if ($port = $this->input->getOption(self::OPTION_MAILHOG_HTTP_PORT)) { + $repository->set(self::SYSTEM_MAILHOG_HTTP_PORT, $port); + } + return $repository; } } diff --git a/src/Config/Source/SourceInterface.php b/src/Config/Source/SourceInterface.php index f5e25495..11faf693 100644 --- a/src/Config/Source/SourceInterface.php +++ b/src/Config/Source/SourceInterface.php @@ -143,6 +143,8 @@ interface SourceInterface public const SYSTEM_DB_ENTRYPOINT = 'system.db_entrypoint'; public const SYSTEM_MARIADB_CONF = 'system.mariadb_conf'; public const SYSTEM_SET_DOCKER_HOST = 'system.set_docker_host'; + public const SYSTEM_MAILHOG_SMTP_PORT = 'system.mailhog.smtp_port'; + public const SYSTEM_MAILHOG_HTTP_PORT = 'system.mailhog.http_port'; public const SYSTEM_DB_INCREMENT_INCREMENT = 'system.db.increment_increment'; public const SYSTEM_DB_INCREMENT_OFFSET = 'system.db.increment_offset'; diff --git a/src/Service/ServiceFactory.php b/src/Service/ServiceFactory.php index ea23d99f..0e76de50 100644 --- a/src/Service/ServiceFactory.php +++ b/src/Service/ServiceFactory.php @@ -132,7 +132,6 @@ class ServiceFactory 'version' => 'latest', 'pattern' => self::PATTERN_STD, 'config' => [ - 'restart' => 'always', 'ports' => [ '1025:1025', '8025:8025' diff --git a/src/Test/Functional/Acceptance.suite.dist.yml b/src/Test/Functional/Acceptance.suite.dist.yml index b8a860d5..4286cf54 100644 --- a/src/Test/Functional/Acceptance.suite.dist.yml +++ b/src/Test/Functional/Acceptance.suite.dist.yml @@ -6,3 +6,4 @@ modules: - Magento\CloudDocker\Test\Functional\Codeception\MagentoDb - PhpBrowser - Asserts + - REST diff --git a/src/Test/Functional/Acceptance/MailHogCest.php b/src/Test/Functional/Acceptance/MailHogCest.php new file mode 100644 index 00000000..a594d2c2 --- /dev/null +++ b/src/Test/Functional/Acceptance/MailHogCest.php @@ -0,0 +1,65 @@ +<?php +/** + * Copyright © Magento, Inc. All rights reserved. + * See COPYING.txt for license details. + */ +declare(strict_types=1); + +namespace Magento\CloudDocker\Test\Functional\Acceptance; + +/** + * @group php74 + */ +class MailHogCest extends AbstractCest +{ + /** + * @param \CliTester $I + * @throws \Exception + */ + public function testDefaultPorts(\CliTester $I): void + { + $I->updateBaseUrl('http://magento2.docker:8025/'); + $I->assertTrue( + $I->runEceDockerCommand('build:compose'), + 'Command build:compose failed' + ); + $this->runAndAssert($I); + } + + /** + * @param \CliTester $I + * @throws \Exception + */ + public function testCustomPorts(\CliTester $I): void + { + $I->updateBaseUrl('http://magento2.docker:8026/'); + $I->assertTrue( + $I->runEceDockerCommand('build:compose --mailhog-http-port=8026 --mailhog-smtp-port=1026'), + 'Command build:compose failed' + ); + $this->runAndAssert($I); + } + + /** + * @param \CliTester $I + * @throws \Exception + */ + private function runAndAssert(\CliTester $I): void + { + $I->replaceImagesWithGenerated(); + $I->startEnvironment(); + $I->amOnPage('/'); + $I->see('MailHog'); + + $I->sendAjaxGetRequest('/api/v2/messages', ['limit' => 10]); + $I->seeResponseIsJson(); + $I->assertSame([0], $I->grabDataFromResponseByJsonPath('$.total')); + + $I->assertTrue( + $I->runDockerComposeCommand('run deploy bash -c "php -r \"mail(\'test@example.com\',\'test\',\'test\');\""') + ); + $I->sendAjaxGetRequest('/api/v2/messages', ['limit' => 10]); + $I->seeResponseIsJson(); + $I->assertSame([1], $I->grabDataFromResponseByJsonPath('$.total')); + } +} diff --git a/src/Test/Integration/BuildCustomComposeTest.php b/src/Test/Integration/BuildCustomComposeTest.php index 6d0b00b3..1a7e172d 100644 --- a/src/Test/Integration/BuildCustomComposeTest.php +++ b/src/Test/Integration/BuildCustomComposeTest.php @@ -85,6 +85,10 @@ public function buildDataProvider(): array 'db' => [ 'increment_increment' => 3, 'increment_offset' => 2 + ], + 'mailhog' => [ + 'smtp_port' => '1026', + 'http_port' => '8026' ] ], 'services' => [ diff --git a/src/Test/Integration/_files/cloud_base/docker-compose.exp.yml b/src/Test/Integration/_files/cloud_base/docker-compose.exp.yml index 293bcad1..ccfed8d0 100644 --- a/src/Test/Integration/_files/cloud_base/docker-compose.exp.yml +++ b/src/Test/Integration/_files/cloud_base/docker-compose.exp.yml @@ -170,7 +170,6 @@ services: mailhog: hostname: mailhog.magento2.docker image: 'mailhog/mailhog:latest' - restart: always ports: - '1025:1025' - '8025:8025' diff --git a/src/Test/Integration/_files/custom_cloud_base/docker-compose.exp.yml b/src/Test/Integration/_files/custom_cloud_base/docker-compose.exp.yml index 5658d981..4c480ba0 100644 --- a/src/Test/Integration/_files/custom_cloud_base/docker-compose.exp.yml +++ b/src/Test/Integration/_files/custom_cloud_base/docker-compose.exp.yml @@ -136,10 +136,9 @@ services: mailhog: hostname: mailhog.magento2.test image: 'mailhog/mailhog:latest' - restart: always ports: - - '1025:1025' - - '8025:8025' + - '1026:1025' + - '8026:8025' networks: magento: aliases: