24
24
/**
25
25
* Class InteractsWithModels
26
26
* @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
30
27
*/
31
28
trait InteractsWithModels
32
29
{
@@ -36,37 +33,37 @@ trait InteractsWithModels
36
33
*
37
34
* @param Model $model
38
35
* 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.
41
38
* @param string|string[]|null $attributeKeys
42
39
* the keys of the model attributes that should be checked, or null to check all.
43
40
* @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
45
43
*/
46
- public function assertModelCreated (
44
+ protected function assertModelCreated (
47
45
Model $ model ,
48
- $ expectedId ,
46
+ $ expectedResourceId ,
49
47
$ attributeKeys = null ,
50
48
$ keyName = null
51
49
) {
52
- if (!$ keyName ) {
53
- $ keyName = $ model ->getKeyName ();
54
- }
55
-
50
+ $ keyName = $ keyName ?: $ model ->getKeyName ();
56
51
$ attributes = $ model ->getAttributes ();
52
+ $ expected = [$ keyName => $ expectedResourceId ];
57
53
58
54
if (is_null ($ attributeKeys )) {
59
55
$ attributeKeys = array_keys ($ attributes );
60
56
}
61
57
62
- $ expected = [];
63
-
64
58
foreach ((array ) $ attributeKeys as $ attr ) {
59
+ if ($ keyName === $ attr ) {
60
+ continue ;
61
+ }
62
+
65
63
$ expected [$ attr ] = isset ($ attributes [$ attr ]) ? $ attributes [$ attr ] : null ;
66
64
}
67
65
68
- $ expected [$ keyName ] = $ expectedId ;
69
- $ this ->seeInDatabase ($ model ->getTable (), $ expected , $ model ->getConnectionName ());
66
+ return $ this ->seeModelInDatabase ($ model , $ expected );
70
67
}
71
68
72
69
/**
@@ -78,8 +75,9 @@ public function assertModelCreated(
78
75
* the expected changed attributes - key to value pairs.
79
76
* @param string|string[] $unchangedKeys
80
77
* the keys of the attributes that should not have changed.
78
+ * @return $this
81
79
*/
82
- public function assertModelPatched (Model $ model , array $ changedAttributes , $ unchangedKeys = [])
80
+ protected function assertModelPatched (Model $ model , array $ changedAttributes , $ unchangedKeys = [])
83
81
{
84
82
/** We need to ensure values are cast to database values */
85
83
$ expected = $ model ->newInstance ($ changedAttributes )->getAttributes ();
@@ -90,29 +88,80 @@ public function assertModelPatched(Model $model, array $changedAttributes, $unch
90
88
}
91
89
92
90
$ expected [$ model ->getKeyName ()] = $ model ->getKey ();
93
- $ this ->seeInDatabase ($ model ->getTable (), $ expected , $ model ->getConnectionName ());
91
+
92
+ return $ this ->seeModelInDatabase ($ model , $ expected );
94
93
}
95
94
96
95
/**
97
96
* Assert that a model was deleted.
98
97
*
99
98
* @param Model $model
99
+ * @return $this
100
100
*/
101
- public function assertModelDeleted (Model $ model )
101
+ protected function assertModelDeleted (Model $ model )
102
102
{
103
- $ this ->notSeeInDatabase ($ model ->getTable (), [
104
- $ model ->getKeyName () => $ model ->getKey ()
105
- ], $ model ->getConnectionName ());
103
+ return $ this ->notSeeModelInDatabase ($ model , [$ model ->getKeyName () => $ model ->getKey ()]);
106
104
}
107
105
108
106
/**
109
107
* Assert that a model was soft deleted.
110
108
*
111
109
* @param Model $model
110
+ * @return $this
112
111
*/
113
- public function assertModelTrashed (Model $ model )
112
+ protected function assertModelTrashed (Model $ model )
114
113
{
115
114
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 ();
117
166
}
118
167
}
0 commit comments