AppendFileBase::addNewStepToTest()   A
last analyzed

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
c 0
b 0
f 0
dl 4
loc 11
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
abstract class AppendFileBase extends WriteFileBase
10
{
11
    protected $updated_content;
12
13 24
    public function writeTest($path, $name, $dusk_class_and_methods)
14
    {
15
16 24
        $this->write_class_name = $name;
17
18 24
        $this->write_destination_folder_path = $path;
19
20 24
        $this->checkDestinationTestFolder();
21
22 18
        $this->convertDuskClassAndMethodsArrayToText($dusk_class_and_methods);
23
24 18
        $this->createUpdatedContent();
25
26 18
        $this->appendToFile();
27
28 18
        $this->saveToFile();
29 18
    }
30
31
32
33 18
    protected function createUpdatedContent()
34
    {
35 18
        $where_is_the_end = strrpos($this->getExistingTestContent(), "}");
36
37 18
        $existing_content = $this->getExistingTestContent();
38
39 18
        $this->updated_content = $this->appendExistingFileWithNewResults($existing_content, $where_is_the_end);
40
41 18
        $this->dusk_class_and_methods_string = $this->updated_content;
42 18
    }
43
44 18
    protected function appendToFile()
45
    {
46
47 18
        $this->dusk_class_and_methods_string = $this->dusk_class_and_methods_string . "\n}";
48 18
    }
49
50
51 18
    protected function convertDuskClassAndMethodsArrayToText($dusk_class_and_methods)
52
    {
53
54
55 18
        foreach ($dusk_class_and_methods as $dusk_class_and_method_index => $dusk_class_and_method) {
56 18
            if (!$this->checkIfMethodInClass($dusk_class_and_method)) {
57 18
                $this->addParentContent($dusk_class_and_method['parent']);
58 12
            }
59
60 18
            $this->addSteps($dusk_class_and_method['steps']);
61 12
        }
62 18
    }
63
64 24
    protected function checkDestinationTestFolder()
65
    {
66 24
        $file_to_append = $this->write_destination_folder_path . '/' . $this->write_class_name . '.php';
67
68 24
        if (!$this->getFilesystem()->exists($file_to_append)) {
69 6
            throw new TestDoesNotFileExists(sprintf("Could not find the file to append to at %s", $file_to_append));
70
        } else {
71 18
            $content = $this->getFilesystem()->get($file_to_append);
72 18
            $this->setExistingTestContent($content);
73
        }
74 18
    }
75
76 18
    protected function addNewStepToTest($step, $step_template)
77
    {
78 18
        $method = sprintf("protected function %s()", $step['method_name']);
79 18
        $found_existing = substr_count($this->getExistingTestContent(), $method);
80 18
        $found = substr_count($this->dusk_class_and_methods_string, $method);
81
82 18 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...
83 18
            $content = str_replace(["[STEP]", "[METHOD]"], $step['method_name'], $step_template);
84 18
            $this->dusk_class_and_methods_string = $this->dusk_class_and_methods_string . $content;
85 12
        }
86 18
    }
87
88
    /**
89
     * @return mixed
90
     */
91 6
    public function getUpdatedContent()
92
    {
93 6
        return $this->updated_content;
94
    }
95
96
    protected function getAndSetFooterArea()
97
    {
98
        // TODO: Implement getAndSetFooterArea() method.
99
    }
100
101
    /**
102
     * @param $dusk_class_and_method
103
     * @return bool
104
     */
105 18
    protected function checkIfMethodInClass($dusk_class_and_method)
106
    {
107 18
        $parent = sprintf("function %s()", $dusk_class_and_method['parent']);
108
109 18
        return str_contains($this->getExistingTestContent(), $parent);
110
    }
111
112
    /**
113
     * @param $existing_content
114
     * @param $where_is_the_end
115
     * @return mixed
116
     */
117 18
    protected function appendExistingFileWithNewResults($existing_content, $where_is_the_end)
118
    {
119 18
        return substr_replace($existing_content, $this->dusk_class_and_methods_string, $where_is_the_end);
120
    }
121
}
122