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

RonasIT / laravel-helpers / 24707098614

21 Apr 2026 06:12AM UTC coverage: 80.436% (+0.1%) from 80.288%
24707098614

push

github

web-flow
Merge pull request #249 from RonasIT/248-auto-increment

[248]: add mysql auto increment reset in TestCase

13 of 15 new or added lines in 2 files covered. (86.67%)

1180 of 1467 relevant lines covered (80.44%)

15.5 hits per line

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

27.78
/src/Testing/TestCase.php
1
<?php
2

3
namespace RonasIT\Support\Testing;
4

5
use Carbon\Carbon;
6
use Illuminate\Contracts\Auth\Authenticatable;
7
use Illuminate\Foundation\Testing\TestCase as BaseTest;
8
use Illuminate\Support\Facades\DB;
9
use Illuminate\Support\Facades\Mail;
10
use Illuminate\Support\Facades\ParallelTesting;
11
use Illuminate\Testing\TestResponse;
12
use RonasIT\Support\Contracts\VersionEnumContract;
13
use RonasIT\Support\Traits\MailsMockTrait;
14

15
abstract class TestCase extends BaseTest
16
{
17
    use MailsMockTrait;
18

19
    protected const int REDIS_COUNT_DATABASES = 16;
20
    protected $auth;
21

22
    protected string $testNow = '2018-11-11 11:11:11';
23

24
    protected static string $startedTestSuite = '';
25
    protected static bool $isWrappedIntoTransaction = true;
26

27
    protected ?VersionEnumContract $apiVersion;
28

29
    protected function setUp(): void
30
    {
31
        parent::setUp();
×
32

33
        $this->artisan('cache:clear');
×
34

35
        if ((static::$startedTestSuite !== static::class) || !self::$isWrappedIntoTransaction) {
×
36
            $this->artisan('migrate');
×
37

38
            $this->loadTestDump();
×
39

40
            static::$startedTestSuite = static::class;
×
41
        }
42

43
        $this->configureRedis();
×
44

NEW
45
        $this->prepareDB();
×
46

47
        Carbon::setTestNow(Carbon::parse($this->testNow));
×
48

49
        Mail::fake();
×
50

51
        $this->beginDatabaseTransaction();
×
52
    }
53

54
    public function tearDown(): void
55
    {
56
        $this->beforeApplicationDestroyed(function () {
×
57
            DB::disconnect();
×
58
        });
×
59

60
        parent::tearDown();
×
61
    }
62

63
    public function callRawRequest(string $method, string $uri, $content, array $headers = []): TestResponse
64
    {
65
        $server = $this->transformHeadersToServerVars($headers);
×
66

67
        return $this->call($method, $uri, [], [], [], $server, $content);
×
68
    }
69

70
    protected function dontWrapIntoTransaction(): void
71
    {
72
        $this->rollbackTransaction();
×
73

74
        self::$isWrappedIntoTransaction = false;
×
75
    }
76

77
    protected function beginDatabaseTransaction(): void
78
    {
79
        $database = $this->app->make('db');
×
80

81
        foreach ($this->connectionsToTransact() as $name) {
×
82
            $connection = $database->connection($name);
×
83
            $dispatcher = $connection->getEventDispatcher();
×
84

85
            $connection->unsetEventDispatcher();
×
86
            $connection->beginTransaction();
×
87
            $connection->setEventDispatcher($dispatcher);
×
88
        }
89

90
        $this->beforeApplicationDestroyed(function () {
×
91
            $this->rollbackTransaction();
×
92
        });
×
93
    }
94

95
    protected function connectionsToTransact(): array
96
    {
97
        return property_exists($this, 'connectionsToTransact') ? $this->connectionsToTransact : [null];
×
98
    }
99

100
    protected function rollbackTransaction(): void
101
    {
102
        $database = $this->app->make('db');
×
103

104
        foreach ($this->connectionsToTransact() as $name) {
×
105
            $connection = $database->connection($name);
×
106
            $dispatcher = $connection->getEventDispatcher();
×
107

108
            $connection->unsetEventDispatcher();
×
109
            $connection->rollback();
×
110
            $connection->setEventDispatcher($dispatcher);
×
111
            $connection->disconnect();
×
112
        }
113
    }
114

115
    protected function prepareModelTestState(string $modelClassName): ModelTestState
116
    {
117
        return (new ModelTestState($modelClassName))->setGlobalExportMode($this->globalExportMode);
2✔
118
    }
119

120
    protected function prepareTableTestState(string $tableName, array $jsonFields = [], ?string $connectionName = null): TableTestState
121
    {
122
        return (new TableTestState($tableName, $jsonFields, $connectionName))->setGlobalExportMode($this->globalExportMode);
2✔
123
    }
124

125
    public function withoutAPIVersion(): TestCase
126
    {
127
        return $this->setAPIVersion(null);
1✔
128
    }
129

130
    public function setAPIVersion(?VersionEnumContract $apiVersion): TestCase
131
    {
132
        $this->apiVersion = $apiVersion;
2✔
133

134
        return $this;
2✔
135
    }
136

137
    public function json($method, $uri, array $data = [], array $headers = [], $options = 0): TestResponse
138
    {
139
        $version = (empty($this->apiVersion)) ? '' : "/v{$this->apiVersion->value}";
2✔
140

141
        return parent::json($method, "{$version}{$uri}", $data, $headers);
2✔
142
    }
143

144
    public function actingAs(Authenticatable $user, $guard = null): self
145
    {
146
        return parent::actingAs(clone $user, $guard);
1✔
147
    }
148

149
    protected function configureRedis(): void
150
    {
151
        $token = ParallelTesting::token();
5✔
152

153
        if ($token) {
5✔
154
            config(['database.redis.default.database' => intval($token) % self::REDIS_COUNT_DATABASES]);
4✔
155
        }
156
    }
157

158
    protected function prepareDB(): void
159
    {
160
        match (config('database.default')) {
2✔
161
            'pgsql' => $this->prepareSequences(),
1✔
162
            'mysql' => $this->resetMySQLAutoIncrement($this->getTables()),
1✔
NEW
163
            default => null,
×
164
        };
2✔
165
    }
166
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE TRIAL · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc