Completed
Push — master ( 6d48a0...fb36d6 )
by Jeroen De
14s
created

MailTemplatesTest.php$0 ➔ __construct()   A

Complexity

Conditions 1

Size

Total Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 2
rs 10
c 0
b 0
f 0
cc 1
1
<?php
2
3
declare( strict_types = 1 );
4
5
namespace WMDE\Fundraising\Frontend\Tests\Integration;
6
7
use WMDE\Fundraising\ContentProvider\ContentProvider;
8
use WMDE\Fundraising\Frontend\Factories\FunFunFactory;
9
use WMDE\Fundraising\Frontend\Tests\TestEnvironment;
10
11
/**
12
 * @licence GNU GPL v2+
13
 * @author Jeroen De Dauw < [email protected] >
14
 */
15
class MailTemplatesTest extends \PHPUnit\Framework\TestCase {
16
17
	/**
18
	 * @var FunFunFactory
19
	 */
20
	private $factory;
21
22
	public function setUp() {
23
		$this->createMissingTestFiles();
24
	}
25
26
	private function createMissingTestFiles() {
27
		foreach ( $this->getFreshlyRenderedContent() as $testFilePath => $testFileContent ) {
28
			if ( !file_exists( $testFilePath ) ) {
29
				file_put_contents( $testFilePath, $testFileContent );
30
			}
31
		}
32
	}
33
34
	private function getFreshlyRenderedContent(): \Iterator {
35
		$this->factory = $this->newFactory();
36
37
		foreach ( $this->getTestData() as $templateFileName => $templateTestData ) {
38
			 yield from $this->getFreshlyRenderedContentForTemplate( $templateFileName, $templateTestData );
39
		}
40
	}
41
42
	private function newFactory(): FunFunFactory {
43
		$ffFactory = TestEnvironment::newInstance( $this->getConfig() )->getFactory();
44
45
		$ffFactory->setContentProvider( $this->newContentKeyReturningContentProvider() );
46
47
		$app = require __DIR__ . '/../../app/bootstrap.php';
48
		$app->flush();
49
50
		$ffFactory->setTwigEnvironment( $app['twig'] );
51
52
		return $ffFactory;
53
	}
54
55
	private function getConfig(): array {
56
		return [
57
			'twig' => [
58
				'strict-variables' => true
59
			]
60
		];
61
	}
62
63
	private function newContentKeyReturningContentProvider(): ContentProvider {
64
		return new class() extends ContentProvider {
65
			public function __construct() {
66
			}
67
			public function getMail( string $contentKey, array $context = [] ): string {
68
				return $contentKey;
69
			}
70
		};
71
	}
72
73
	private function getTestData(): array {
74
		$ffFactory = $this->factory;
75
		return require __DIR__ . '/../Data/mail_templates.php';
76
	}
77
78
	private function getFreshlyRenderedContentForTemplate( string $templateFileName, array $templateTestData ): \Iterator {
79
		if ( empty( $templateTestData['variants'] ) ) {
80
			$templateTestData['variants'] = [ '' => [] ];
81
		}
82
83
		foreach( $templateTestData['variants'] as $variantName => $additionalContext ) {
84
			$filePath = $this->createTestFilePath( $templateFileName, $variantName );
85
			$content = $this->factory->getTwig()->render(
86
				$templateFileName,
87
				array_merge_recursive(
88
					$templateTestData['context'],
89
					$additionalContext
90
				)
91
			);
92
			yield $filePath => $content;
93
		}
94
	}
95
96
	private function createTestFilePath( string $templateFileName, string $variantName ): string {
97
		return __DIR__ . '/../Data/GeneratedMailTemplates/'
98
			. basename( $templateFileName, '.txt.twig' )
99
			. ( $variantName === '' ? '' : ".$variantName" )
100
			. '.txt';
101
	}
102
103
	/**
104
	 * @dataProvider storedRenderedContentProvider
105
	 */
106
	public function testCurrentRenderingMatchesStoredRendering( string $testFilePath, string $testFileContent ) {
107
		$this->assertSame(
108
			file_get_contents( $testFilePath ),
109
			$testFileContent
110
		);
111
	}
112
113
	public function storedRenderedContentProvider(): \Iterator {
114
		foreach ( $this->getFreshlyRenderedContent() as $testFilePath => $testFileContent ) {
115
			yield $testFilePath => [ $testFilePath, $testFileContent ];
116
		}
117
	}
118
119
}
120
121