Passed
Push — next ( bea07e...b4e6e6 )
by Bas
05:12 queued 01:45
created

DatabaseTruncation   A

Complexity

Total Complexity 7

Size/Duplication

Total Lines 77
Duplicated Lines 0 %

Test Coverage

Coverage 95.24%

Importance

Changes 3
Bugs 2 Features 0
Metric Value
eloc 33
dl 0
loc 77
ccs 40
cts 42
cp 0.9524
rs 10
c 3
b 2
f 0
wmc 7

3 Methods

Rating   Name   Duplication   Size   Complexity  
A truncateTablesForConnection() 0 29 3
A migrateFreshUsing() 0 17 2
A tableExistsIn() 0 5 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Testing;
6
7
use Illuminate\Database\ConnectionInterface;
8
use Illuminate\Foundation\Testing\DatabaseTruncation as IlluminateDatabaseTruncation;
9
use Illuminate\Support\Collection;
10
use LaravelFreelancerNL\Aranguent\Testing\Concerns\CanConfigureMigrationCommands;
11
12
trait DatabaseTruncation
13
{
14
    use IlluminateDatabaseTruncation;
15
    use CanConfigureMigrationCommands;
16
17
    /**
18
     * The parameters that should be used when running "migrate:fresh".
19
     *
20
     * Duplicate code because CanConfigureMigrationCommands has a conflict otherwise.
21
     *
22
     * @return array
23
     */
24 1
    protected function migrateFreshUsing()
25
    {
26 1
        $seeder = $this->seeder();
27
28 1
        $results =  array_merge(
29 1
            [
30 1
                '--drop-analyzers' => $this->shouldDropAnalyzers(),
31 1
                '--drop-graphs' => $this->shouldDropGraphs(),
32 1
                '--drop-views' => $this->shouldDropViews(),
33 1
                '--drop-types' => $this->shouldDropTypes(),
34 1
                '--drop-all' => $this->shouldDropAll(),
35 1
            ],
36 1
            $seeder ? ['--seeder' => $seeder] : ['--seed' => $this->shouldSeed()],
37 1
            $this->setMigrationPaths(),
38 1
        );
39
40 1
        return $results;
41
    }
42
43
    /**
44
     * Determine if a table exists in the given list, with or without its schema.
45
     */
46 1
    protected function tableExistsIn(array $table, array $tables): bool
47
    {
48 1
        return isset($table['schema'])
49
            ? ! empty(array_intersect([$table['name'], $table['schema'] . '.' . $table['name']], $tables))
50 1
            : in_array($table['name'], $tables);
51
    }
52
53
    /**
54
     * Truncate the database tables for the given database connection.
55
     *
56
     * @param  \Illuminate\Database\ConnectionInterface  $connection
57
     * @param  string|null  $name
58
     * @return void
59
     */
60 1
    protected function truncateTablesForConnection(ConnectionInterface $connection, ?string $name): void
61
    {
62 1
        $dispatcher = $connection->getEventDispatcher();
63
64 1
        $connection->unsetEventDispatcher();
65
66 1
        (new Collection($this->getAllTablesForConnection($connection, $name)))
0 ignored issues
show
Bug introduced by
$this->getAllTablesForCo...ion($connection, $name) of type array is incompatible with the type Illuminate\Contracts\Support\Arrayable expected by parameter $items of Illuminate\Support\Collection::__construct(). ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

66
        (new Collection(/** @scrutinizer ignore-type */ $this->getAllTablesForConnection($connection, $name)))
Loading history...
67 1
            ->when(
68 1
                $this->tablesToTruncate($connection, $name),
69 1
                function (Collection $tables, array $tablesToTruncate) {
70
                    return $tables->filter(fn(array $table) => $this->tableExistsIn($table, $tablesToTruncate));
71 1
                },
72 1
                function (Collection $tables) use ($connection, $name) {
73 1
                    $exceptTables = $this->exceptTables($connection, $name);
74 1
                    return $tables->filter(fn(array $table) => ! $this->tableExistsIn($table, $exceptTables));
75 1
                },
76 1
            )
77 1
            ->each(function (array $table) use ($connection) {
78 1
                $connection->withoutTablePrefix(function ($connection) use ($table) {
79 1
                    $table = $connection->table(
80 1
                        isset($table['schema']) ? $table['schema'] . '.' . $table['name'] : $table['name'],
81 1
                    );
82 1
                    if ($table->exists()) {
83 1
                        $table->truncate();
84
                    }
85 1
                });
86 1
            });
87
88 1
        $connection->setEventDispatcher($dispatcher);
89
    }
90
91
}
92