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

orchestral / testbench-core / 11968182539

22 Nov 2024 07:16AM UTC coverage: 91.991% (+0.7%) from 91.321%
11968182539

Pull #265

github

crynobone
wip

Signed-off-by: Mior Muhammad Zaki <crynobone@gmail.com>
Pull Request #265: [7.x] Allow to configure `tty` via `Orchestra\Testbench\remote()` function

3 of 4 new or added lines in 1 file covered. (75.0%)

93 existing lines in 25 files now uncovered.

1206 of 1311 relevant lines covered (91.99%)

61.99 hits per line

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

93.85
/src/Concerns/InteractsWithPublishedFiles.php
1
<?php
2

3
namespace Orchestra\Testbench\Concerns;
4

5
use Illuminate\Support\Collection;
6

7
/**
8
 * @internal
9
 */
10
trait InteractsWithPublishedFiles
11
{
12
    /**
13
     * Determine if trait teardown has been registered.
14
     *
15
     * @var bool
16
     */
17
    protected $interactsWithPublishedFilesTeardownRegistered = false;
18

19
    /**
20
     * List of existing migration files.
21
     *
22
     * @var array<int, string>|null
23
     */
24
    protected $cachedExistingMigrationsFiles;
25

26
    /**
27
     * Setup Interacts with Published Files environment.
28
     */
29
    protected function setUpInteractsWithPublishedFiles(): void
30
    {
31
        $this->cacheExistingMigrationsFiles();
14✔
32

33
        $this->cleanUpPublishedFiles();
14✔
34
        $this->cleanUpPublishedMigrationFiles();
14✔
35
    }
36

37
    /**
38
     * Teardown Interacts with Published Files environment.
39
     */
40
    protected function tearDownInteractsWithPublishedFiles(): void
41
    {
42
        if ($this->interactsWithPublishedFilesTeardownRegistered === false) {
14✔
43
            $this->cleanUpPublishedFiles();
14✔
44
            $this->cleanUpPublishedMigrationFiles();
14✔
45
        }
46

47
        $this->interactsWithPublishedFilesTeardownRegistered = true;
14✔
48
    }
49

50
    /**
51
     * Cache existing migration files.
52
     *
53
     * @internal
54
     *
55
     * @return void
56
     */
57
    protected function cacheExistingMigrationsFiles()
58
    {
59
        $this->cachedExistingMigrationsFiles ??= Collection::make(
14✔
60
            $this->app['files']->files($this->app->databasePath('migrations'))
14✔
61
        )->filter(static fn ($file) => str_ends_with($file, '.php'))
14✔
62
            ->all();
14✔
63
    }
64

65
    /**
66
     * Assert file does contains data.
67
     *
68
     * @param  array<int, string>  $contains
69
     */
70
    protected function assertFileContains(array $contains, string $file, string $message = ''): void
71
    {
72
        $this->assertFilenameExists($file);
1✔
73

74
        $haystack = $this->app['files']->get(
1✔
75
            $this->app->basePath($file)
1✔
76
        );
1✔
77

78
        foreach ($contains as $needle) {
1✔
79
            $this->assertStringContainsString($needle, $haystack, $message);
1✔
80
        }
81
    }
82

83
    /**
84
     * Assert file doesn't contains data.
85
     *
86
     * @param  array<int, string>  $contains
87
     */
88
    protected function assertFileDoesNotContains(array $contains, string $file, string $message = ''): void
89
    {
90
        $this->assertFilenameExists($file);
1✔
91

92
        $haystack = $this->app['files']->get(
1✔
93
            $this->app->basePath($file)
1✔
94
        );
1✔
95

96
        foreach ($contains as $needle) {
1✔
97
            $this->assertStringNotContainsString($needle, $haystack, $message);
1✔
98
        }
99
    }
100

101
    /**
102
     * Assert file doesn't contains data.
103
     *
104
     * @param  array<int, string>  $contains
105
     */
106
    protected function assertFileNotContains(array $contains, string $file, string $message = ''): void
107
    {
108
        $this->assertFileDoesNotContains($contains, $file, $message);
1✔
109
    }
110

111
    /**
112
     * Assert file does contains data.
113
     *
114
     * @param  array<int, string>  $contains
115
     */
116
    protected function assertMigrationFileContains(array $contains, string $file, string $message = '', ?string $directory = null): void
117
    {
118
        $migrationFile = $this->findFirstPublishedMigrationFile($file, $directory);
1✔
119

120
        $this->assertTrue(! \is_null($migrationFile), "Assert migration file {$file} does exist");
1✔
121

122
        $haystack = $this->app['files']->get($migrationFile);
1✔
123

124
        foreach ($contains as $needle) {
1✔
125
            $this->assertStringContainsString($needle, $haystack, $message);
1✔
126
        }
127
    }
128

129
    /**
130
     * Assert file doesn't contains data.
131
     *
132
     * @param  array<int, string>  $contains
133
     */
134
    protected function assertMigrationFileDoesNotContains(array $contains, string $file, string $message = '', ?string $directory = null): void
135
    {
136
        $migrationFile = $this->findFirstPublishedMigrationFile($file, $directory);
1✔
137

138
        $this->assertTrue(! \is_null($migrationFile), "Assert migration file {$file} does exist");
1✔
139

140
        $haystack = $this->app['files']->get($migrationFile);
1✔
141

142
        foreach ($contains as $needle) {
1✔
143
            $this->assertStringNotContainsString($needle, $haystack, $message);
1✔
144
        }
145
    }
146

147
    /**
148
     * Assert file doesn't contains data.
149
     *
150
     * @param  array<int, string>  $contains
151
     */
152
    protected function assertMigrationFileNotContains(array $contains, string $file, string $message = '', ?string $directory = null): void
153
    {
UNCOV
154
        $this->assertMigrationFileDoesNotContains($contains, $file, $message, $directory);
×
155
    }
156

157
    /**
158
     * Assert filename exists.
159
     */
160
    protected function assertFilenameExists(string $file): void
161
    {
162
        $appFile = $this->app->basePath($file);
2✔
163

164
        $this->assertTrue($this->app['files']->exists($appFile), "Assert file {$file} does exist");
2✔
165
    }
166

167
    /**
168
     * Assert filename not exists.
169
     */
170
    protected function assertFilenameDoesNotExists(string $file): void
171
    {
172
        $appFile = $this->app->basePath($file);
1✔
173

174
        $this->assertTrue(! $this->app['files']->exists($appFile), "Assert file {$file} doesn't exist");
1✔
175
    }
176

177
    /**
178
     * Assert filename not exists.
179
     */
180
    protected function assertFilenameNotExists(string $file): void
181
    {
182
        $this->assertFilenameDoesNotExists($file);
1✔
183
    }
184

185
    /**
186
     * Assert migration filename exists.
187
     */
188
    protected function assertMigrationFileExists(string $file, ?string $directory = null): void
189
    {
190
        $migrationFile = $this->findFirstPublishedMigrationFile($file, $directory);
1✔
191

192
        $this->assertTrue(! \is_null($migrationFile), "Assert migration file {$file} does exist");
1✔
193
    }
194

195
    /**
196
     * Assert migration filename not exists.
197
     */
198
    protected function assertMigrationFileDoesNotExists(string $file, ?string $directory = null): void
199
    {
200
        $migrationFile = $this->findFirstPublishedMigrationFile($file, $directory);
1✔
201

202
        $this->assertTrue(\is_null($migrationFile), "Assert migration file {$file} doesn't exist");
1✔
203
    }
204

205
    /**
206
     * Assert migration filename not exists.
207
     */
208
    protected function assertMigrationFileNotExists(string $file, ?string $directory = null): void
209
    {
UNCOV
210
        $this->assertMigrationFileNotExists($file, $directory);
×
211
    }
212

213
    /**
214
     * Removes generated files.
215
     */
216
    protected function cleanUpPublishedFiles(): void
217
    {
218
        $this->app['files']->delete(
14✔
219
            Collection::make($this->files ?? [])
14✔
220
                ->transform(fn ($file) => $this->app->basePath($file))
14✔
221
                ->map(fn ($file) => str_contains($file, '*') ? [...$this->app['files']->glob($file)] : $file)
14✔
222
                ->flatten()
14✔
223
                ->filter(fn ($file) => $this->app['files']->exists($file))
14✔
224
                ->reject(static function ($file) {
14✔
UNCOV
225
                    return str_ends_with($file, '.gitkeep') || str_ends_with($file, '.gitignore');
×
226
                })->all()
14✔
227
        );
14✔
228
    }
229

230
    /**
231
     * Removes generated migration files.
232
     */
233
    protected function findFirstPublishedMigrationFile(string $filename, ?string $directory = null): ?string
234
    {
235
        $migrationPath = ! \is_null($directory)
1✔
236
            ? $this->app->basePath($directory)
1✔
UNCOV
237
            : $this->app->databasePath('migrations');
×
238

239
        return $this->app['files']->glob("{$migrationPath}/*{$filename}")[0] ?? null;
1✔
240
    }
241

242
    /**
243
     * Removes generated migration files.
244
     */
245
    protected function cleanUpPublishedMigrationFiles(): void
246
    {
247
        $this->app['files']->delete(
14✔
248
            Collection::make($this->app['files']->files($this->app->databasePath('migrations')))
14✔
249
                ->reject(fn ($file) => \in_array($file, $this->cachedExistingMigrationsFiles))
14✔
250
                ->filter(static fn ($file) => str_ends_with($file, '.php'))
14✔
251
                ->all()
14✔
252
        );
14✔
253
    }
254
}
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