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

RonasIT / laravel-helpers / 18088881088

29 Sep 2025 07:11AM UTC coverage: 78.333% (+0.005%) from 78.328%
18088881088

push

github

web-flow
Merge pull request #217 from RonasIT/eparusov/add-test-token

feat: set  token for parallel tests

4 of 5 new or added lines in 2 files covered. (80.0%)

1128 of 1440 relevant lines covered (78.33%)

13.23 hits per line

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

22.0
/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

NEW
43
        $this->configureRedis();
×
44

45
        if (config('database.default') === 'pgsql') {
×
46
            $this->prepareSequences();
×
47
        }
48

49
        Carbon::setTestNow(Carbon::parse($this->testNow));
×
50

51
        Mail::fake();
×
52

53
        $this->beginDatabaseTransaction();
×
54
    }
55

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

62
        parent::tearDown();
×
63
    }
64

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

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

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

76
        self::$isWrappedIntoTransaction = false;
×
77
    }
78

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

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

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

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

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

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

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

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

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

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

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

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

136
        return $this;
2✔
137
    }
138

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

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

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

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

155
        if ($token) {
5✔
156
            config(['database.redis.default.database' => intval($token) % self::REDIS_COUNT_DATABASES]);
4✔
157
        }
158
    }
159
}
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