|
| 1 | +<?php |
| 2 | + |
| 3 | +use CodeIgniter\Test\CIUnitTestCase; |
| 4 | +use CodeIgniter\Test\DOMParser; |
| 5 | +use Tatter\Outbox\Entities\Template; |
| 6 | +use Tatter\Outbox\Models\TemplateModel; |
| 7 | +use Tests\Support\DatabaseTestCase; |
| 8 | + |
| 9 | +class ParentTest extends DatabaseTestCase |
| 10 | +{ |
| 11 | + /** |
| 12 | + * @var DOMParser |
| 13 | + */ |
| 14 | + protected $parser; |
| 15 | + |
| 16 | + /** |
| 17 | + * @var Template |
| 18 | + */ |
| 19 | + protected $parent; |
| 20 | + |
| 21 | + /** |
| 22 | + * @var Template |
| 23 | + */ |
| 24 | + protected $template; |
| 25 | + |
| 26 | + public function setUp(): void |
| 27 | + { |
| 28 | + $this->resetServices(); |
| 29 | + parent::setUp(); |
| 30 | + |
| 31 | + $this->parser = new DOMParser(); |
| 32 | + |
| 33 | + $this->parent = new Template([ |
| 34 | + 'name' => 'Parent Template', |
| 35 | + 'subject' => 'Parent {subject}', |
| 36 | + 'body' => '<div>{body}</div><aside>{foobar}</aside>', |
| 37 | + 'tokens' => ['subject', 'body', 'foobar'], |
| 38 | + ]); |
| 39 | + $this->parent->id = model(TemplateModel::class)->insert($this->parent); |
| 40 | + |
| 41 | + $this->template = new Template([ |
| 42 | + 'name' => 'Test Template', |
| 43 | + 'subject' => 'Some {subject}', |
| 44 | + 'body' => '<p>{number}</p>', |
| 45 | + 'tokens' => ['subject', 'number', 'foobar'], |
| 46 | + 'parent_id' => $this->parent->id, |
| 47 | + ]); |
| 48 | + $this->template->id = model(TemplateModel::class)->insert($this->template); |
| 49 | + } |
| 50 | + |
| 51 | + public function testRenderReturnsParentBody() |
| 52 | + { |
| 53 | + // Expect the child variable in the parent context with inlined CSS |
| 54 | + $expected = '<p style="font-family: sans-serif; font-size: 14px; font-weight: normal; margin: 0; margin-bottom: 15px;">{number}</p>'; |
| 55 | + $result = $this->template->render(); |
| 56 | + |
| 57 | + $this->parser->withString($result); |
| 58 | + $this->assertTrue($this->parser->see('{number}', 'p')); |
| 59 | + |
| 60 | + $this->assertStringContainsString($expected, $result); |
| 61 | + } |
| 62 | + |
| 63 | + public function testRenderAppliesTokens() |
| 64 | + { |
| 65 | + $this->parser->withString($this->template->render([ |
| 66 | + 'number' => 'Banana', |
| 67 | + 'foobar' => 'Orange', |
| 68 | + ])); |
| 69 | + |
| 70 | + $this->assertTrue($this->parser->see('Banana', 'div')); |
| 71 | + $this->assertTrue($this->parser->see('Orange', 'aside')); |
| 72 | + } |
| 73 | + |
| 74 | + public function testRenderInlinesStyles() |
| 75 | + { |
| 76 | + $result = $this->template->render([], 'aside { color: magenta; }'); |
| 77 | + |
| 78 | + $this->parser->withString($result); |
| 79 | + |
| 80 | + $this->assertTrue($this->parser->see('magenta')); |
| 81 | + } |
| 82 | +} |
0 commit comments