Passed
Push — next ( b7ca98...230762 )
by Bas
12:22
created

InteractsWithDatabase::assertDatabaseMissing()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 9
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 5
CRAP Score 1

Importance

Changes 1
Bugs 0 Features 0
Metric Value
eloc 4
c 1
b 0
f 0
dl 0
loc 9
ccs 5
cts 5
cp 1
rs 10
cc 1
nc 1
nop 3
crap 1
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Testing\Concerns;
6
7
use Illuminate\Support\Arr;
8
use Illuminate\Support\Facades\DB;
9
use Illuminate\Testing\Constraints\HasInDatabase;
10
use Illuminate\Testing\Constraints\NotSoftDeletedInDatabase;
11
use Illuminate\Testing\Constraints\SoftDeletedInDatabase;
12
use LaravelFreelancerNL\Aranguent\Testing\TestCase;
13
use PHPUnit\Framework\Constraint\LogicalNot as ReverseConstraint;
14
15
trait InteractsWithDatabase
16
{
17
    /**
18
     * Assert that a given where condition exists in the database.
19
     *
20
     * @param  \Illuminate\Database\Eloquent\Model|string  $table
21
     * @param  array  $data
22
     * @param  string|null  $connection
23
     * @return self|TestCase
24
     */
25 2
    protected function assertDatabaseHas($table, array $data, $connection = null)
26
    {
27 2
        $this->assertThat(
0 ignored issues
show
Bug introduced by
It seems like assertThat() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

27
        $this->/** @scrutinizer ignore-call */ 
28
               assertThat(
Loading history...
28 2
            $this->getTable($table),
0 ignored issues
show
Bug introduced by
It seems like getTable() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

28
            $this->/** @scrutinizer ignore-call */ 
29
                   getTable($table),
Loading history...
29 2
            new HasInDatabase($this->getConnection($connection), Arr::dot($data))
0 ignored issues
show
Bug introduced by
It seems like getConnection() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

29
            new HasInDatabase($this->/** @scrutinizer ignore-call */ getConnection($connection), Arr::dot($data))
Loading history...
30
        );
31
32 2
        return $this;
33
    }
34
35
    /**
36
     * Assert that a given where condition does not exist in the database.
37
     *
38
     * @param  \Illuminate\Database\Eloquent\Model|string  $table
39
     * @param  array  $data
40
     * @param  string|null  $connection
41
     * @return self|TestCase
42
     */
43 4
    protected function assertDatabaseMissing($table, array $data, $connection = null)
44
    {
45 4
        $constraint = new ReverseConstraint(
46 4
            new HasInDatabase($this->getConnection($connection), Arr::dot($data))
47
        );
48
49 4
        $this->assertThat($this->getTable($table), $constraint);
50
51 4
        return $this;
52
    }
53
54
    /**
55
     * Assert the given record has been "soft deleted".
56
     *
57
     * @param  \Illuminate\Database\Eloquent\Model|string  $table
58
     * @param  array  $data
59
     * @param  string|null  $connection
60
     * @param  string|null  $deletedAtColumn
61
     * @return \Illuminate\Foundation\Testing\Concerns\InteractsWithDatabase
62
     */
63 2
    protected function assertSoftDeleted($table, array $data = [], $connection = null, $deletedAtColumn = 'deleted_at')
64
    {
65 2
        if ($this->isSoftDeletableModel($table)) {
0 ignored issues
show
Bug introduced by
It seems like isSoftDeletableModel() must be provided by classes using this trait. How about adding it as abstract method to this trait? ( Ignorable by Annotation )

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

65
        if ($this->/** @scrutinizer ignore-call */ isSoftDeletableModel($table)) {
Loading history...
66 1
            return $this->assertSoftDeleted(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->assertSoft...->getDeletedAtColumn()) returns the type LaravelFreelancerNL\Aran...s\InteractsWithDatabase which is incompatible with the documented return type Illuminate\Foundation\Te...s\InteractsWithDatabase.
Loading history...
67 1
                $table->getTable(),
68 1
                [$table->getKeyName() => $table->getKey()],
69 1
                $table->getConnectionName(),
70 1
                $table->getDeletedAtColumn()
0 ignored issues
show
Bug introduced by
It seems like $table->getDeletedAtColumn() can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $deletedAtColumn of LaravelFreelancerNL\Aran...se::assertSoftDeleted() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

70
                /** @scrutinizer ignore-type */ $table->getDeletedAtColumn()
Loading history...
71
            );
72
        }
73
74 2
        $this->assertThat(
75 2
            $this->getTable($table),
76 2
            new SoftDeletedInDatabase(
77 2
                $this->getConnection($connection),
78 2
                Arr::dot($data),
79
                $deletedAtColumn
0 ignored issues
show
Bug introduced by
It seems like $deletedAtColumn can also be of type null; however, parameter $deletedAtColumn of Illuminate\Testing\Const...Database::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

79
                /** @scrutinizer ignore-type */ $deletedAtColumn
Loading history...
80
            )
81
        );
82
83 2
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type LaravelFreelancerNL\Aran...s\InteractsWithDatabase which is incompatible with the documented return type Illuminate\Foundation\Te...s\InteractsWithDatabase.
Loading history...
84
    }
85
86
    /**
87
     * Assert the given record has not been "soft deleted".
88
     *
89
     * @param  \Illuminate\Database\Eloquent\Model|string  $table
90
     * @param  array  $data
91
     * @param  string|null  $connection
92
     * @param  string|null  $deletedAtColumn
93
     * @return \Illuminate\Foundation\Testing\Concerns\InteractsWithDatabase
94
     */
95 2
    protected function assertNotSoftDeleted(
96
        $table,
97
        array $data = [],
98
        $connection = null,
99
        $deletedAtColumn = 'deleted_at'
100
    ) {
101 2
        if ($this->isSoftDeletableModel($table)) {
102 1
            return $this->assertNotSoftDeleted(
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this->assertNotS...->getDeletedAtColumn()) returns the type LaravelFreelancerNL\Aran...s\InteractsWithDatabase which is incompatible with the documented return type Illuminate\Foundation\Te...s\InteractsWithDatabase.
Loading history...
103 1
                $table->getTable(),
104 1
                [$table->getKeyName() => $table->getKey()],
105 1
                $table->getConnectionName(),
106 1
                $table->getDeletedAtColumn()
0 ignored issues
show
Bug introduced by
It seems like $table->getDeletedAtColumn() can also be of type Illuminate\Database\Eloquent\Builder; however, parameter $deletedAtColumn of LaravelFreelancerNL\Aran...:assertNotSoftDeleted() does only seem to accept null|string, maybe add an additional type check? ( Ignorable by Annotation )

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

106
                /** @scrutinizer ignore-type */ $table->getDeletedAtColumn()
Loading history...
107
            );
108
        }
109
110 2
        $this->assertThat(
111 2
            $this->getTable($table),
112 2
            new NotSoftDeletedInDatabase(
113 2
                $this->getConnection($connection),
114 2
                Arr::dot($data),
115
                $deletedAtColumn
0 ignored issues
show
Bug introduced by
It seems like $deletedAtColumn can also be of type null; however, parameter $deletedAtColumn of Illuminate\Testing\Const...Database::__construct() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

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

115
                /** @scrutinizer ignore-type */ $deletedAtColumn
Loading history...
116
            )
117
        );
118
119 2
        return $this;
0 ignored issues
show
Bug Best Practice introduced by
The expression return $this returns the type LaravelFreelancerNL\Aran...s\InteractsWithDatabase which is incompatible with the documented return type Illuminate\Foundation\Te...s\InteractsWithDatabase.
Loading history...
120
    }
121
}
122