Skip to content

Commit ea06e74

Browse files
#46 Upgrade to CakePHP 4.3 (#47)
1 parent 47b9517 commit ea06e74

File tree

10 files changed

+55
-72
lines changed

10 files changed

+55
-72
lines changed

composer.json

+2-1
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,12 @@
1212
}
1313
],
1414
"require": {
15-
"cakephp/cakephp": "~4.0"
15+
"cakephp/cakephp": "^4.3"
1616
},
1717
"require-dev": {
1818
"phpunit/phpunit": "^8.5",
1919
"cakephp/cakephp-codesniffer": "^4.0",
20+
"cakephp/migrations": "^3.3",
2021
"php-coveralls/php-coveralls": "^2.1"
2122
},
2223
"autoload": {

phpunit.xml.dist

+3-10
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,9 @@
1717
</testsuite>
1818
</testsuites>
1919

20-
<!-- Setup a listener for fixtures -->
21-
<listeners>
22-
<listener
23-
class="\Cake\TestSuite\Fixture\FixtureInjector"
24-
file="./vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php">
25-
<arguments>
26-
<object class="\Cake\TestSuite\Fixture\FixtureManager" />
27-
</arguments>
28-
</listener>
29-
</listeners>
20+
<extensions>
21+
<extension class="\Cake\TestSuite\Fixture\PHPUnitExtension" />
22+
</extensions>
3023

3124
<!-- Prevent coverage reports from looking in tests and vendors -->
3225
<filter>

src/EmailQueue.php

-1
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ class EmailQueue
2222
* - headers: Key => Value list of extra headers for the email
2323
* - theme: The View Theme to find the email templates
2424
* - config : the name of the email config to be used for sending
25-
*
2625
* @return bool
2726
*/
2827
public static function enqueue($to, array $data, array $options = [])

src/Model/Table/EmailQueueTable.php

+3-4
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class EmailQueueTable extends Table
2121
public const MAX_TEMPLATE_LENGTH = 100;
2222

2323
/**
24-
* {@inheritdoc}
24+
* @inheritDoc
2525
*/
2626
public function initialize(array $config = []): void
2727
{
@@ -53,7 +53,6 @@ public function initialize(array $config = []): void
5353
* - layout : the name of the layout to be used to wrap email message
5454
* - format: Type of template to use (html, text or both)
5555
* - config : the name of the email config to be used for sending
56-
*
5756
* @throws \Exception any exception raised in transactional callback
5857
* @throws \LengthException If `template` option length is greater than maximum allowed length
5958
* @return bool
@@ -118,7 +117,8 @@ public function getBatch($size = 10): array
118117
$this->aliasField('locked') => false,
119118
])
120119
->limit($size)
121-
->order([$this->aliasField('created') => 'ASC']);
120+
->order([$this->aliasField('created') => 'ASC'])
121+
->all();
122122

123123
$emails
124124
->extract('id')
@@ -138,7 +138,6 @@ public function getBatch($size = 10): array
138138
* Releases locks for all emails in $ids.
139139
*
140140
* @param array|\Traversable $ids The email ids to unlock
141-
*
142141
* @return void
143142
*/
144143
public function releaseLocks($ids): void

