ThemeContext::cleanThemesAfterScenario()   A
last analyzed

Complexity

Conditions 6
Paths 6

Size

Total Lines 26
Code Lines 12

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 6
eloc 12
nc 6
nop 0
dl 0
loc 26
rs 9.2222
c 0
b 0
f 0
1
<?php
2
3
namespace SilverStripe\CMS\Tests\Behaviour;
4
5
use Behat\Behat\Context\Context;
0 ignored issues
show
Bug introduced by
The type Behat\Behat\Context\Context was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
6
use SilverStripe\BehatExtension\Context\MainContextAwareTrait;
0 ignored issues
show
Bug introduced by
The type SilverStripe\BehatExtens...t\MainContextAwareTrait was not found. Maybe you did not declare it correctly or list all dependencies?

The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g. excluded_paths: ["lib/*"], you can move it to the dependency path list as follows:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
7
8
/**
9
 * Context used to create fixtures in the SilverStripe ORM.
10
 */
11
class ThemeContext implements Context
12
{
13
    use MainContextAwareTrait;
14
15
    protected $restoreFiles = array();
16
    protected $restoreDirectories = array();
17
18
    /**
19
     * Create a test theme
20
     *
21
     * @Given /^a theme "(?<theme>[^"]+)"/
22
     * @param string $theme
23
     */
24
    public function stepCreateTheme($theme)
25
    {
26
        if (!preg_match('/^[0-9a-zA-Z_-]+$/', $theme)) {
27
            throw new \InvalidArgumentException("Bad theme '$theme'");
28
        }
29
30
        $this->requireDir(BASE_PATH . '/themes');
31
        $this->requireDir(BASE_PATH . '/themes/' . $theme);
32
        $this->requireDir(BASE_PATH . '/themes/' . $theme . '/templates');
33
    }
34
35
    /**
36
     * Create a template within a test theme
37
     *
38
     * @Given /^a template "(?<template>[^"]+)" in theme "(?<theme>[^"]+)" with content "(?<content>[^"]+)"/
39
     * @param string $template
40
     * @param string $theme
41
     * @param string $content
42
     */
43
    public function stepCreateTemplate($template, $theme, $content)
44
    {
45
        if (!preg_match('/^[0-9a-zA-Z_-]+$/', $theme)) {
46
            throw new \InvalidArgumentException("Bad theme '$theme'");
47
        }
48
        if (!preg_match('/^(Layout\/)?[0-9a-zA-Z_-]+\.ss$/', $template)) {
49
            throw new \InvalidArgumentException("Bad template '$template'");
50
        }
51
52
        $this->stepCreateTheme($theme);
53
        $this->requireFile(BASE_PATH . '/themes/' . $theme . '/templates/' . $template, $content);
54
    }
55
56
    protected function requireFile($filename, $content)
57
    {
58
        // Already exists
59
        if (file_exists($filename)) {
60
            // If the content is different, remember old content for restoration
61
            $origContent = file_get_contents($filename);
62
            if ($origContent != $content) {
63
                file_put_contents($filename, $content);
64
                $this->restoreFiles[$filename] = $origContent;
65
            }
66
        // Doesn't exist, mark it for deletion after test
67
        } else {
68
            file_put_contents($filename, $content);
69
            $this->restoreFiles[$filename] = null;
70
        }
71
    }
72
73
    protected function requireDir($dirname)
74
    {
75
        // Directory doesn't exist, create it and mark it for deletion
76
        if (!file_exists($dirname)) {
77
            mkdir($dirname);
78
            $this->restoreDirectories[] = $dirname;
79
        }
80
    }
81
82
    /**
83
     * Clean up any theme manipulation
84
     *
85
     * @AfterScenario
86
     */
87
    public function cleanThemesAfterScenario()
88
    {
89
        // Restore any created/modified files.
90
        //  - If modified, revert then to original contnet
91
        //  - If created, delete them
92
        if ($this->restoreFiles) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->restoreFiles of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
93
            foreach ($this->restoreFiles as $file => $origContent) {
94
                if ($origContent === null) {
95
                    unlink($file);
96
                } else {
97
                    file_put_contents($file, $origContent);
98
                }
99
            }
100
101
            $this->restoreFiles = array();
102
        }
103
104
        // Restore any created directories: that is, delete them
105
        if ($this->restoreDirectories) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->restoreDirectories of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using ! empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
106
            // Flip the order so that nested direcotires are unlinked() first
107
            $this->restoreDirectories = array_reverse($this->restoreDirectories);
108
            foreach ($this->restoreDirectories as $dir) {
109
                rmdir($dir);
110
            }
111
112
            $this->restoreDirectories = array();
113
        }
114
    }
115
}
116