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

RonasIT / laravel-entity-generator / 13150891945

05 Feb 2025 05:29AM UTC coverage: 90.022% (+1.6%) from 88.395%
13150891945

Pull #81

github

web-flow
Merge dff470277 into 882d04c57
Pull Request #81: Add service provider tests

830 of 922 relevant lines covered (90.02%)

4.08 hits per line

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

97.92
/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✔
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
            'modelNamespace' => $this->getOrCreateNamespace('models'),
2✔
55
        ]);
2✔
56

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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