src/Shell/PreviewShell.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class PreviewShell extends Shell
1414
/**
1515
* Main
1616
*
17-
* @return bool|int|null|void
17+
* @return void
1818
*/
1919
public function main()
2020
{

tests/Fixture/EmailQueueFixture.php

+12-33
Original file line numberDiff line numberDiff line change
@@ -11,48 +11,22 @@ class EmailQueueFixture extends TestFixture
1111
{
1212
public $table = 'email_queue';
1313

14-
/**
15-
* Fields.
16-
*
17-
* @var array
18-
*/
19-
public $fields = [
20-
'id' => ['type' => 'uuid', 'null' => false],
21-
'email' => ['type' => 'string', 'null' => false, 'default' => null, 'length' => 100, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'],
22-
'from_name' => ['type' => 'string', 'null' => true, 'default' => null, 'length' => 100, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'],
23-
'from_email' => ['type' => 'string', 'null' => true, 'default' => null, 'length' => 100, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'],
24-
'subject' => ['type' => 'string', 'null' => false, 'default' => null, 'length' => 255, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'],
25-
'config' => ['type' => 'string', 'null' => false, 'default' => null, 'length' => 30, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'],
26-
'template' => ['type' => 'string', 'null' => false, 'default' => null, 'length' => 50, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'],
27-
'layout' => ['type' => 'string', 'null' => false, 'default' => null, 'length' => 50, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'],
28-
'format' => ['type' => 'string', 'null' => false, 'default' => null, 'length' => 5, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'],
29-
'template_vars' => ['type' => 'text', 'null' => false, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'],
30-
'headers' => ['type' => 'text', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'],
31-
'error' => ['type' => 'text', 'null' => true, 'default' => null, 'collate' => 'utf8_general_ci', 'charset' => 'utf8'],
32-
'sent' => ['type' => 'boolean', 'null' => false, 'default' => 0],
33-
'locked' => ['type' => 'boolean', 'null' => false, 'default' => 0],
34-
'send_tries' => ['type' => 'integer', 'null' => false, 'default' => 0, 'length' => 2],
35-
'send_at' => ['type' => 'datetime', 'null' => true, 'default' => null],
36-
'created' => ['type' => 'datetime', 'null' => false, 'default' => null],
37-
'modified' => ['type' => 'datetime', 'null' => false, 'default' => null],
38-
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]],
39-
];
40-
4114
/**
4215
* Records.
4316
*
4417
* @var array
4518
*/
4619
public $records = [
4720
[
48-
'id' => 'email-1',
21+
'id' => '1',
4922
'email' => '[email protected]',
5023
'from_name' => null,
5124
'from_email' => null,
5225
'subject' => 'Free dealz',
5326
'config' => 'default',
5427
'template' => 'default',
5528
'layout' => 'default',
29+
'theme' => 'default',
5630
'format' => 'both',
5731
'template_vars' => '{"a":1,"b":2}',
5832
'headers' => '{"foo":"bar"}',
@@ -64,14 +38,15 @@ class EmailQueueFixture extends TestFixture
6438
'modified' => '2011-06-20 13:50:48',
6539
],
6640
[
67-
'id' => 'email-2',
41+
'id' => 2,
6842
'email' => '[email protected]',
6943
'from_name' => null,
7044
'from_email' => null,
7145
'subject' => 'Free dealz',
7246
'config' => 'default',
7347
'template' => 'default',
7448
'layout' => 'default',
49+
'theme' => 'default',
7550
'format' => 'both',
7651
'template_vars' => '{"a":1,"b":2}',
7752
'headers' => '{"foo":"bar"}',
@@ -83,14 +58,15 @@ class EmailQueueFixture extends TestFixture
8358
'modified' => '2011-06-20 13:50:48',
8459
],
8560
[
86-
'id' => 'email-3',
61+
'id' => 3,
8762
'email' => '[email protected]',
8863
'from_name' => null,
8964
'from_email' => null,
9065
'subject' => 'Free dealz',
9166
'config' => 'default',
9267
'template' => 'default',
9368
'layout' => 'default',
69+
'theme' => 'default',
9470
'format' => 'both',
9571
'template_vars' => '{"a":1,"b":2}',
9672
'headers' => '{"foo":"bar"}',
@@ -102,14 +78,15 @@ class EmailQueueFixture extends TestFixture
10278
'modified' => '2011-06-20 13:50:48',
10379
],
10480
[
105-
'id' => 'email-4',
81+
'id' => 4,
10682
'email' => '[email protected]',
10783
'from_name' => null,
10884
'from_email' => null,
10985
'subject' => 'Free dealz',
11086
'config' => 'default',
11187
'template' => 'default',
11288
'layout' => 'default',
89+
'theme' => 'default',
11390
'format' => 'both',
11491
'template_vars' => '{"a":1,"b":2}',
11592
'headers' => '{"foo":"bar"}',
@@ -121,14 +98,15 @@ class EmailQueueFixture extends TestFixture
12198
'modified' => '2011-06-20 13:50:48',
12299
],
123100
[
124-
'id' => 'email-5',
101+
'id' => 5,
125102
'email' => '[email protected]',
126103
'from_name' => null,
127104
'from_email' => null,
128105
'subject' => 'Free dealz',
129106
'config' => 'default',
130107
'template' => 'default',
131108
'layout' => 'default',
109+
'theme' => 'default',
132110
'format' => 'both',
133111
'template_vars' => '{"a":1,"b":2}',
134112
'headers' => '{"foo":"bar"}',
@@ -140,14 +118,15 @@ class EmailQueueFixture extends TestFixture
140118
'modified' => '2011-06-20 13:50:48',
141119
],
142120
[
143-
'id' => 'email-6',
121+
'id' => 6,
144122
'email' => '[email protected]',
145123
'from_name' => null,
146124
'from_email' => null,
147125
'subject' => 'Free dealz',
148126
'config' => 'default',
149127
'template' => 'default',
150128
'layout' => 'default',
129+
'theme' => 'default',
151130
'format' => 'both',
152131
'template_vars' => '{"a":1,"b":2}',
153132
'headers' => '{"foo":"bar"}',

tests/TestCase/Model/Table/EmailQueueTest.php

+22-18
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace EmailQueue\Test\Model\Table;
4+
namespace EmailQueue\Test\TestCase\Model\Table;
55

6-
use Cake\I18n\Time;
6+
use Cake\I18n\FrozenTime;
77
use Cake\ORM\TableRegistry;
88
use Cake\TestSuite\TestCase;
99
use DateTime;
1010
use EmailQueue\EmailQueue;
1111
use EmailQueue\Model\Table\EmailQueueTable;
12+
use EmailQueue\Test\Fixture\EmailQueueFixture;
1213

1314
class EmailQueueTest extends TestCase
1415
{
@@ -23,7 +24,7 @@ class EmailQueueTest extends TestCase
2324
* @var array
2425
*/
2526
public $fixtures = [
26-
'plugin.EmailQueue.EmailQueue',
27+
EmailQueueFixture::class,
2728
];
2829

2930
/**
@@ -42,8 +43,9 @@ public function setUp(): void
4243
public function testEnqueue()
4344
{
4445
$count = $this->EmailQueue->find()->count();
46+
$email = '[email protected]';
4547
$this->EmailQueue->enqueue(
46-
48+
$email,
4749
['a' => 'variable', 'some' => 'thing'],
4850
[
4951
'subject' => 'Hey!',
@@ -54,15 +56,17 @@ public function testEnqueue()
5456
$this->assertEquals(++$count, $this->EmailQueue->find()->count());
5557

5658
$result = $this->EmailQueue->find()
57-
->where(['email' => '[email protected]'])
59+
->where(compact('email'))
5860
->first()
5961
->toArray();
6062

6163
$expected = [
62-
'email' => '[email protected]',
64+
'email' => $email,
6365
'subject' => 'Hey!',
6466
'template' => 'default',
6567
'layout' => 'default',
68+
'theme' => '',
69+
'attachments' => [],
6670
'format' => 'both',
6771
'template_vars' => ['a' => 'variable', 'some' => 'thing'],
6872
'sent' => false,
@@ -74,12 +78,12 @@ public function testEnqueue()
7478
'from_name' => null,
7579
'from_email' => null,
7680
];
77-
$sendAt = new Time($result['send_at']);
81+
$sendAt = new FrozenTime($result['send_at']);
7882
unset($result['id'], $result['created'], $result['modified'], $result['send_at']);
7983
$this->assertEquals($expected, $result);
8084
$this->assertEquals(gmdate('Y-m-d H'), $sendAt->format('Y-m-d H'));
8185

82-
$date = new Time('2019-01-11 11:14:15');
86+
$date = new FrozenTime('2019-01-11 11:14:15');
8387
$this->EmailQueue->enqueue(['[email protected]', '[email protected]'], ['a' => 'b'], ['send_at' => $date, 'subject' => 'Hey!']);
8488
$this->assertEquals($count + 2, $this->EmailQueue->find()->count());
8589

@@ -120,16 +124,16 @@ public function testEnqueue()
120124
public function testGetBatch()
121125
{
122126
$batch = $this->EmailQueue->getBatch();
123-
$this->assertEquals(['email-1', 'email-2', 'email-3'], collection($batch)->extract('id')->toList());
127+
$this->assertEquals([1, 2, 3], collection($batch)->extract('id')->toList());
124128

125129
//At this point previous batch should be locked and next call should return an empty set
126130
$batch = $this->EmailQueue->getBatch();
127131
$this->assertEmpty($batch);
128132

129133
//Let's change send_at date for email-6 to get it on a batch
130-
$this->EmailQueue->updateAll(['send_at' => '2011-01-01 00:00'], ['id' => 'email-6']);
134+
$this->EmailQueue->updateAll(['send_at' => '2011-01-01 00:00'], ['id' => 6]);
131135
$batch = $this->EmailQueue->getBatch();
132-
$this->assertEquals(['email-6'], collection($batch)->extract('id')->toList());
136+
$this->assertEquals([6], collection($batch)->extract('id')->toList());
133137
}
134138

135139
/**
@@ -154,28 +158,28 @@ public function testClearLocks()
154158
$this->assertEmpty($this->EmailQueue->getBatch());
155159
$this->EmailQueue->clearLocks();
156160
$batch = $this->EmailQueue->getBatch();
157-
$this->assertEquals(['email-1', 'email-2', 'email-3', 'email-5'], collection($batch)->extract('id')->toList());
161+
$this->assertEquals([1, 2, 3, 5], collection($batch)->extract('id')->toList());
158162
}
159163

160164
/**
161165
* testSuccess method.
162166
*/
163167
public function testSuccess()
164168
{
165-
$this->EmailQueue->success('email-1');
166-
$this->assertEquals(1, $this->EmailQueue->get('email-1')->sent);
169+
$this->EmailQueue->success(1);
170+
$this->assertEquals(1, $this->EmailQueue->get(1)->sent);
167171
}
168172

169173
/**
170174
* testFail method.
171175
*/
172176
public function testFail()
173177
{
174-
$this->EmailQueue->fail('email-1');
175-
$this->assertEquals(2, $this->EmailQueue->get('email-1')->send_tries);
178+
$this->EmailQueue->fail(1);
179+
$this->assertEquals(2, $this->EmailQueue->get(1)->send_tries);
176180

177-
$this->EmailQueue->fail('email-1');
178-
$this->assertEquals(3, $this->EmailQueue->get('email-1')->send_tries);
181+
$this->EmailQueue->fail(1);
182+
$this->assertEquals(3, $this->EmailQueue->get(1)->send_tries);
179183
}
180184

181185
public function testProxy()

tests/TestCase/Shell/SenderShellTest.php

+7-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
<?php
22
declare(strict_types=1);
33

4-
namespace EmailQueue\Test\Shell;
4+
namespace EmailQueue\Test\TestCase\Shell;
55

66
use Cake\Console\ConsoleIo;
77
use Cake\Console\ConsoleOutput;
@@ -12,6 +12,7 @@
1212
use Cake\TestSuite\TestCase;
1313
use EmailQueue\Model\Table\EmailQueueTable;
1414
use EmailQueue\Shell\SenderShell;
15+
use EmailQueue\Test\Fixture\EmailQueueFixture;
1516
use TestApp\Mailer\TestMailer;
1617

1718
/**
@@ -42,7 +43,7 @@ class SenderShellTest extends TestCase
4243
* @var array
4344
*/
4445
public $fixtures = [
45-
'plugin.EmailQueue.EmailQueue',
46+
EmailQueueFixture::class,
4647
];
4748

4849
/**
@@ -100,7 +101,8 @@ public function testMainAllWin()
100101

101102
$emails = $this->EmailQueue
102103
->find()
103-
->where(['id IN' => ['email-1', 'email-2', 'email-3']])
104+
->where(['id IN' => [1, 2, 3]])
105+
->all()
104106
->toList();
105107

106108
$this->assertEquals(1, $emails[0]['send_tries']);
@@ -140,7 +142,8 @@ public function testMainAllFail()
140142

141143
$emails = $this->EmailQueue
142144
->find()
143-
->where(['id IN' => ['email-1', 'email-2', 'email-3']])
145+
->where(['id IN' => [1, 2, 3]])
146+
->all()
144147
->toList();
145148

146149
$this->assertEquals(2, $emails[0]['send_tries']);

0 commit comments

Comments
 (0)