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

RonasIT / laravel-entity-generator / 12195557862

06 Dec 2024 08:33AM UTC coverage: 48.31% (+0.1%) from 48.198%
12195557862

push

github

web-flow
Merge pull request #108 from RonasIT/skip-generation-for-invalid-stubs

feat: skip generation for invalid stubs

33 of 55 new or added lines in 15 files covered. (60.0%)

1 existing line in 1 file now uncovered.

443 of 917 relevant lines covered (48.31%)

1.65 hits per line

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

97.87
/src/Generators/FactoryGenerator.php
1
<?php
2

3
namespace RonasIT\Support\Generators;
4

5
use Faker\Generator as Faker;
6
use Illuminate\Support\Arr;
7
use Illuminate\Support\Str;
8
use InvalidArgumentException;
9
use RonasIT\Support\Exceptions\FakerMethodNotFoundException;
10
use RonasIT\Support\Exceptions\ClassNotExistsException;
11
use RonasIT\Support\Exceptions\ClassAlreadyExistsException;
12
use RonasIT\Support\Events\SuccessCreateMessage;
13

14
class FactoryGenerator extends EntityGenerator
15
{
16
    const array FAKERS_METHODS = [
17
        'integer' => 'randomNumber()',
18
        'boolean' => 'boolean',
19
        'string' => 'word',
20
        'float' => 'randomFloat(2, 0, 10000)',
21
        'timestamp' => 'dateTime',
22
    ];
23

24
    const array CUSTOM_METHODS = [
25
        'json' => '[]',
26
    ];
27

28
    public function generate(): void
29
    {
30
        if (!$this->classExists('models', $this->model)) {
4✔
31
            $this->throwFailureException(
1✔
32
                exceptionClass: ClassNotExistsException::class,
1✔
33
                failureMessage: "Cannot create {$this->model}Factory cause {$this->model} Model does not exists.",
1✔
34
                recommendedMessage: "Create a {$this->model} Model by itself or run command 'php artisan make:entity {$this->model} --only-model'.",
1✔
35
            );
1✔
36
        }
37

38
        if ($this->classExists('factories', "{$this->model}Factory")) {
3✔
39
            $this->throwFailureException(
1✔
40
                exceptionClass: ClassAlreadyExistsException::class,
1✔
41
                failureMessage: "Cannot create {$this->model}Factory cause {$this->model}Factory already exists.",
1✔
42
                recommendedMessage: "Remove {$this->model}Factory.",
1✔
43
            );
1✔
44
        }
45

46
        if (!$this->isStubExists('factory')) {
2✔
NEW
47
            return;
×
48
        }
49

50
        $factoryContent = $this->getStub('factory', [
2✔
51
            'namespace' => $this->getOrCreateNamespace('factories'),
2✔
52
            'entity' => $this->model,
2✔
53
            'fields' => $this->prepareFields(),
2✔
54
        ]);
2✔
55

56
        $this->saveClass('factories', "{$this->model}Factory", $factoryContent);
1✔
57

58
        event(new SuccessCreateMessage("Created a new Factory: {$this->model}Factory"));
1✔
59
    }
60

61
    protected static function getFakerMethod($field): string
62
    {
63
        if (Arr::has(self::FAKERS_METHODS, $field['type'])) {
2✔
64
            return "\$faker->" . self::FAKERS_METHODS[$field['type']];
1✔
65
        }
66

67
        return self::getCustomMethod($field);
2✔
68
    }
69

70
    protected static function getCustomMethod($field): string
71
    {
72
        if (Arr::has(self::CUSTOM_METHODS, $field['type'])) {
2✔
73
            return self::CUSTOM_METHODS[$field['type']];
1✔
74
        }
75

76
        $message = "Cannot generate fake data for unsupported {$field['type']} field type. "
1✔
77
            . "Supported custom field types are " . implode(', ', array_keys(self::CUSTOM_METHODS));
1✔
78

79
        throw new FakerMethodNotFoundException($message);
1✔
80
    }
81

82
    public static function getFactoryFieldsContent($field): string
83
    {
84
        /** @var Faker $faker */
85
        $faker = app(Faker::class);
2✔
86

87
        if (preg_match('/_id$/', $field['name']) || ($field['name'] == 'id')) {
2✔
88
            return 1;
1✔
89
        }
90

91
        try {
92
            $faker->{$field['name']};
2✔
93
            $hasFormatter = true;
1✔
94
        } catch (InvalidArgumentException $e) {
2✔
95
            $hasFormatter = false;
2✔
96
        }
97

98
        if ($hasFormatter) {
2✔
99
            return "\$faker->{$field['name']}";
1✔
100
        }
101

102
        return self::getFakerMethod($field);
2✔
103
    }
104

105
    protected function prepareFields(): array
106
    {
107
        $result = [];
2✔
108

109
        foreach ($this->fields as $type => $fields) {
2✔
110
            foreach ($fields as $field) {
2✔
111
                $result[] = [
2✔
112
                    'name' => $field,
2✔
113
                    'type' => Str::before($type, '-'),
2✔
114
                ];
2✔
115
            }
116
        }
117

118
        return $result;
2✔
119
    }
120
}
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