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

RonasIT / laravel-helpers / 3929967872

pending completion
3929967872

Pull #52

github

GitHub
Merge e8557eed9 into 71346419b
Pull Request #52: feat: update http request service to use easy mock;

32 of 32 new or added lines in 3 files covered. (100.0%)

150 of 910 relevant lines covered (16.48%)

1.26 hits per line

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

37.31
/src/Traits/FixturesTrait.php
1
<?php
2

3
namespace RonasIT\Support\Traits;
4

5
use Illuminate\Support\Arr;
6
use Illuminate\Support\Facades\DB;
7
use Illuminate\Foundation\Testing\TestResponse;
8

9
trait FixturesTrait
10
{
11
    protected static $tables;
12
    protected $postgisTables = [
13
        'tiger.addrfeat',
14
        'tiger.edges',
15
        'tiger.faces',
16
        'topology.topology',
17
        'tiger.place_lookup',
18
        'topology.layer',
19
        'tiger.geocode_settings',
20
        'tiger.geocode_settings_default',
21
        'tiger.direction_lookup',
22
        'tiger.secondary_unit_lookup',
23
        'tiger.state_lookup',
24
        'tiger.street_type_lookup',
25
        'tiger.county_lookup',
26
        'tiger.countysub_lookup',
27
        'tiger.zip_lookup_all',
28
        'tiger.zip_lookup_base',
29
        'tiger.zip_lookup',
30
        'tiger.county',
31
        'tiger.state',
32
        'tiger.place',
33
        'tiger.zip_state',
34
        'tiger.zip_state_loc',
35
        'tiger.cousub',
36
        'tiger.featnames',
37
        'tiger.addr',
38
        'tiger.zcta5',
39
        'tiger.loader_platform',
40
        'tiger.loader_variables',
41
        'tiger.loader_lookuptables',
42
        'tiger.tract',
43
        'tiger.tabblock',
44
        'tiger.bg',
45
        'tiger.pagc_gaz',
46
        'tiger.pagc_lex',
47
        'tiger.pagc_rules',
48
    ];
49

50
    protected $truncateExceptTables = ['migrations', 'password_resets'];
51
    protected $prepareSequencesExceptTables = ['migrations', 'password_resets'];
52

53
    protected function loadTestDump(): void
54
    {
55
        $dump = $this->getFixture('dump.sql', false);
×
56

57
        if (empty($dump)) {
×
58
            return;
×
59
        }
60

61
        $databaseTables = $this->getTables();
×
62
        $scheme = config('database.default');
×
63

64
        $this->clearDatabase($scheme, $databaseTables, array_merge($this->postgisTables, $this->truncateExceptTables));
×
65

66
        DB::unprepared($dump);
×
67
    }
68

69
    public function getFixturePath(string $fixtureName): string
70
    {
71
        $class = get_class($this);
34✔
72
        $explodedClass = explode('\\', $class);
34✔
73
        $className = Arr::last($explodedClass);
34✔
74

75
        return base_path("tests/fixtures/{$className}/{$fixtureName}");
34✔
76
    }
77

78
    public function getFixture(string $fixtureName, $failIfNotExists = true): string
79
    {
80
        $path = $this->getFixturePath($fixtureName);
34✔
81

82
        if (file_exists($path)) {
34✔
83
            return file_get_contents($path);
32✔
84
        }
85

86
        if ($failIfNotExists) {
2✔
87
            $this->fail($path . ' fixture does not exist');
1✔
88
        }
89

90
        return '';
1✔
91
    }
92

93
    public function getJsonFixture(string $fixtureName, $assoc = true)
94
    {
95
        return json_decode($this->getFixture($fixtureName), $assoc);
32✔
96
    }
97

98
    public function assertEqualsFixture(string $fixture, $data, bool $exportMode = false): void
99
    {
100
        if ($exportMode || $this->globalExportMode) {
23✔
101
            $this->exportJson($fixture, $data);
×
102
        }
103

104
        $this->assertEquals($this->getJsonFixture($fixture), $data);
23✔
105
    }
106

107
    public function exportJson($fixture, $data): void
108
    {
109
        if ($data instanceof TestResponse) {
×
110
            $data = $data->json();
×
111
        }
112

113
        $this->exportContent(json_encode($data, JSON_PRETTY_PRINT | JSON_UNESCAPED_UNICODE), $fixture);
×
114
    }
115

116
    public function clearDatabase(string $scheme, array $tables, array $except): void
117
    {
118
        if ($scheme === 'pgsql') {
×
119
            $query = $this->getClearPsqlDatabaseQuery($tables, $except);
×
120
        } elseif ($scheme === 'mysql') {
×
121
            $query = $this->getClearMySQLDatabaseQuery($tables, $except);
×
122
        }
123

124
        if (!empty($query)) {
×
125
            app('db.connection')->unprepared($query);
×
126
        }
127
    }
128

129
    public function getClearPsqlDatabaseQuery(array $tables, array $except = ['migrations']): string
130
    {
131
        return array_concat($tables, function ($table) use ($except) {
1✔
132
            if (in_array($table, $except)) {
1✔
133
                return '';
1✔
134
            } else {
135
                return "TRUNCATE {$table} RESTART IDENTITY CASCADE; \n";
1✔
136
            }
137
        });
1✔
138
    }
139

140
    public function getClearMySQLDatabaseQuery(array $tables, array $except = ['migrations']): string
141
    {
142
        $query = "SET FOREIGN_KEY_CHECKS = 0;\n";
1✔
143

144
        $query .= array_concat($tables, function ($table) use ($except) {
1✔
145
            if (in_array($table, $except)) {
1✔
146
                return '';
1✔
147
            } else {
148
                return "TRUNCATE TABLE {$table}; \n";
1✔
149
            }
150
        });
1✔
151

152
        return  "{$query} SET FOREIGN_KEY_CHECKS = 1;\n";
1✔
153
    }
154

155
    public function prepareSequences(array $tables, array $except = []): void
156
    {
157
        $except = array_merge($this->postgisTables, $this->prepareSequencesExceptTables, $except);
×
158

159
        $query = array_concat($tables, function ($table) use ($except) {
×
160
            if (in_array($table, $except)) {
×
161
                return '';
×
162
            } else {
163
                return "SELECT setval('{$table}_id_seq', (select max(id) from {$table}));\n";
×
164
            }
165
        });
×
166

167
        app('db.connection')->unprepared($query);
×
168
    }
169

170
    public function exportFile(TestResponse $response, string $fixture): void
171
    {
172
        $this->exportContent(
×
173
            file_get_contents($response->getFile()->getPathName()),
×
174
            $fixture
×
175
        );
×
176
    }
177

178
    protected function getTables(): array
179
    {
180
        if (empty(self::$tables)) {
×
181
            self::$tables = app('db.connection')
×
182
                ->getDoctrineSchemaManager()
×
183
                ->listTableNames();
×
184
        }
185

186
        return self::$tables;
×
187
    }
188

189
    protected function exportContent($content, string $fixture): void
190
    {
191
        if (env('FAIL_EXPORT_JSON', true)) {
×
192
            $this->fail(preg_replace('/[ ]+/mu', ' ',
×
193
                'Looks like you forget to remove exportJson. If it is your local environment add 
×
194
                FAIL_EXPORT_JSON=false to .env.testing.
195
                If it is dev.testing environment then remove it.'
×
196
            ));
×
197
        }
198

199
        file_put_contents(
×
200
            $this->getFixturePath($fixture),
×
201
            $content
×
202
        );
×
203
    }
204
}
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