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

RonasIT / laravel-entity-generator / 12904129135

22 Jan 2025 08:27AM UTC coverage: 80.326% (+3.2%) from 77.174%
12904129135

Pull #77

github

web-flow
Merge 20d923d1c into 926bbfb73
Pull Request #77: Add service generator tests

3 of 3 new or added lines in 1 file covered. (100.0%)

739 of 920 relevant lines covered (80.33%)

2.93 hits per line

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

94.74
/src/Generators/MigrationGenerator.php
1
<?php
2

3
namespace RonasIT\Support\Generators;
4

5
use Carbon\Carbon;
6
use Illuminate\Support\Str;
7
use RonasIT\Support\Events\SuccessCreateMessage;
8
use RonasIT\Support\Exceptions\UnknownFieldTypeException;
9

10
class MigrationGenerator extends EntityGenerator
11
{
12
    public function generate(): void
13
    {
14
        if (!$this->isStubExists('migration')) {
3✔
15
            return;
×
16
        }
17

18
        $entities = $this->getTableName($this->model);
3✔
19

20
        $content = $this->getStub('migration', [
3✔
21
            'class' => $this->getPluralName($this->model),
3✔
22
            'entity' => $this->model,
3✔
23
            'entities' => $entities,
3✔
24
            'relations' => $this->relations,
3✔
25
            'fields' => $this->fields,
3✔
26
            'table' => $this->generateTable($this->fields)
3✔
27
        ]);
3✔
28

29
        $now = Carbon::now()->format('Y_m_d_His');
2✔
30

31
        $this->saveClass('migrations', "{$now}_{$entities}_create_table", $content);
2✔
32

33
        event(new SuccessCreateMessage("Created a new Migration: {$entities}_create_table"));
2✔
34
    }
35

36
    protected function isJson(string $typeName): bool
37
    {
38
        return $typeName === 'json';
3✔
39
    }
40

41
    protected function isRequired(string $typeName): bool
42
    {
43
        return Str::afterLast($typeName, '-') === 'required';
3✔
44
    }
45

46
    protected function isNullable(string $typeName): bool
47
    {
48
        return empty(explode('-', $typeName)[1]);
3✔
49
    }
50

51
    protected function getJsonLine(string $fieldName): string
52
    {
53
        if (env("DB_CONNECTION") == "mysql") {
2✔
54
            return "\$table->json('{$fieldName}')->nullable();";
1✔
55
        }
56

57
        return "\$table->jsonb('{$fieldName}')->default(\"{}\");";
1✔
58
    }
59

60
    protected function getRequiredLine(string $fieldName, string $typeName): string
61
    {
62
        $type = explode('-', $typeName)[0];
3✔
63

64
        if ($type === 'timestamp' && env('DB_CONNECTION') === 'mysql') {
3✔
65
            return "\$table->{$type}('{$fieldName}')->nullable();";
×
66
        }
67

68
        return "\$table->{$type}('{$fieldName}');";
3✔
69
    }
70

71
    protected function getNonRequiredLine(string $fieldName, string $typeName): string
72
    {
73
        $type = explode('-', $typeName)[0];
2✔
74

75
        return "\$table->{$type}('{$fieldName}')->nullable();";
2✔
76
    }
77

78
    protected function generateTable(array $fields): array
79
    {
80
        $resultTable = [];
3✔
81

82
        foreach ($fields as $typeName => $fieldNames) {
3✔
83
            foreach ($fieldNames as $fieldName) {
3✔
84
                $resultTable[] = $this->getTableRow($fieldName, $typeName);
3✔
85
            }
86
        }
87

88
        return $resultTable;
2✔
89
    }
90

91
    protected function getTableRow(string $fieldName, string $typeName): string
92
    {
93
        if ($this->isJson($typeName)) {
3✔
94
            return $this->getJsonLine($fieldName);
2✔
95
        }
96

97
        if ($this->isRequired($typeName)) {
3✔
98
            return $this->getRequiredLine($fieldName, $typeName);
3✔
99
        }
100

101
        if ($this->isNullable($typeName)) {
3✔
102
            return $this->getNonRequiredLine($fieldName, $typeName);
2✔
103
        }
104

105
        throw new UnknownFieldTypeException($typeName, 'MigrationGenerator');
1✔
106
    }
107
}
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