Skip to content

Commit 37da931

Browse files
committed
Add checks to check if message has property for mailer
We cannot blanket add `$transport` field to Mailer, as not all implementations may have it add basic docker-compose file that helps run tests and phpstan
1 parent 56715b5 commit 37da931

File tree

7 files changed

+91
-11
lines changed

7 files changed

+91
-11
lines changed

.env-example

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
COMPOSER_HOME=~/.config/composer
2+
UID=1000

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,4 @@
55
tests/_support
66
tests/_output
77
tests/cases/yii2-app-advanced/_data/db.sqlite
8+
.env

Dockerfile

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
ARG PHP_VERSION="8.3-alpine"
2+
3+
FROM php:$PHP_VERSION
4+
5+
ADD https://github.com/mlocati/docker-php-extension-installer/releases/latest/download/install-php-extensions /usr/local/bin/
6+
7+
RUN chmod +x /usr/local/bin/install-php-extensions \
8+
&& apk add --no-cache git \
9+
&& install-php-extensions gd intl zip intl pcov @composer
10+
11+
WORKDIR /var/www/html

composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,8 @@
2929
"codemix/yii2-localeurls": "^1.7",
3030
"codeception/module-asserts": ">= 3.0",
3131
"codeception/module-filesystem": "> 3.0",
32-
"phpstan/phpstan": "^1.10"
32+
"phpstan/phpstan": "^1.10",
33+
"yiisoft/yii2-swiftmailer": "^2.0.0"
3334
},
3435
"autoload":{
3536
"classmap": ["src/"]

docker-compose.yml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
services:
2+
3+
php80: &phpbase
4+
user: ${UID}
5+
volumes:
6+
- ${COMPOSER_HOME}:/.config/composer:delegated
7+
# Mount source-code for development
8+
- ./:/var/www/html
9+
env_file:
10+
- .env
11+
environment:
12+
- XDEBUG_CONFIG=client_host=host.docker.internal
13+
extra_hosts:
14+
- "host.docker.internal:host-gateway"
15+
build:
16+
context: ./
17+
dockerfile: Dockerfile
18+
args:
19+
- PHP_VERSION=8.0-alpine
20+
21+
php81:
22+
<<: *phpbase
23+
build:
24+
args:
25+
- PHP_VERSION=8.1-alpine
26+
php82:
27+
<<: *phpbase
28+
build:
29+
args:
30+
- PHP_VERSION=8.2-alpine
31+
32+
php83:
33+
<<: *phpbase
34+
build:
35+
args:
36+
- PHP_VERSION=8.3-alpine
37+
php84:
38+
<<: *phpbase
39+
build:
40+
args:
41+
- PHP_VERSION=8.4-alpine

src/Codeception/Lib/Connector/Yii2.php

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@
1414
use yii\base\ExitException;
1515
use yii\base\Security;
1616
use yii\base\UserException;
17+
use yii\helpers\ArrayHelper;
1718
use yii\mail\MessageInterface;
18-
use yii\symfonymailer\Mailer;
1919
use yii\web\Application;
2020
use yii\web\ErrorHandler;
2121
use yii\web\IdentityInterface;
@@ -429,13 +429,22 @@ protected function mockMailer(array $config): array
429429
$this->emails[] = $message;
430430
},
431431
'mailer' => [
432-
'class' => Mailer::class,
433-
'transportFactory' => new NullTransportFactory()
432+
'class' => \yii\symfonymailer\Mailer::class
434433
]
435434
];
436435

437-
if (isset($config['components']['mailer'])) {
438-
$mailerConfig['mailer'] = $config['components']['mailer'];
436+
if (isset($config['components']['mailer']) && is_array($config['components']['mailer'])) {
437+
$mailerConfig['mailer'] = ArrayHelper::merge($mailerConfig['mailer'], $config['components']['mailer']);
438+
}
439+
440+
/**
441+
* Set transports only if known mailers that have this field is used
442+
*/
443+
if ($mailerConfig['mailer']['class'] instanceof \yii\symfonymailer\Mailer) {
444+
$mailerConfig['mailer']['transport'] = \Symfony\Component\Mailer\Transport\NullTransport::class;
445+
}
446+
if ($mailerConfig['mailer']['class'] instanceof \yii\swiftmailer\Mailer) {
447+
$mailerConfig['mailer']['transport'] = \Swift_Transport_NullTransport::class;
439448
}
440449

441450
$config['components']['mailer'] = $mailerConfig;

src/Codeception/Lib/Connector/Yii2/TestMailer.php

Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,15 @@
11
<?php
22
namespace Codeception\Lib\Connector\Yii2;
33

4+
use ReflectionClass;
5+
use yii\base\BaseObject;
6+
use yii\base\InvalidConfigException;
47
use yii\base\UnknownMethodException;
58
use yii\base\UnknownPropertyException;
69
use yii\di\Instance;
710
use yii\mail\BaseMailer;
811
use yii\mail\MailerInterface;
12+
use yii\mail\MessageInterface;
913

1014
class TestMailer extends BaseMailer
1115
{
@@ -23,7 +27,7 @@ class TestMailer extends BaseMailer
2327
* @inheritdoc
2428
* @throws InvalidConfigException
2529
*/
26-
public function init()
30+
public function init(): void
2731
{
2832
parent::init();
2933
$this->mailer = Instance::ensure($this->mailer, MailerInterface::class);
@@ -72,20 +76,31 @@ public function __set($name, $value)
7276
/**
7377
* @inheritdoc
7478
*/
75-
public function compose($view = null, array $params = [])
79+
public function compose($view = null, array $params = []): MessageInterface
7680
{
7781
$message = $this->mailer->compose($view, $params);
78-
$message->mailer = $this;
82+
83+
if ($message instanceof BaseObject && $message->canSetProperty('mailer')) {
84+
/** @phpstan-ignore property.notFound */
85+
$message->mailer = $this;
86+
} else {
87+
$reflection = new ReflectionClass($message);
88+
if ($reflection->hasProperty('mailer')) {
89+
/** @phpstan-ignore property.notFound */
90+
$message->mailer = $this;
91+
}
92+
}
93+
7994
return $message;
8095
}
8196

82-
protected function sendMessage($message)
97+
protected function sendMessage($message): bool
8398
{
8499
call_user_func($this->callback, $message);
85100
return true;
86101
}
87102

88-
protected function saveMessage($message)
103+
protected function saveMessage($message): bool
89104
{
90105
call_user_func($this->callback, $message);
91106
return true;

0 commit comments

Comments
 (0)