Skip to content

Commit b913c15

Browse files
committed
Update tests for refactor
1 parent f6f8791 commit b913c15

File tree

6 files changed

+113
-167
lines changed

6 files changed

+113
-167
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use Tatter\Outbox\Models\EmailModel;
77
use Tests\Support\DatabaseTestCase;
88

9-
class EmailEntityTest extends DatabaseTestCase
9+
class EmailTest extends DatabaseTestCase
1010
{
1111
/**
1212
* Record of the email sent during setUp.
Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use Tatter\Outbox\Models\TemplateModel;
66
use Tests\Support\DatabaseTestCase;
77

8-
class ParentTest extends DatabaseTestCase
8+
class ParentTemplateTest extends DatabaseTestCase
99
{
1010
/**
1111
* @var DOMParser
@@ -33,25 +33,40 @@ public function setUp(): void
3333
'name' => 'Parent Template',
3434
'subject' => 'Parent {subject}',
3535
'body' => '<div>{body}</div><aside>{foobar}</aside>',
36-
'tokens' => ['subject', 'body', 'foobar'],
3736
]);
3837
$this->parent->id = model(TemplateModel::class)->insert($this->parent);
3938

4039
$this->template = new Template([
4140
'name' => 'Test Template',
4241
'subject' => 'Some {subject}',
4342
'body' => '<p>{number}</p>',
44-
'tokens' => ['subject', 'number', 'foobar'],
4543
'parent_id' => $this->parent->id,
4644
]);
4745
$this->template->id = model(TemplateModel::class)->insert($this->template);
4846
}
4947

50-
public function testRenderReturnsParentBody()
48+
public function testChildIncludesParentTokens()
49+
{
50+
$expected = ['subject', 'body', 'foobar', 'number'];
51+
$result = $this->template->getTokens();
52+
53+
$this->assertEquals($expected, array_values($result));
54+
}
55+
56+
public function testChildFallsBackToParentSubject()
57+
{
58+
$this->template->subject = '';
59+
60+
$result = $this->template->getSubject();
61+
62+
$this->assertEquals('Parent {subject}', $result);
63+
}
64+
65+
public function testRenderBodyIncludesParentBody()
5166
{
5267
// Expect the child variable in the parent context with inlined CSS
5368
$expected = '<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; margin-bottom: 15px;">{number}</p>';
54-
$result = $this->template->render();
69+
$result = $this->template->renderBody();
5570

5671
$this->parser->withString($result);
5772
$this->assertTrue($this->parser->see('{number}', 'p'));
@@ -61,7 +76,7 @@ public function testRenderReturnsParentBody()
6176

6277
public function testRenderAppliesTokens()
6378
{
64-
$this->parser->withString($this->template->render([
79+
$this->parser->withString($this->template->renderBody([
6580
'number' => 'Banana',
6681
'foobar' => 'Orange',
6782
]));
@@ -72,7 +87,7 @@ public function testRenderAppliesTokens()
7287

7388
public function testRenderInlinesStyles()
7489
{
75-
$result = $this->template->render([], 'aside { color: magenta; }');
90+
$result = $this->template->renderBody([], 'aside { color: magenta; }');
7691

7792
$this->parser->withString($result);
7893

tests/entities/TemplateTest.php

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
<?php
2+
3+
use CodeIgniter\Email\Email;
4+
use CodeIgniter\Test\CIUnitTestCase;
5+
use CodeIgniter\Test\DOMParser;
6+
use Tatter\Outbox\Entities\Template;
7+
8+
class TemplateEntityTest extends CIUnitTestCase
9+
{
10+
/**
11+
* @var DOMParser
12+
*/
13+
protected $parser;
14+
15+
/**
16+
* @var Template
17+
*/
18+
protected $template;
19+
20+
public function setUp(): void
21+
{
22+
$this->resetServices();
23+
parent::setUp();
24+
25+
$this->parser = new DOMParser();
26+
$this->template = new Template([
27+
'name' => 'Test Template',
28+
'subject' => 'Some {subject}',
29+
'body' => '<p>{number}</p>',
30+
]);
31+
}
32+
33+
public function testGetTokensMatchesAll()
34+
{
35+
$result = $this->template->getTokens();
36+
37+
$this->assertEquals(['subject', 'number'], $result);
38+
}
39+
40+
public function testRenderBodyReturnsBody()
41+
{
42+
$this->parser->withString($this->template->renderBody());
43+
44+
$this->assertTrue($this->parser->see('{number}', 'p'));
45+
}
46+
47+
public function testRenderBodyAppliesTokens()
48+
{
49+
$this->parser->withString($this->template->renderBody(['number' => 'Banana']));
50+
51+
$this->assertTrue($this->parser->see('Banana', 'p'));
52+
}
53+
54+
public function testRenderBodyInlinesStyles()
55+
{
56+
$this->parser->withString($this->template->renderBody([], 'p { color: magenta; }'));
57+
58+
$this->assertTrue($this->parser->see('magenta'));
59+
}
60+
61+
public function testEmailReturnsEmailInstance()
62+
{
63+
$template = new Template([
64+
'name' => 'Test Template',
65+
'subject' => 'Some {subject}',
66+
'body' => '<p>{number}</p>',
67+
]);
68+
69+
$result = $template->email();
70+
71+
$this->assertInstanceOf(Email::class, $result);
72+
}
73+
74+
public function testEmailUsesData()
75+
{
76+
$template = new Template([
77+
'name' => 'Test Template',
78+
'subject' => 'Some {subject}',
79+
'body' => '<p>{number}</p>',
80+
]);
81+
82+
$email = $template->email([
83+
'subject' => 'pig',
84+
]);
85+
86+
$result = $this->getPrivateProperty($email, 'tmpArchive');
87+
88+
$this->assertEquals('Some pig', $result['subject']);
89+
}
90+
}
Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ public function setUp(): void
2020
'name' => 'Test Template',
2121
'subject' => 'Some {subject}',
2222
'body' => '<p>{number}</p>',
23-
'tokens' => ['subject', 'number', 'foobar'],
2423
]);
2524
$this->template->id = model(TemplateModel::class)->insert($this->template);
2625
}

tests/templates/TemplateEntityTest.php

Lines changed: 0 additions & 53 deletions
This file was deleted.

tests/unit/LibraryTest.php

Lines changed: 0 additions & 105 deletions
This file was deleted.

0 commit comments

Comments
 (0)