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

RonasIT / laravel-helpers / 15582941545

11 Jun 2025 10:53AM UTC coverage: 77.985% (+0.3%) from 77.654%
15582941545

Pull #206

github

web-flow
Merge faf7b01db into c09b8a113
Pull Request #206: 199 implement ability to test different version

5 of 5 new or added lines in 1 file covered. (100.0%)

27 existing lines in 1 file now uncovered.

1130 of 1449 relevant lines covered (77.98%)

12.61 hits per line

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

17.39
/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\Testing\TestResponse;
11
use RonasIT\Support\Contracts\VersionEnumContract;
12
use RonasIT\Support\Traits\MailsMockTrait;
13

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

18
    protected $auth;
19

20
    protected string $testNow = '2018-11-11 11:11:11';
21

22
    protected static string $startedTestSuite = '';
23
    protected static bool $isWrappedIntoTransaction = true;
24

25
    protected ?VersionEnumContract $apiVersion;
26

27
    protected function setUp(): void
28
    {
29
        parent::setUp();
×
30

31
        $this->artisan('cache:clear');
×
32

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

36
            $this->loadTestDump();
×
37

38
            static::$startedTestSuite = static::class;
×
39
        }
40

41
        if (config('database.default') === 'pgsql') {
×
42
            $this->prepareSequences();
×
43
        }
44

45
        Carbon::setTestNow(Carbon::parse($this->testNow));
×
46

47
        Mail::fake();
×
48

49
        $this->beginDatabaseTransaction();
×
50
    }
51

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

58
        parent::tearDown();
×
59
    }
60

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

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

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

72
        self::$isWrappedIntoTransaction = false;
×
73
    }
74

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

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

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

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

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

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

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

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

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

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

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

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

132
        return $this;
2✔
133
    }
134

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

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

142
    public function actingAs(Authenticatable $user, $guard = null): self
143
    {
144
        return parent::actingAs(clone $user, $guard);
1✔
145
    }
146
}
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