Passed
Push — next ( 230762...ff9bff )
by Bas
08:27
created

InteractsWithDatabase::castAsJson()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
eloc 1
c 0
b 0
f 0
dl 0
loc 3
ccs 0
cts 0
cp 0
rs 10
cc 1
nc 1
nop 1
crap 2
1
<?php
2
3
declare(strict_types=1);
4
5
namespace LaravelFreelancerNL\Aranguent\Testing\Concerns;
6
7
use Illuminate\Testing\Constraints\HasInDatabase;
8
use Illuminate\Testing\Constraints\NotSoftDeletedInDatabase;
9
use Illuminate\Testing\Constraints\SoftDeletedInDatabase;
10
use LaravelFreelancerNL\Aranguent\Testing\TestCase;
11
use PHPUnit\Framework\Constraint\LogicalNot as ReverseConstraint;
12
13
trait InteractsWithDatabase
14
{
15
    /**
16
     * Assert that a given where condition exists in the database.
17
     *
18
     * @param  \Illuminate\Database\Eloquent\Model|string  $table
19
     * @param  array  $data
20
     * @param  string|null  $connection
21
     * @return self|TestCase
22
     */
23
    protected function assertDatabaseHas($table, array $data, $connection = null)
24
    {
25 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

25
        $this->/** @scrutinizer ignore-call */ 
26
               assertThat(
Loading history...
26
            $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

26
            $this->/** @scrutinizer ignore-call */ 
27
                   getTable($table),
Loading history...
27 2
            new HasInDatabase($this->getConnection($connection), associativeFlatten($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

27
            new HasInDatabase($this->/** @scrutinizer ignore-call */ getConnection($connection), associativeFlatten($data))
Loading history...
28 2
        );
29 2
30
        return $this;
31
    }
32 2
33
    /**
34
     * Assert that a given where condition does not exist in the database.
35
     *
36
     * @param  \Illuminate\Database\Eloquent\Model|string  $table
37
     * @param  array  $data
38
     * @param  string|null  $connection
39
     * @return self|TestCase
40
     */
41
    protected function assertDatabaseMissing($table, array $data, $connection = null)
42
    {
43 4
        $constraint = new ReverseConstraint(
44
            new HasInDatabase($this->getConnection($connection), associativeFlatten($data))
45 4
        );
46 4
47
        $this->assertThat($this->getTable($table), $constraint);
48
49 4
        return $this;
50
    }
51 4
52
    /**
53
     * Assert the given record has been "soft deleted".
54
     *
55
     * @param  \Illuminate\Database\Eloquent\Model|string  $table
56
     * @param  array  $data
57
     * @param  string|null  $connection
58
     * @param  string|null  $deletedAtColumn
59
     * @return \Illuminate\Foundation\Testing\Concerns\InteractsWithDatabase
60
     */
61
    protected function assertSoftDeleted($table, array $data = [], $connection = null, $deletedAtColumn = 'deleted_at')
62
    {
63 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

63
        if ($this->/** @scrutinizer ignore-call */ isSoftDeletableModel($table)) {
Loading history...
64
            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...
65 2
                $table->getTable(),
66 1
                [$table->getKeyName() => $table->getKey()],
67 1
                $table->getConnectionName(),
68 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

68
                /** @scrutinizer ignore-type */ $table->getDeletedAtColumn()
Loading history...
69 1
            );
70 1
        }
71
72
        $this->assertThat(
73
            $this->getTable($table),
74 2
            new SoftDeletedInDatabase(
75 2
                $this->getConnection($connection),
76 2
                associativeFlatten($data),
77 2
                $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

77
                /** @scrutinizer ignore-type */ $deletedAtColumn
Loading history...
78 2
            )
79
        );
80
81
        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...
82
    }
83 2
84
    /**
85
     * Assert the given record has not been "soft deleted".
86
     *
87
     * @param  \Illuminate\Database\Eloquent\Model|string  $table
88
     * @param  array  $data
89
     * @param  string|null  $connection
90
     * @param  string|null  $deletedAtColumn
91
     * @return \Illuminate\Foundation\Testing\Concerns\InteractsWithDatabase
92
     */
93
    protected function assertNotSoftDeleted(
94
        $table,
95 2
        array $data = [],
96
        $connection = null,
97
        $deletedAtColumn = 'deleted_at'
98
    ) {
99
        if ($this->isSoftDeletableModel($table)) {
100
            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...
101 2
                $table->getTable(),
102 1
                [$table->getKeyName() => $table->getKey()],
103 1
                $table->getConnectionName(),
104 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

104
                /** @scrutinizer ignore-type */ $table->getDeletedAtColumn()
Loading history...
105 1
            );
106 1
        }
107
108
        $this->assertThat(
109
            $this->getTable($table),
110 2
            new NotSoftDeletedInDatabase(
111 2
                $this->getConnection($connection),
112 2
                associativeFlatten($data),
113 2
                $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

113
                /** @scrutinizer ignore-type */ $deletedAtColumn
Loading history...
114 2
            )
115
        );
116
117
        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...
118
    }
119 2
120
    /**
121
     * Cast a JSON string to a database compatible type.
122
     * Supported for backwards compatibility in existing projects.
123
     * No cast is necessary as json is a first class citizen in ArangoDB.
124
     *
125
     * @param  array|string  $value
126
     * @return array|string
127
     */
128
    public function castAsJson($value)
129
    {
130
        return $value;
131
    }
132
}
133