1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* @copyright 2017 Vladimir Jimenez |
5
|
|
|
* @license https://github.com/allejo/stakx/blob/master/LICENSE.md MIT |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace allejo\stakx\Test; |
9
|
|
|
|
10
|
|
|
use allejo\stakx\Command\BuildableCommand; |
11
|
|
|
use allejo\stakx\Core\StakxLogger; |
12
|
|
|
use allejo\stakx\Manager\CollectionManager; |
13
|
|
|
use allejo\stakx\Service; |
14
|
|
|
use allejo\stakx\System\Filesystem; |
15
|
|
|
use allejo\stakx\System\Folder; |
16
|
|
|
use org\bovigo\vfs\vfsStream; |
17
|
|
|
use org\bovigo\vfs\vfsStreamDirectory; |
18
|
|
|
use org\bovigo\vfs\vfsStreamFile; |
19
|
|
|
use Psr\Log\LoggerInterface; |
20
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
21
|
|
|
use Symfony\Component\Yaml\Yaml; |
22
|
|
|
|
23
|
|
|
abstract class PHPUnit_Stakx_TestCase extends \PHPUnit_Framework_TestCase |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
const FM_OBJ_TEMPLATE = "---\n%s\n---\n\n%s"; |
26
|
|
|
|
27
|
|
|
/** @var string */ |
28
|
|
|
protected $assetFolder; |
29
|
|
|
|
30
|
|
|
/** |
31
|
|
|
* @var vfsStreamFile |
32
|
|
|
*/ |
33
|
|
|
protected $dummyFile; |
34
|
|
|
|
35
|
|
|
/** |
36
|
|
|
* @var vfsStreamDirectory |
37
|
|
|
*/ |
38
|
|
|
protected $rootDir; |
39
|
|
|
|
40
|
|
|
/** |
41
|
|
|
* @var Filesystem |
42
|
|
|
*/ |
43
|
|
|
protected $fs; |
|
|
|
|
44
|
|
|
|
45
|
|
|
public function setUp() |
46
|
|
|
{ |
47
|
|
|
$this->dummyFile = vfsStream::newFile('stakx.html.twig'); |
48
|
|
|
$this->rootDir = vfsStream::setup(); |
49
|
|
|
$this->fs = new Filesystem(); |
50
|
|
|
|
51
|
|
|
Service::setParameter(BuildableCommand::USE_DRAFTS, false); |
52
|
|
|
Service::setParameter(BuildableCommand::WATCHING, false); |
53
|
|
|
|
54
|
|
|
// Inspect the VFS as an array |
55
|
|
|
// vfsStream::inspect(new vfsStreamStructureVisitor())->getStructure(); |
|
|
|
|
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
/// |
59
|
|
|
// Assertion functions |
60
|
|
|
/// |
61
|
|
|
|
62
|
|
|
protected function assertStringContains($needle, $haystack, $message = '') |
63
|
|
|
{ |
64
|
|
|
$this->assertNotFalse(strpos($haystack, $needle), $message); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
protected function assertFileContains($fileContent, $filePath, $message = '') |
68
|
|
|
{ |
69
|
|
|
(substr($filePath, -1, 1) == '/') && $filePath .= 'index.html'; |
70
|
|
|
|
71
|
|
|
$contents = file_get_contents($filePath); |
72
|
|
|
|
73
|
|
|
$this->assertStringContains($fileContent, $contents, $message); |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
/// |
77
|
|
|
// Utility Functions |
78
|
|
|
/// |
79
|
|
|
|
80
|
|
|
protected function bookCollectionProvider($jailed = false) |
81
|
|
|
{ |
82
|
|
|
$cm = new CollectionManager(); |
|
|
|
|
83
|
|
|
$cm->setLogger($this->getMockLogger()); |
84
|
|
|
$cm->parseCollections(array( |
85
|
|
|
array( |
86
|
|
|
'name' => 'books', |
87
|
|
|
'folder' => 'tests/allejo/stakx/Test/assets/MyBookCollection/', |
88
|
|
|
), |
89
|
|
|
)); |
90
|
|
|
|
91
|
|
|
return (!$jailed) ? $cm->getCollections() : $cm->getJailedCollections(); |
92
|
|
|
} |
93
|
|
|
|
94
|
|
|
/** |
95
|
|
|
* Write a temporary file to the asset folder |
96
|
|
|
* |
97
|
|
|
* @param $fileName |
98
|
|
|
* @param $content |
99
|
|
|
* |
100
|
|
|
* @return string Path to the temporary file; relative to the project's root |
101
|
|
|
*/ |
102
|
|
|
protected function createTempFile($fileName, $content) |
103
|
|
|
{ |
104
|
|
|
$folder = new Folder($this->assetFolder); |
105
|
|
|
$folder->writeFile($fileName, $content); |
106
|
|
|
|
107
|
|
|
return $this->fs->appendPath($this->assetFolder, $fileName); |
108
|
|
|
} |
109
|
|
|
|
110
|
|
|
protected function createBlankFile($filename, $classType, $content) |
111
|
|
|
{ |
112
|
|
|
$file = vfsStream::newFile($filename); |
113
|
|
|
$file |
114
|
|
|
->setContent($content) |
115
|
|
|
->at($this->rootDir); |
116
|
|
|
|
117
|
|
|
return new $classType($file->url()); |
118
|
|
|
} |
119
|
|
|
|
120
|
|
|
/** |
121
|
|
|
* @param string $classType |
122
|
|
|
* @param array $frontMatter |
123
|
|
|
* @param string $body |
124
|
|
|
* |
125
|
|
|
* @return mixed |
126
|
|
|
*/ |
127
|
|
|
protected function createVirtualFile($classType, $frontMatter = array(), $body = 'Body Text') |
128
|
|
|
{ |
129
|
|
|
$this->dummyFile |
130
|
|
|
->setContent($this->generateFM($frontMatter, $body)) |
131
|
|
|
->at($this->rootDir); |
132
|
|
|
|
133
|
|
|
return new $classType($this->dummyFile->url()); |
134
|
|
|
} |
135
|
|
|
|
136
|
|
|
protected function createMultipleVirtualFiles($classType, $elements) |
137
|
|
|
{ |
138
|
|
|
$results = array(); |
139
|
|
|
|
140
|
|
|
foreach ($elements as $element) { |
141
|
|
|
$filename = (isset($element['filename'])) ? $element['filename'] : hash('sha256', uniqid(mt_rand(), true), false); |
142
|
|
|
$frontMatter = (empty($element['frontmatter'])) ? '' : Yaml::dump($element['frontmatter'], 2); |
143
|
|
|
$body = (isset($element['body'])) ? $element['body'] : 'Body Text'; |
144
|
|
|
|
145
|
|
|
$file = vfsStream::newFile($filename); |
146
|
|
|
$file |
147
|
|
|
->setContent(sprintf("---\n%s\n---\n\n%s", $frontMatter, $body)) |
148
|
|
|
->at($this->rootDir); |
149
|
|
|
|
150
|
|
|
$results[] = new $classType($file->url()); |
151
|
|
|
} |
152
|
|
|
|
153
|
|
|
return $results; |
154
|
|
|
} |
155
|
|
|
|
156
|
|
|
/** |
157
|
|
|
* Get a mock logger. |
158
|
|
|
* |
159
|
|
|
* @return LoggerInterface |
160
|
|
|
*/ |
161
|
|
|
protected function getMockLogger() |
162
|
|
|
{ |
163
|
|
|
return $this->getMock(LoggerInterface::class); |
164
|
|
|
} |
165
|
|
|
|
166
|
|
|
/** |
167
|
|
|
* Get a real logger instance that will save output to the console. |
168
|
|
|
* |
169
|
|
|
* @return StakxLogger |
170
|
|
|
*/ |
171
|
|
|
protected function getReadableLogger() |
172
|
|
|
{ |
173
|
|
|
stream_filter_register('intercept', StreamInterceptor::class); |
174
|
|
|
$stakxLogger = new StakxLogger(new ConsoleOutput()); |
175
|
|
|
stream_filter_append($stakxLogger->getOutputInterface()->getStream(), 'intercept'); |
|
|
|
|
176
|
|
|
|
177
|
|
|
return $stakxLogger; |
178
|
|
|
} |
179
|
|
|
|
180
|
|
|
/** |
181
|
|
|
* Generate a FrontMatter-ready syntax to be used as a file's content. |
182
|
|
|
* |
183
|
|
|
* @param array $frontMatter |
184
|
|
|
* @param string $body |
185
|
|
|
* |
186
|
|
|
* @return string |
187
|
|
|
*/ |
188
|
|
|
protected function generateFM(array $frontMatter = array(), $body = 'Body text') |
189
|
|
|
{ |
190
|
|
|
$fm = (empty($frontMatter)) ? '' : Yaml::dump($frontMatter, 2); |
|
|
|
|
191
|
|
|
|
192
|
|
|
return sprintf(self::FM_OBJ_TEMPLATE, $fm, $body); |
193
|
|
|
} |
194
|
|
|
|
195
|
|
|
/** |
196
|
|
|
* Create a temporary folder where temporary file writes will be made to |
197
|
|
|
* |
198
|
|
|
* Remember to remove the folder in during the ::tearDown() |
199
|
|
|
* |
200
|
|
|
* @param string $folderName |
201
|
|
|
*/ |
202
|
|
|
protected function createAssetFolder($folderName) |
203
|
|
|
{ |
204
|
|
|
$this->assetFolder = $this->fs->getRelativePath($this->fs->appendPath(__DIR__, $folderName)); |
205
|
|
|
|
206
|
|
|
$this->fs->mkdir($this->assetFolder); |
207
|
|
|
} |
208
|
|
|
} |
209
|
|
|
|
This check examines a number of code elements and verifies that they conform to the given naming conventions.
You can set conventions for local variables, abstract classes, utility classes, constant, properties, methods, parameters, interfaces, classes, exceptions and special methods.