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( |
|
|
|
|
26
|
|
|
$this->getTable($table), |
|
|
|
|
27
|
2 |
|
new HasInDatabase($this->getConnection($connection), associativeFlatten($data)) |
|
|
|
|
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)) { |
|
|
|
|
64
|
|
|
return $this->assertSoftDeleted( |
|
|
|
|
65
|
2 |
|
$table->getTable(), |
66
|
1 |
|
[$table->getKeyName() => $table->getKey()], |
67
|
1 |
|
$table->getConnectionName(), |
68
|
1 |
|
$table->getDeletedAtColumn() |
|
|
|
|
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 |
|
|
|
|
78
|
2 |
|
) |
79
|
|
|
); |
80
|
|
|
|
81
|
|
|
return $this; |
|
|
|
|
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( |
|
|
|
|
101
|
2 |
|
$table->getTable(), |
102
|
1 |
|
[$table->getKeyName() => $table->getKey()], |
103
|
1 |
|
$table->getConnectionName(), |
104
|
1 |
|
$table->getDeletedAtColumn() |
|
|
|
|
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 |
|
|
|
|
114
|
2 |
|
) |
115
|
|
|
); |
116
|
|
|
|
117
|
|
|
return $this; |
|
|
|
|
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
|
|
|
|