1 | <?php |
||
21 | abstract class PHPUnit_Stakx_TestCase extends \PHPUnit_Framework_TestCase |
||
22 | { |
||
23 | const FM_OBJ_TEMPLATE = "---\n%s\n---\n\n%s"; |
||
24 | |||
25 | /** @var string */ |
||
26 | protected $assetFolder; |
||
27 | |||
28 | /** |
||
29 | * @var vfsStreamFile |
||
30 | */ |
||
31 | protected $dummyFile; |
||
32 | |||
33 | /** |
||
34 | * @var vfsStreamDirectory |
||
35 | */ |
||
36 | protected $rootDir; |
||
37 | |||
38 | /** |
||
39 | * @var Filesystem |
||
40 | */ |
||
41 | protected $fs; |
||
42 | |||
43 | public function setUp() |
||
44 | { |
||
45 | $this->dummyFile = vfsStream::newFile('stakx.html.twig'); |
||
46 | $this->rootDir = vfsStream::setup(); |
||
47 | $this->fs = new Filesystem(); |
||
48 | |||
49 | // Inspect the VFS as an array |
||
50 | // vfsStream::inspect(new vfsStreamStructureVisitor())->getStructure(); |
||
51 | } |
||
52 | |||
53 | /// |
||
54 | // Assertion functions |
||
55 | /// |
||
56 | |||
57 | protected function assertStringContains($needle, $haystack, $message = '') |
||
58 | { |
||
59 | $this->assertNotFalse(strpos($haystack, $needle), $message); |
||
60 | } |
||
61 | |||
62 | protected function assertFileContains($fileContent, $filePath, $message = '') |
||
63 | { |
||
64 | (substr($filePath, -1, 1) == '/') && $filePath .= 'index.html'; |
||
65 | |||
66 | $contents = file_get_contents($filePath); |
||
67 | |||
68 | $this->assertStringContains($fileContent, $contents, $message); |
||
69 | } |
||
70 | |||
71 | /// |
||
72 | // Utility Functions |
||
73 | /// |
||
74 | |||
75 | protected function bookCollectionProvider($jailed = false) |
||
76 | { |
||
77 | $cm = new CollectionManager(); |
||
78 | $cm->setLogger($this->getMockLogger()); |
||
79 | $cm->parseCollections(array( |
||
80 | array( |
||
81 | 'name' => 'books', |
||
82 | 'folder' => 'tests/allejo/stakx/Test/assets/MyBookCollection/', |
||
83 | ), |
||
84 | )); |
||
85 | |||
86 | return (!$jailed) ? $cm->getCollections() : $cm->getJailedCollections(); |
||
87 | } |
||
88 | |||
89 | /** |
||
90 | * @param string $classType |
||
91 | * @param array $frontMatter |
||
92 | * @param string $body |
||
93 | * |
||
94 | * @return mixed |
||
95 | */ |
||
96 | protected function createVirtualFile($classType, $frontMatter = array(), $body = 'Body Text') |
||
97 | { |
||
98 | $this->dummyFile |
||
99 | ->setContent($this->generateFM($frontMatter, $body)) |
||
100 | ->at($this->rootDir); |
||
101 | |||
102 | return new $classType($this->dummyFile->url()); |
||
103 | } |
||
104 | |||
105 | protected function createMultipleVirtualFiles($classType, $elements) |
||
106 | { |
||
107 | $results = array(); |
||
108 | |||
109 | foreach ($elements as $element) { |
||
110 | $filename = (isset($element['filename'])) ? $element['filename'] : hash('sha256', uniqid(mt_rand(), true), false); |
||
111 | $frontMatter = (empty($element['frontmatter'])) ? '' : Yaml::dump($element['frontmatter'], 2); |
||
112 | $body = (isset($element['body'])) ? $element['body'] : 'Body Text'; |
||
113 | |||
114 | $file = vfsStream::newFile($filename); |
||
115 | $file |
||
116 | ->setContent(sprintf("---\n%s\n---\n\n%s", $frontMatter, $body)) |
||
117 | ->at($this->rootDir); |
||
118 | |||
119 | $results[] = new $classType($file->url()); |
||
120 | } |
||
121 | |||
122 | return $results; |
||
123 | } |
||
124 | |||
125 | /** |
||
126 | * Get a mock logger. |
||
127 | * |
||
128 | * @return LoggerInterface |
||
129 | */ |
||
130 | protected function getMockLogger() |
||
134 | |||
135 | /** |
||
136 | * Get a real logger instance that will save output to the console. |
||
137 | * |
||
138 | * @return StakxLogger |
||
139 | */ |
||
140 | protected function getReadableLogger() |
||
148 | |||
149 | /** |
||
150 | * Generate a FrontMatter-ready syntax to be used as a file's content. |
||
151 | * |
||
152 | * @param array $frontMatter |
||
153 | * @param string $body |
||
154 | * |
||
155 | * @return string |
||
156 | */ |
||
157 | protected function generateFM(array $frontMatter = array(), $body = 'Body text') |
||
163 | |||
164 | /** |
||
165 | * Create a temporary folder where temporary file writes will be made to |
||
166 | * |
||
167 | * Remember to remove the folder in during the ::tearDown() |
||
168 | * |
||
169 | * @param string $folderName |
||
170 | */ |
||
171 | protected function createAssetFolder($folderName) |
||
177 | |||
178 | /** |
||
179 | * Write a temporary file to the asset folder |
||
180 | * |
||
181 | * @param $fileName |
||
182 | * @param $content |
||
183 | * |
||
184 | * @return string Path to the temporary file; relative to the project's root |
||
185 | */ |
||
186 | protected function writeTempFile($fileName, $content) |
||
193 | } |
||
194 |
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.