• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

RonasIT / laravel-entity-generator / 11889408991

18 Nov 2024 09:23AM UTC coverage: 39.537% (+15.1%) from 24.475%
11889408991

push

github

web-flow
Merge pull request #65 from RonasIT/49-add-tests

Cover generators with tests

4 of 5 new or added lines in 4 files covered. (80.0%)

376 of 951 relevant lines covered (39.54%)

0.81 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

93.02
/src/Generators/EntityGenerator.php
1
<?php
2

3
namespace RonasIT\Support\Generators;
4

5
use Illuminate\Filesystem\Filesystem;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Str;
8

9
/**
10
 * @property Filesystem $fs
11
 */
12
abstract class EntityGenerator
13
{
14
    const AVAILABLE_FIELDS = [
15
        'integer', 'integer-required', 'string-required', 'string', 'float-required', 'float',
16
        'boolean-required', 'boolean', 'timestamp-required', 'timestamp', 'json'
17
    ];
18

19
    protected $paths = [];
20
    protected $model;
21
    protected $fields;
22
    protected $relations;
23
    protected $crudOptions;
24

25
    /**
26
     * @param array $crudOptions
27
     * @return $this
28
     */
29
    public function setCrudOptions($crudOptions)
30
    {
31
        $this->crudOptions = $crudOptions;
2✔
32

33
        return $this;
2✔
34
    }
35

36
    /**
37
     * @param string $model
38
     * @return $this
39
     */
40
    public function setModel($model)
41
    {
42
        $this->model = Str::studly($model);
15✔
43

44
        return $this;
15✔
45
    }
46

47
    /**
48
     * @param array $fields
49
     * @return $this
50
     */
51
    public function setFields($fields)
52
    {
53
        $this->fields = $fields;
2✔
54

55
        return $this;
2✔
56
    }
57

58
    /**
59
     * @param array $relations
60
     * @return $this
61
     */
62
    public function setRelations($relations)
63
    {
64
        $this->relations = $relations;
2✔
65

66
        foreach ($relations['belongsTo'] as $field) {
2✔
67
            $name = Str::snake($field) . '_id';
2✔
68

69
            $this->fields['integer-required'][] = $name;
2✔
70
        }
71

72
        return $this;
2✔
73
    }
74

75
    public function __construct()
76
    {
77
        $this->paths = config('entity-generator.paths');
15✔
78
    }
79

80
    protected function getOrCreateNamespace(string $path): string
81
    {
82
        $path = $this->paths[$path];
6✔
83
        $pathParts = explode('/', $path);
6✔
84

85
        if (Str::endsWith(Arr::last($pathParts), '.php')) {
6✔
86
            array_pop($pathParts);
×
87
        }
88

89
        $namespace = array_map(function (string $part) {
6✔
90
            return ucfirst($part);
6✔
91
        }, $pathParts);
6✔
92

93
        $fullPath = base_path($path);
6✔
94

95
        if (!file_exists($fullPath)) {
6✔
96
            mkdir($fullPath, 0777, true);
4✔
97
        }
98

99
        return implode('\\', $namespace);
6✔
100
    }
101

102
    abstract public function generate(): void;
103

104
    protected function classExists($path, $name): bool
105
    {
106
        $entitiesPath = $this->paths[$path];
6✔
107

108
        $classPath = base_path("{$entitiesPath}/{$name}.php");
6✔
109

110
        return file_exists($classPath);
6✔
111
    }
112

113
    protected function saveClass($path, $name, $content, $additionalEntityFolder = false): string
114
    {
115
        $entitiesPath = base_path($this->paths[$path]);
5✔
116

117
        if ($additionalEntityFolder) {
5✔
118
            $entitiesPath = $entitiesPath . "/{$additionalEntityFolder}";
×
119
        }
120

121
        $classPath = "{$entitiesPath}/{$name}.php";
5✔
122
        $tag = "<?php";
5✔
123

124
        if (!Str::contains($content, $tag)) {
5✔
125
            $content = "{$tag}\n\n{$content}";
5✔
126
        }
127

128
        if (!file_exists($entitiesPath)) {
5✔
NEW
129
            mkdir($entitiesPath, 0777, true);
×
130
        }
131

132
        return file_put_contents($classPath, $content);
5✔
133
    }
134

135
    protected function getStub($stub, $data = []): string
136
    {
137
        $stubPath = config("entity-generator.stubs.{$stub}");
5✔
138

139
        $data['options'] = $this->crudOptions;
5✔
140

141
        return view($stubPath)->with($data)->render();
5✔
142
    }
143

144
    protected function getTableName($entityName, $delimiter = '_'): string
145
    {
146
        $entityName = Str::snake($entityName, $delimiter);
2✔
147

148
        return $this->getPluralName($entityName);
2✔
149
    }
150

151
    protected function getPluralName($entityName): string
152
    {
153
        return Str::plural($entityName);
3✔
154
    }
155

156
    protected function throwFailureException($exceptionClass, $failureMessage, $recommendedMessage): void
157
    {
158
        throw new $exceptionClass("{$failureMessage} {$recommendedMessage}");
7✔
159
    }
160
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc