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

RonasIT / laravel-entity-generator / 21166733718

20 Jan 2026 09:43AM UTC coverage: 99.432% (-0.6%) from 100.0%
21166733718

Pull #241

github

web-flow
Merge 2b61df918 into 962b971b7
Pull Request #241: [239]: append field modifiers instead of uppercase options

165 of 167 new or added lines in 17 files covered. (98.8%)

4 existing lines in 2 files now uncovered.

1051 of 1057 relevant lines covered (99.43%)

11.39 hits per line

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

92.11
/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 InvalidArgumentException;
8
use RonasIT\Support\Events\SuccessCreateMessage;
9
use RonasIT\Support\Exceptions\FakerMethodNotFoundException;
10

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

21
    const array CUSTOM_METHODS = [
22
        'json' => '[]',
23
    ];
24

25
    public function generate(): void
26
    {
27
        $this->checkResourceNotExists('models', "{$this->model}Factory", $this->model, $this->modelSubFolder);
8✔
28

29
        $this->checkResourceExists('factories', "{$this->model}Factory");
7✔
30

31
        if (!$this->isStubExists('factory')) {
6✔
32
            return;
1✔
33
        }
34

35
        $this->createNamespace('factories');
5✔
36

37
        $factoryContent = $this->getStub('factory', [
5✔
38
            'namespace' => $this->generateNamespace($this->paths['factories']),
5✔
39
            'entity' => $this->model,
5✔
40
            'fields' => $this->prepareFields(),
5✔
41
            'modelNamespace' => $this->generateNamespace($this->paths['models'], $this->modelSubFolder),
5✔
42
        ]);
5✔
43

44
        $this->saveClass('factories', "{$this->model}Factory", $factoryContent);
5✔
45

46
        event(new SuccessCreateMessage("Created a new Factory: {$this->model}Factory"));
5✔
47
    }
48

49
    protected static function getFakerMethod($field): string
50
    {
51
        if (Arr::has(self::FAKERS_METHODS, $field['type'])) {
2✔
52
            return '$faker->' . self::FAKERS_METHODS[$field['type']];
2✔
53
        }
54

55
        return self::getCustomMethod($field);
2✔
56
    }
57

58
    protected static function getCustomMethod($field): string
59
    {
60
        if (Arr::has(self::CUSTOM_METHODS, $field['type'])) {
2✔
61
            return self::CUSTOM_METHODS[$field['type']];
2✔
62
        }
63

UNCOV
64
        $message = "Cannot generate fake data for unsupported {$field['type']} field type. "
×
UNCOV
65
            . 'Supported custom field types are ' . implode(', ', array_keys(self::CUSTOM_METHODS));
×
66

UNCOV
67
        throw new FakerMethodNotFoundException($message);
×
68
    }
69

70
    public static function getFactoryFieldsContent($field): string
71
    {
72
        /** @var Faker $faker */
73
        $faker = app(Faker::class);
2✔
74

75
        if (preg_match('/_id$/', $field['name']) || ($field['name'] == 'id')) {
2✔
76
            return 1;
2✔
77
        }
78

79
        try {
80
            $faker->{$field['name']};
2✔
81
            $hasFormatter = true;
2✔
82
        } catch (InvalidArgumentException $e) {
2✔
83
            $hasFormatter = false;
2✔
84
        }
85

86
        if ($hasFormatter) {
2✔
87
            return "\$faker->{$field['name']}";
2✔
88
        }
89

90
        return self::getFakerMethod($field);
2✔
91
    }
92

93
    protected function prepareFields(): array
94
    {
95
        $result = [];
5✔
96

97
        foreach ($this->fields as $field) {
5✔
98
            $result[] = [
2✔
99
                'name' => $field->name,
2✔
100
                'type' => $field->type->value,
2✔
101
            ];
2✔
102
        }
103

104
        return $result;
5✔
105
    }
106
}
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