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( |
|
|
|
|
28
|
2 |
|
$this->getTable($table), |
|
|
|
|
29
|
2 |
|
new HasInDatabase($this->getConnection($connection), Arr::dot($data)) |
|
|
|
|
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)) { |
|
|
|
|
66
|
1 |
|
return $this->assertSoftDeleted( |
|
|
|
|
67
|
1 |
|
$table->getTable(), |
68
|
1 |
|
[$table->getKeyName() => $table->getKey()], |
69
|
1 |
|
$table->getConnectionName(), |
70
|
1 |
|
$table->getDeletedAtColumn() |
|
|
|
|
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 |
|
|
|
|
80
|
|
|
) |
81
|
|
|
); |
82
|
|
|
|
83
|
2 |
|
return $this; |
|
|
|
|
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( |
|
|
|
|
103
|
1 |
|
$table->getTable(), |
104
|
1 |
|
[$table->getKeyName() => $table->getKey()], |
105
|
1 |
|
$table->getConnectionName(), |
106
|
1 |
|
$table->getDeletedAtColumn() |
|
|
|
|
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 |
|
|
|
|
116
|
|
|
) |
117
|
|
|
); |
118
|
|
|
|
119
|
2 |
|
return $this; |
|
|
|
|
120
|
|
|
} |
121
|
|
|
} |
122
|
|
|
|