Completed
Push — master ( 36acf5...646be7 )
by Alfred
03:12
created

AppendFile::addNewStepToTest()   A

Complexity

Conditions 3
Paths 2

Size

Total Lines 11
Code Lines 7

Duplication

Lines 4
Ratio 36.36 %

Code Coverage

Tests 9
CRAP Score 3

Importance

Changes 0
Metric Value
dl 4
loc 11
c 0
b 0
f 0
ccs 9
cts 9
cp 1
rs 9.4285
cc 3
eloc 7
nc 2
nop 2
crap 3
1
<?php
2
3
4
namespace GD\Helpers;
5
6
use GD\Exceptions\TestDoesNotFileExists;
7
use GD\Exceptions\TestFileExists;
8
9
class AppendFile extends WriteFileBase
10
{
11
12
    protected $updated_content;
13
    
14 12
    public function writeTest($path, $name, $dusk_class_and_methods)
15
    {
16 12
        $this->write_class_name = $name;
17
18 12
        $this->write_destination_folder_path = $path;
19
20 12
        $this->checkDestinationTestFolder();
21
22 9
        $this->convertDuskClassAndMethodsArrayToText($dusk_class_and_methods);
23
        
24 9
        $this->createUpdatedContent();
25
26 9
        $this->appendToFile();
27
28 9
        $this->saveToFile();
29 9
    }
30
    
31 9
    protected function createUpdatedContent()
32
    {
33 9
        $where_is_the_end = strrpos($this->getExistingTestContent(), "}");
34
35 9
        $existing_content = $this->getExistingTestContent();
36
37 9
        $this->updated_content = $this->appendExistingFileWithNewResults($existing_content, $where_is_the_end);
38
39 9
        $this->dusk_class_and_methods_string = $this->updated_content;
40 9
    }
41
42 9
    protected function appendToFile()
43
    {
44
45 9
        $this->dusk_class_and_methods_string = $this->dusk_class_and_methods_string . "\n}";
46 9
    }
47
48 9
    protected function addParentContent($parent_function)
49
    {
50 9
        $parent_base = __DIR__ . '/../../stubs/parent.txt';
51 9
        $this->base = $this->getFilesystem()->get($parent_base);
52
53 9
        parent::addParentContent($parent_function);
54 9
    }
55
56 9
    protected function addSteps(array $steps)
57
    {
58 9
        $path = __DIR__ . '/../../stubs/step.txt';
59 9
        $this->step_template = $this->getFilesystem()->get($path);
60
61 9
        parent::addSteps($steps);
62 9
    }
63
64 9
    protected function convertDuskClassAndMethodsArrayToText($dusk_class_and_methods)
65
    {
66
67
68 9
        foreach ($dusk_class_and_methods as $dusk_class_and_method_index => $dusk_class_and_method) {
69 9
            if (!$this->checkIfMethodInClass($dusk_class_and_method)) {
70 9
                $this->addParentContent($dusk_class_and_method['parent']);
71 6
            }
72
73 9
            $this->addSteps($dusk_class_and_method['steps']);
74 6
        }
75 9
    }
76
77 12
    protected function checkDestinationTestFolder()
78
    {
79 12
        $file_to_append = $this->write_destination_folder_path . '/' . $this->write_class_name . '.php';
80
81 12
        if (!$this->getFilesystem()->exists($file_to_append)) {
82 3
            throw new TestDoesNotFileExists(sprintf("Could not find the file to append to at %s", $file_to_append));
83
        } else {
84 9
            $content = $this->getFilesystem()->get($file_to_append);
85 9
            $this->setExistingTestContent($content);
86
        }
87 9
    }
88
89 9
    protected function addNewStepToTest($step, $step_template)
90
    {
91 9
        $method = sprintf("protected function %s()", $step['method_name']);
92 9
        $found_existing = substr_count($this->getExistingTestContent(), $method);
93 9
        $found = substr_count($this->dusk_class_and_methods_string, $method);
94
95 9 View Code Duplication
        if ($found < 1 && $found_existing < 1) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
96 9
            $content = str_replace("[STEP]", $step['method_name'], $step_template);
97 9
            $this->dusk_class_and_methods_string = $this->dusk_class_and_methods_string . $content;
98 6
        }
99 9
    }
100
101
    /**
102
     * @return mixed
103
     */
104 3
    public function getUpdatedContent()
105
    {
106 3
        return $this->updated_content;
107
    }
108
109
    protected function getAndSetFooterArea()
110
    {
111
        // TODO: Implement getAndSetFooterArea() method.
112
    }
113
114
    /**
115
     * @param $dusk_class_and_method
116
     * @return bool
117
     */
118 9
    protected function checkIfMethodInClass($dusk_class_and_method)
119
    {
120 9
        $parent = sprintf("function %s()", $dusk_class_and_method['parent']);
121
122 9
        return str_contains($this->getExistingTestContent(), $parent);
123
    }
124
125
    /**
126
     * @param $existing_content
127
     * @param $where_is_the_end
128
     * @return mixed
129
     */
130 9
    protected function appendExistingFileWithNewResults($existing_content, $where_is_the_end)
131
    {
132 9
        return substr_replace($existing_content, $this->dusk_class_and_methods_string, $where_is_the_end);
133
    }
134
}
135