Completed
Pull Request — master (#41)
by Vladimir
02:23
created

assertFileExistsAndContains()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 8
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
nc 1
nop 3
dl 0
loc 8
rs 9.4285
c 0
b 0
f 0
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\Manager\CollectionManager;
11
use allejo\stakx\System\Filesystem;
12
use org\bovigo\vfs\vfsStream;
13
use org\bovigo\vfs\vfsStreamDirectory;
14
use org\bovigo\vfs\vfsStreamFile;
15
use Psr\Log\LoggerInterface;
16
use Symfony\Component\Yaml\Yaml;
17
18
abstract class PHPUnit_Stakx_TestCase extends \PHPUnit_Framework_TestCase
0 ignored issues
show
Coding Style introduced by
This class is not in CamelCase format.

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.

Loading history...
19
{
20
    const FM_OBJ_TEMPLATE = "---\n%s\n---\n\n%s";
21
22
    /**
23
     * @var vfsStreamFile
24
     */
25
    protected $dummyFile;
26
27
    /**
28
     * @var vfsStreamDirectory
29
     */
30
    protected $rootDir;
31
32
    /**
33
     * @var Filesystem
34
     */
35
    protected $fs;
36
37
    public function setUp()
38
    {
39
        $this->dummyFile    = vfsStream::newFile('stakx.html.twig');
40
        $this->rootDir      = vfsStream::setup();
41
        $this->fs           = new Filesystem();
42
    }
43
44
    //
45
    // Assert Functions
46
    //
47
48
    public function assertFileExistsAndContains ($filePath, $needle, $message = '')
49
    {
50
        $this->assertFileExists($filePath, $message);
51
52
        $contents = file_get_contents($filePath);
53
54
        $this->assertContains($needle, $contents, $message);
55
    }
56
57
    //
58
    // Utility Functions
59
    //
60
61
    protected function bookCollectionProvider ($jailed = false)
62
    {
63
        $cm = new CollectionManager();
64
        $cm->setLogger($this->loggerMock());
65
        $cm->parseCollections(array(
66
            array(
67
                'name'   => 'books',
68
                'folder' => 'tests/allejo/stakx/Test/assets/MyBookCollection/'
69
            )
70
        ));
71
72
        return ($jailed) ? $cm->getCollections() : $cm->getJailedCollections();
73
    }
74
75
    /**
76
     * @param  string $classType
77
     * @param  array  $frontMatter
78
     * @param  string $body
79
     *
80
     * @return mixed
0 ignored issues
show
Documentation introduced by
Consider making the return type a bit more specific; maybe use object.

This check looks for the generic type array as a return type and suggests a more specific type. This type is inferred from the actual code.

Loading history...
81
     */
82
    protected function createVirtualFile ($classType, $frontMatter = array(), $body = "Body Text")
83
    {
84
        $fm = (empty($frontMatter)) ? '' : Yaml::dump($frontMatter, 2);
85
86
        $this->dummyFile
87
            ->setContent(sprintf("---\n%s\n---\n\n%s", $fm, $body))
88
            ->at($this->rootDir);
89
90
        return (new $classType($this->dummyFile->url()));
91
    }
92
93
    protected function createMultipleVirtualFiles ($classType, $elements)
94
    {
95
        $results = array();
96
97
        foreach ($elements as $element)
98
        {
99
            $filename = (isset($element['filename'])) ? $element['filename'] : hash('sha256', uniqid(mt_rand(), true), false);
100
            $frontMatter = (empty($element['frontmatter'])) ? '' : Yaml::dump($element['frontmatter'], 2);
101
            $body = (isset($element['body'])) ? $element['body'] : 'Body Text';
102
103
            $file = vfsStream::newFile($filename);
104
            $file
105
                ->setContent(sprintf("---\n%s\n---\n\n%s", $frontMatter, $body))
106
                ->at($this->rootDir);
107
108
            $results[] = new $classType($file->url());
109
        }
110
111
        return $results;
112
    }
113
114
    /**
115
     * @return LoggerInterface
116
     */
117
    protected function loggerMock ()
118
    {
119
        return $this->getMock(LoggerInterface::class);
120
    }
121
}