8000 [Refactor] Remove dependency on database test helper · josh-taylor/laravel-json-api@8742d10 · GitHub
[go: up one dir, main page]

Skip to content < 8000 /span>

Commit 8742d10

Browse files
committed
[Refactor] Remove dependency on database test helper
Removed the dependency between this package's model test helper (`InteractsWithModels`) and Laravel's database test helper (`InteractsWithDatabase`). This dependency was unnecessary because the test helper can work out how to query the database from the model instance that is passed to it.
1 parent 34e5f23 commit 8742d10

File tree

2 files changed

+79
-25
lines changed

2 files changed

+79
-25
lines changed

CHANGELOG.md

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,12 @@ All notable changes to this project will be documented in this file. This projec
44

55
## [Unreleased]
66

7-
Fixed
7+
### Changed
8+
9+
- Removed dependency between model test helper and Laravel's database test helper, as there was no need for
10+
this dependency to exist.
11+
12+
### Fixed
813

914
- Asserting that a model has been created now correctly checks expected attributes.
1015

src/Testing/InteractsWithModels.php

Lines changed: 73 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@
2424
/**
2525
* Class InteractsWithModels
2626
* @package CloudCreativity\LaravelJsonApi\Testing
27-
*
28-
* This trait MUST be used on a class that also uses this trait:
29-
* Illuminate\Foundation\Testing\Concerns\InteractsWithDatabase
3027
*/
3128
trait InteractsWithModels
3229
{
@@ -36,37 +33,37 @@ trait InteractsWithModels
3633
*
3734
* @param Model $model
3835
* a representation of the model that should have been created.
39-
* @param $expectedId
40-
* the expected id of the model.
36+
* @param $expectedResourceId
37+
* the expected resource id of the model.
4138
* @param string|string[]|null $attributeKeys
4239
* the keys of the model attributes that should be checked, or null to check all.
4340
* @param string|null $keyName
44-
* the key name to use for the id - defaults to `Model::getKeyName()`
41+
* the key name to use for the resource id - defaults to `Model::getKeyName()`
42+
* @return $this
4543
*/
46-
public function assertModelCreated(
44+
protected function assertModelCreated(
4745
Model $model,
48-
$expectedId,
46+
$expectedResourceId,
4947
$attributeKeys = null,
5048
$keyName = null
5149
) {
52-
if (!$keyName) {
53-
$keyName = $model->getKeyName();
54-
}
55-
50+
$keyName = $keyName ?: $model->getKeyName();
5651
$attributes = $model->getAttributes();
52+
$expected = [$keyName => $expectedResourceId];
5753

5854
if (is_null($attributeKeys)) {
5955
$attributeKeys = array_keys($attributes);
6056
}
6157

62-
$expected = [];
63-
6458
foreach ((array) $attributeKeys as $attr) {
59+
if ($keyName === $attr) {
60+
continue;
61+
}
62+
6563
$expected[$attr] = isset($attributes[$attr]) ? $attributes[$attr] : null;
6664
}
6765

68-
$expected[$keyName] = $expectedId;
69-
$this->seeInDatabase($model->getTable(), $expected, $model->getConnectionName());
66+
return $this->seeModelInDatabase($model, $expected);
7067
}
7168

7269
/**
@@ -78,8 +75,9 @@ public function assertModelCreated(
7875
* the expected changed attributes - key to value pairs.
7976
* @param string|string[] $unchangedKeys
8077
* the keys of the attributes that should not have changed.
78+
* @return $this
8179
*/
82-
public function assertModelPatched(Model $model, array $changedAttributes, $unchangedKeys = [])
80+
protected function assertModelPatched(Model $model, array $changedAttributes, $unchangedKeys = [])
8381
{
8482
/** We need to ensure values are cast to database values */
8583
$expected = $model->newInstance($changedAttributes)->getAttributes();
@@ -90,29 +88,80 @@ public function assertModelPatched(Model $model, array $changedAttributes, $unch
9088
}
9189

9290
$expected[$model->getKeyName()] = $model->getKey();
93-
$this->seeInDatabase($model->getTable(), $expected, $model->getConnectionName());
91+
92+
return $this->seeModelInDatabase($model, $expected);
9493
}
9594

9695
/**
9796
* Assert that a model was deleted.
9897
*
9998
* @param Model $model
99+
* @return $this
100100
*/
101-
public function assertModelDeleted(Model $model)
101+
protected function assertModelDeleted(Model $model)
102102
{
103-
$this->notSeeInDatabase($model->getTable(), [
104-
$model->getKeyName() => $model->getKey()
105-
], $model->getConnectionName());
103+
return $this->notSeeModelInDatabase($model, [$model->getKeyName() => $model->getKey()]);
106104
}
107105

108106
/**
109107
* Assert that a model was soft deleted.
110108
*
111109
* @param Model $model
110+
* @return $this
112111
*/
113-
public function assertModelTrashed(Model $model)
112+
protected function assertModelTrashed(Model $model)
114113
{
115114
PHPUnit::assertNull($model->fresh(), 'Model is not trashed.');
116-
$this->seeInDatabase($model->getTable(), [$model->getKeyName() => $model->getKey()], $model->getConnectionName());
115+
return $this->seeModelInDatabase($model, [$model->getKeyName() => $model->getKey()]);
116+
}
117+
118+
/**
119+
* @param Model $model
120+
* @param array $expected
121+
* @return $this
122+
*/
123+
protected function seeModelInDatabase(Model $model, array $expected)
124+
{
125+
$message = sprintf(
126+
'Unable to find model in database table [%s] that matched attributes [%s].',
127+
$model->getTable(),
128+
json_encode($expected)
129+
);
130+
131+
PHPUnit::assertGreaterThan(0, $this->countModels($model, $expected), $message);
132+
133+
return $this;
134+
}
135+
136+
/**
137+
* @param Model $model
138+
* @param array $expected
139+
* @return $this
140+
*/
141+
protected function notSeeModelInDatabase(Model $model, array $expected)
142+
{
143+
$message = sprintf(
144+
'Found model in database table [%s] that matched attributes [%s].',
145+
$model->getTable(),
146+
json_encode($expected)
147+
);
148+
149+
PHPUnit::assertEquals(0, $this->countModels($model, $expected), $message);
150+
151+
return $this;
152+
}
153+
154+
/**
155+
* @param Model $model
156+
* @param array $expected
157+
* @return int
158+
*/
159+
private function countModels(Model $model, array $expected)
160+
{
161+
return $model
162+
->getConnection()
163+
->table($model->getTable())
164+
->where($expected)
165+
->count();
117166
}
118167
}

0 commit comments

Comments
 (0)
0