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\Core\StakxLogger; |
11
|
|
|
use allejo\stakx\Manager\CollectionManager; |
12
|
|
|
use allejo\stakx\System\Filesystem; |
13
|
|
|
use org\bovigo\vfs\vfsStream; |
14
|
|
|
use org\bovigo\vfs\vfsStreamDirectory; |
15
|
|
|
use org\bovigo\vfs\vfsStreamFile; |
16
|
|
|
use Psr\Log\LoggerInterface; |
17
|
|
|
use Symfony\Component\Console\Output\ConsoleOutput; |
18
|
|
|
use Symfony\Component\Yaml\Yaml; |
19
|
|
|
|
20
|
|
|
abstract class PHPUnit_Stakx_TestCase extends \PHPUnit_Framework_TestCase |
|
|
|
|
21
|
|
|
{ |
22
|
|
|
const FM_OBJ_TEMPLATE = "---\n%s\n---\n\n%s"; |
23
|
|
|
|
24
|
|
|
/** |
25
|
|
|
* @var vfsStreamFile |
26
|
|
|
*/ |
27
|
|
|
protected $dummyFile; |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* @var vfsStreamDirectory |
31
|
|
|
*/ |
32
|
|
|
protected $rootDir; |
33
|
|
|
|
34
|
|
|
/** |
35
|
|
|
* @var Filesystem |
36
|
|
|
*/ |
37
|
|
|
protected $fs; |
38
|
|
|
|
39
|
|
|
public function setUp() |
40
|
|
|
{ |
41
|
|
|
$this->dummyFile = vfsStream::newFile('stakx.html.twig'); |
42
|
|
|
$this->rootDir = vfsStream::setup(); |
43
|
|
|
$this->fs = new Filesystem(); |
44
|
|
|
|
45
|
|
|
// Inspect the VFS as an array |
46
|
|
|
// vfsStream::inspect(new vfsStreamStructureVisitor())->getStructure(); |
|
|
|
|
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/// |
50
|
|
|
// Assertion functions |
51
|
|
|
/// |
52
|
|
|
|
53
|
|
|
protected function assertStringContains($needle, $haystack, $message = '') |
54
|
|
|
{ |
55
|
|
|
$this->assertNotFalse(strpos($haystack, $needle), $message); |
56
|
|
|
} |
57
|
|
|
|
58
|
|
|
protected function assertFileContains($fileContent, $filePath, $message = '') |
59
|
|
|
{ |
60
|
|
|
(substr($filePath, -1, 1) == '/') && $filePath .= 'index.html'; |
61
|
|
|
|
62
|
|
|
$contents = file_get_contents($filePath); |
63
|
|
|
|
64
|
|
|
$this->assertStringContains($fileContent, $contents, $message); |
65
|
|
|
} |
66
|
|
|
|
67
|
|
|
// |
68
|
|
|
// Utility Functions |
69
|
|
|
// |
70
|
|
|
|
71
|
|
|
protected function bookCollectionProvider($jailed = false) |
72
|
|
|
{ |
73
|
|
|
$cm = new CollectionManager(); |
74
|
|
|
$cm->setLogger($this->getMockLogger()); |
75
|
|
|
$cm->parseCollections(array( |
76
|
|
|
array( |
77
|
|
|
'name' => 'books', |
78
|
|
|
'folder' => 'tests/allejo/stakx/Test/assets/MyBookCollection/', |
79
|
|
|
), |
80
|
|
|
)); |
81
|
|
|
|
82
|
|
|
return ($jailed) ? $cm->getCollections() : $cm->getJailedCollections(); |
83
|
|
|
} |
84
|
|
|
|
85
|
|
|
/** |
86
|
|
|
* @param string $classType |
87
|
|
|
* @param array $frontMatter |
88
|
|
|
* @param string $body |
89
|
|
|
* |
90
|
|
|
* @return mixed |
|
|
|
|
91
|
|
|
*/ |
92
|
|
|
protected function createVirtualFile($classType, $frontMatter = array(), $body = 'Body Text') |
93
|
|
|
{ |
94
|
|
|
$this->dummyFile |
95
|
|
|
->setContent($this->generateFM($frontMatter, $body)) |
96
|
|
|
->at($this->rootDir); |
97
|
|
|
|
98
|
|
|
return new $classType($this->dummyFile->url()); |
99
|
|
|
} |
100
|
|
|
|
101
|
|
|
protected function createMultipleVirtualFiles($classType, $elements) |
102
|
|
|
{ |
103
|
|
|
$results = array(); |
104
|
|
|
|
105
|
|
|
foreach ($elements as $element) { |
106
|
|
|
$filename = (isset($element['filename'])) ? $element['filename'] : hash('sha256', uniqid(mt_rand(), true), false); |
107
|
|
|
$frontMatter = (empty($element['frontmatter'])) ? '' : Yaml::dump($element['frontmatter'], 2); |
108
|
|
|
$body = (isset($element['body'])) ? $element['body'] : 'Body Text'; |
109
|
|
|
|
110
|
|
|
$file = vfsStream::newFile($filename); |
111
|
|
|
$file |
112
|
|
|
->setContent(sprintf("---\n%s\n---\n\n%s", $frontMatter, $body)) |
113
|
|
|
->at($this->rootDir); |
114
|
|
|
|
115
|
|
|
$results[] = new $classType($file->url()); |
116
|
|
|
} |
117
|
|
|
|
118
|
|
|
return $results; |
119
|
|
|
} |
120
|
|
|
|
121
|
|
|
/** |
122
|
|
|
* Get a mock logger. |
123
|
|
|
* |
124
|
|
|
* @return LoggerInterface |
125
|
|
|
*/ |
126
|
|
|
protected function getMockLogger() |
127
|
|
|
{ |
128
|
|
|
return $this->getMock(LoggerInterface::class); |
129
|
|
|
} |
130
|
|
|
|
131
|
|
|
/** |
132
|
|
|
* Get a real logger instance that will save output to the console. |
133
|
|
|
* |
134
|
|
|
* @return StakxLogger |
135
|
|
|
*/ |
136
|
|
|
protected function getReadableLogger() |
137
|
|
|
{ |
138
|
|
|
stream_filter_register('intercept', StreamInterceptor::class); |
139
|
|
|
$stakxLogger = new StakxLogger(new ConsoleOutput()); |
140
|
|
|
stream_filter_append($stakxLogger->getOutputInterface()->getStream(), 'intercept'); |
|
|
|
|
141
|
|
|
|
142
|
|
|
return $stakxLogger; |
143
|
|
|
} |
144
|
|
|
|
145
|
|
|
/** |
146
|
|
|
* Generate a FrontMatter-ready syntax to be used as a file's content. |
147
|
|
|
* |
148
|
|
|
* @param array $frontMatter |
149
|
|
|
* @param string $body |
150
|
|
|
* |
151
|
|
|
* @return string |
152
|
|
|
*/ |
153
|
|
|
protected function generateFM(array $frontMatter = array(), $body = 'Body text') |
154
|
|
|
{ |
155
|
|
|
$fm = (empty($frontMatter)) ? '' : Yaml::dump($frontMatter, 2); |
156
|
|
|
|
157
|
|
|
return sprintf(self::FM_OBJ_TEMPLATE, $fm, $body); |
158
|
|
|
} |
159
|
|
|
} |
160
|
|
|
|
Classes in PHP are usually named in CamelCase.
In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. The whole name starts with a capital letter as well.
Thus the name database provider becomes
DatabaseProvider
.