Issues (40)

tests/WorksWithRelationsTest.php (9 issues)

1
<?php
2
3
namespace RafflesArgentina\ResourceController;
4
5
use Orchestra\Testbench\TestCase;
6
7
class WorksWithRelationsTest extends TestCase
8
{
9
    use BaseTest;
10
11
    /**
12
     * @coversNothing
13
     */
14
    function testStoreHasOne()
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
15
    {
16
        $fillable = [
17
            'name' => 'Mario',
18
            'email' => '[email protected]',
19
            'password' => bcrypt(str_random()),
20
            'hasOneRelated' => [
21
                'a' => 'blah blah blah',
22
                'b' => 'blah blah blah',
23
                'c' => 'blah blah blah',
24
            ]
25
        ];
26
27
        $this->post('/test', $fillable)
28
            ->assertRedirect('/test')
29
            ->assertSessionHas('rafflesargentina.status.success');
30
31
        $user = \RafflesArgentina\ResourceController\Models\User::first();
32
        $this->assertTrue(!is_null($user->hasOneRelated));
33
34
        foreach ($fillable['hasOneRelated'] as $k => $v) {
35
            $this->assertTrue($user->hasOneRelated->{$k} == $v);
36
        }
37
38
        $user->forceDelete();
39
40
        $this->json('POST', '/test', $fillable)
41
            ->assertStatus(200)
42
            ->assertSessionHas('rafflesargentina.status.success');
43
        $user = \RafflesArgentina\ResourceController\Models\User::first();
44
        $this->assertTrue(!is_null($user->hasOneRelated));
45
    }
46
47
    /**
48
     * @coversNothing
49
     */
50
    function testStoreMorphOne()
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
51
    {
52
        $fillable = [
53
            'name' => 'Mario',
54
            'email' => '[email protected]',
55
            'password' => bcrypt(str_random()),
56
            'morphOneRelated' => [
57
                'a' => 'blah blah blah',
58
                'b' => 'blah blah blah',
59
                'c' => 'blah blah blah',
60
            ]
61
        ];
62
63
        $this->post('/test', $fillable)
64
            ->assertRedirect('/test')
65
            ->assertSessionHas('rafflesargentina.status.success');
66
67
        $user = \RafflesArgentina\ResourceController\Models\User::first();
68
        $this->assertTrue(!is_null($user->morphOneRelated));
69
70
        foreach ($fillable['morphOneRelated'] as $k => $v) {
71
            $this->assertTrue($user->morphOneRelated->{$k} == $v);
72
        }
73
74
        $user->forceDelete();
75
76
        $this->json('POST', '/test', $fillable)
77
            ->assertStatus(200)
78
            ->assertSessionHas('rafflesargentina.status.success');
79
        $user = \RafflesArgentina\ResourceController\Models\User::first();
80
        $this->assertTrue(!is_null($user->morphOneRelated));
81
82
        foreach ($fillable['morphOneRelated'] as $k => $v) {
83
            $this->assertTrue($user->morphOneRelated->{$k} == $v);
84
        }
85
    }
86
87
    /**
88
     * @coversNothing
89
     */
90
    function testStoreBelongsTo()
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
91
    {
92
        $fillable = [
93
            'name' => 'Mario',
94
            'email' => '[email protected]',
95
            'password' => bcrypt(str_random()),
96
            'belongsToRelated' => [
97
                'a' => 'blah blah blah',
98
                'b' => 'blah blah blah',
99
                'c' => 'blah blah blah',
100
            ]
101
        ];
102
103
        $this->post('/test', $fillable)
104
            ->assertRedirect('/test');
105
106
        $user = \RafflesArgentina\ResourceController\Models\User::first();
107
        $this->assertTrue(!is_null($user->belongsToRelated));
108
109
        foreach ($fillable['belongsToRelated'] as $k => $v) {
110
            $this->assertTrue($user->belongsToRelated->{$k} == $v);
111
        }
112
113
        $user->forceDelete();
114
115
        $this->json('POST', '/test', $fillable)->assertStatus(200);
116
        $user = \RafflesArgentina\ResourceController\Models\User::first();
117
        $this->assertTrue(!is_null($user->belongsToRelated));
118
119
        foreach ($fillable['belongsToRelated'] as $k => $v) {
120
            $this->assertTrue($user->belongsToRelated->{$k} == $v);
121
        }
122
    }
123
124
    /**
125
     * @coversNothing
126
     */
127
    function testStoreHasMany()
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
128
    {
129
        $fillable = [
130
            'name' => 'Mario',
131
            'email' => '[email protected]',
132
            'password' => bcrypt(str_random()),
133
            'hasManyRelated' => [
134
                '0' => [
135
                    'a' => 'blah blah blah',
136
                    'b' => 'blah blah blah',
137
                    'c' => 'blah blah blah',
138
                ],
139
                '1' => [
140
                    'a' => 'bleh bleh bleh',
141
                    'b' => 'bleh bleh bleh',
142
                    'c' => 'bleh bleh bleh',
143
                ],
144
                '2' => [
145
                    'a' => 'blih blih blih',
146
                    'b' => 'blih blih blih',
147
                    'c' => 'blih blih blih',
148
                ],
149
                '3' => [
150
                    'a' => 'bloh bloh bloh',
151
                    'b' => 'bloh bloh bloh',
152
                    'c' => 'bloh bloh bloh',
153
                ],
154
                '4' => [
155
                    'a' => 'bluh bluh bluh',
156
                    'b' => 'bluh bluh bluh',
157
                    'c' => 'bluh bluh bluh',
158
                ]
159
            ]
160
        ];
161
162
        $this->post('/test', $fillable)->assertRedirect('/test');
163
        $user = \RafflesArgentina\ResourceController\Models\User::first();
164
        $this->assertTrue($user->hasManyRelated->count() === 5);
165
166
        foreach ($fillable['hasManyRelated'] as $index => $fields) {
167
            foreach ($fields as $k => $v) {
168
                $this->assertTrue($user->hasManyRelated[$index]->{$k} == $v);
169
            }
170
        }
171
172
        $user->forceDelete();
173
174
        $this->json('POST', '/test', $fillable)->assertStatus(200);
175
        $user = \RafflesArgentina\ResourceController\Models\User::first();
176
        $this->assertTrue($user->hasManyRelated->count() === 5);
177
178
        foreach ($fillable['hasManyRelated'] as $index => $fields) {
179
            foreach ($fields as $k => $v) {
180
                $this->assertTrue($user->hasManyRelated[$index]->{$k} == $v);
181
            }
182
        }
183
    }
184
185
    /**
186
     * @coversNothing
187
     */
188
    function testStoreMorphMany()
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
189
    {
190
        $fillable = [
191
            'name' => 'Mario',
192
            'email' => '[email protected]',
193
            'password' => bcrypt(str_random()),
194
            'morphManyRelated' => [
195
                '0' => [
196
                    'a' => 'blah blah blah',
197
                    'b' => 'blah blah blah',
198
                    'c' => 'blah blah blah',
199
                ],
200
                '1' => [
201
                    'a' => 'bleh bleh bleh',
202
                    'b' => 'bleh bleh bleh',
203
                    'c' => 'bleh bleh bleh',
204
                ],
205
                '2' => [
206
                    'a' => 'blih blih blih',
207
                    'b' => 'blih blih blih',
208
                    'c' => 'blih blih blih',
209
                ],
210
                '3' => [
211
                    'a' => 'bloh bloh bloh',
212
                    'b' => 'bloh bloh bloh',
213
                    'c' => 'bloh bloh bloh',
214
                ],
215
                '4' => [
216
                    'a' => 'bluh bluh bluh',
217
                    'b' => 'bluh bluh bluh',
218
                    'c' => 'bluh bluh bluh',
219
                ]
220
            ]
221
        ];
222
223
        $this->post('/test', $fillable)->assertRedirect('/test');
224
        $user = \RafflesArgentina\ResourceController\Models\User::first();
225
        $this->assertTrue($user->morphManyRelated->count() === 5);
226
227
        foreach ($fillable['morphManyRelated'] as $index => $fields) {
228
            foreach ($fields as $k => $v) {
229
                $this->assertTrue($user->morphManyRelated[$index]->{$k} == $v);
230
            }
231
        }
232
233
        $user->forceDelete();
234
235
        $this->json('POST', '/test', $fillable)->assertStatus(200);
236
        $user = \RafflesArgentina\ResourceController\Models\User::first();
237
        $this->assertTrue($user->morphManyRelated->count() === 5);
238
239
        foreach ($fillable['morphManyRelated'] as $index => $fields) {
240
            foreach ($fields as $k => $v) {
241
                $this->assertTrue($user->morphManyRelated[$index]->{$k} == $v);
242
            }
243
        }
244
    }
245
246
    /**
247
     * @coversNothing
248
     */
249
    function testStoreBelongsToManyExistent()
0 ignored issues
show
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
250
    {
251
        $related = factory(\RafflesArgentina\ResourceController\Models\Related::class, 5)->create();
252
253
        $existent = [];
254
        foreach ($related as $k => $model) {
255
            $existent[$k] = $model->getAttributes();
256
        }
257
258
        $fillable = [
259
            'name' => 'Mario',
260
            'email' => '[email protected]',
261
            'password' => bcrypt(str_random()),
262
            'belongsToManyRelated' => [
263
                '0' => [
264
                    'id' => '1',
265
                    'a' => 'blah blah blah',
266
                    'b' => 'blah blah blah',
267
                    'c' => 'blah blah blah',
268
                ],
269
                '1' => [
270
                    'id' => '2',
271
                    'a' => 'bleh bleh bleh',
272
                    'b' => 'bleh bleh bleh',
273
                    'c' => 'bleh bleh bleh',
274
                ],
275
                '2' => [
276
                    'id' => '3',
277
                    'a' => 'blih blih blih',
278
                    'b' => 'blih blih blih',
279
                    'c' => 'blih blih blih',
280
                ],
281
                '3' => [
282
                    'id' => '4',
283
                    'a' => 'bloh bloh bloh',
284
                    'b' => 'bloh bloh bloh',
285
                    'c' => 'bloh bloh bloh',
286
                ],
287
                '4' => [
288
                    'id' => '5',
289
                    'a' => 'bluh bluh bluh',
290
                    'b' => 'bluh bluh bluh',
291
                    'c' => 'bluh bluh bluh',
292
                ]
293
            ]
294
        ];
295
296
        $this->post('/test', $fillable)->assertRedirect('/test');
297
        $user = \RafflesArgentina\ResourceController\Models\User::with('belongsToManyRelated')->first();
298
        $this->assertTrue($user->belongsToManyRelated->count() === 5);
299
300
        foreach ($fillable['belongsToManyRelated'] as $index => $fields) {
301
            foreach ($fields as $k => $v) {
302
                $this->assertTrue($user->belongsToManyRelated[$index]->{$k} == $v);
303
            }
304
        }
305
306
        $user->forceDelete();
307
308
        $this->json('POST', '/test', $fillable)->assertStatus(200);
309
        $user = \RafflesArgentina\ResourceController\Models\User::first();
310
        $this->assertTrue($user->belongsToManyRelated->count() === 5);
311
312
        foreach ($fillable['belongsToManyRelated'] as $index => $fields) {
313
            foreach ($fields as $k => $v) {
314
                $this->assertTrue($user->belongsToManyRelated[$index]->{$k} == $v);
315
            }
316
        }
317
    }
318
319
    /**
320
     * @coversNothing
321
     */
322
    function testStoreBelongsToManyInexistent()
323
    {
324
        $fillable = [
325
            'name' => 'Mario',
326
            'email' => '[email protected]',
327
            'password' => bcrypt(str_random()),
328
            'belongsToManyRelated' => [
329
                '0' => [
330
                    'a' => 'blah blah blah',
331
                    'b' => 'blah blah blah',
332
                    'c' => 'blah blah blah',
333
                ],
334
                '1' => [
335
                    'a' => 'bleh bleh bleh',
336
                    'b' => 'bleh bleh bleh',
337
                    'c' => 'bleh bleh bleh',
338
                ],
339
                '2' => [
340
                    'a' => 'blih blih blih',
341
                    'b' => 'blih blih blih',
342
                    'c' => 'blih blih blih',
343
                ],
344
                '3' => [
345
                    'a' => 'bloh bloh bloh',
346
                    'b' => 'bloh bloh bloh',
347
                    'c' => 'bloh bloh bloh',
348
                ],
349
                '4' => [
350
                    'a' => 'bluh bluh bluh',
351
                    'b' => 'bluh bluh bluh',
352
                    'c' => 'bluh bluh bluh',
353
                ]
354
            ]
355
        ];
356
357
        $this->post('/test', $fillable)->assertRedirect('/test');
358
        $user = \RafflesArgentina\ResourceController\Models\User::with('belongsToManyRelated')->first();
359
        $this->assertTrue($user->belongsToManyRelated->count() === 5);
360
361
        foreach ($fillable['belongsToManyRelated'] as $index => $fields) {
362
            foreach ($fields as $k => $v) {
363
                $this->assertTrue($user->belongsToManyRelated[$index]->{$k} == $v);
364
            }
365
        }
366
367
        $user->forceDelete();
368
369
        $this->json('POST', '/test', $fillable)->assertStatus(200);
370
        $user = \RafflesArgentina\ResourceController\Models\User::first();
371
        $this->assertTrue($user->belongsToManyRelated->count() === 5);
372
373
        foreach ($fillable['belongsToManyRelated'] as $index => $fields) {
374
            foreach ($fields as $k => $v) {
375
                $this->assertTrue($user->belongsToManyRelated[$index]->{$k} == $v);
376
            }
377
        }
378
    }
379
380
    /**
381
     * @coversNothing
382
     */
383
    function testStoreMorphToManyExistent()
384
    {
385
        $related = factory(\RafflesArgentina\ResourceController\Models\Related::class, 5)->create();
386
387
        $existent = [];
388
        foreach ($related as $k => $model) {
389
            $existent[$k] = $model->getAttributes();
390
        }
391
392
        $fillable = [
393
            'name' => 'Mario',
394
            'email' => '[email protected]',
395
            'password' => bcrypt(str_random()),
396
            'morphToManyRelated' => [
397
                '0' => [
398
                    'id' => '1',
399
                    'a' => 'blah blah blah',
400
                    'b' => 'blah blah blah',
401
                    'c' => 'blah blah blah',
402
                ],
403
                '1' => [
404
                    'id' => '2',
405
                    'a' => 'bleh bleh bleh',
406
                    'b' => 'bleh bleh bleh',
407
                    'c' => 'bleh bleh bleh',
408
                ],
409
                '2' => [
410
                    'id' => '3',
411
                    'a' => 'blih blih blih',
412
                    'b' => 'blih blih blih',
413
                    'c' => 'blih blih blih',
414
                ],
415
                '3' => [
416
                    'id' => '4',
417
                    'a' => 'bloh bloh bloh',
418
                    'b' => 'bloh bloh bloh',
419
                    'c' => 'bloh bloh bloh',
420
                ],
421
                '4' => [
422
                    'id' => '5',
423
                    'a' => 'bluh bluh bluh',
424
                    'b' => 'bluh bluh bluh',
425
                    'c' => 'bluh bluh bluh',
426
                ]
427
            ]
428
        ];
429
430
        $this->post('/test', $fillable)->assertRedirect('/test');
431
        $user = \RafflesArgentina\ResourceController\Models\User::with('morphToManyRelated')->first();
432
        $this->assertTrue($user->morphToManyRelated->count() === 5);
0 ignored issues
show
The property morphToManyRelated does not exist on RafflesArgentina\ResourceController\Models\User. Did you mean morphManyRelated?
Loading history...
433
434
        foreach ($fillable['morphToManyRelated'] as $index => $fields) {
435
            foreach ($fields as $k => $v) {
436
                $this->assertTrue($user->morphToManyRelated[$index]->{$k} == $v);
437
            }
438
        }
439
440
        $user->forceDelete();
441
442
        $this->json('POST', '/test', $fillable)->assertStatus(200);
443
        $user = \RafflesArgentina\ResourceController\Models\User::first();
444
        $this->assertTrue($user->morphTOManyRelated->count() === 5);
445
446
        foreach ($fillable['morphToManyRelated'] as $index => $fields) {
447
            foreach ($fields as $k => $v) {
448
                $this->assertTrue($user->morphToManyRelated[$index]->{$k} == $v);
449
            }
450
        }
451
    }
452
453
    /**
454
     * @coversNothing
455
     */
456
    function testStoreMorphToManyInexistent()
457
    {
458
        $fillable = [
459
            'name' => 'Mario',
460
            'email' => '[email protected]',
461
            'password' => bcrypt(str_random()),
462
            'morphToManyRelated' => [
463
                '0' => [
464
                    'a' => 'blah blah blah',
465
                    'b' => 'blah blah blah',
466
                    'c' => 'blah blah blah',
467
                ],
468
                '1' => [
469
                    'a' => 'bleh bleh bleh',
470
                    'b' => 'bleh bleh bleh',
471
                    'c' => 'bleh bleh bleh',
472
                ],
473
                '2' => [
474
                    'a' => 'blih blih blih',
475
                    'b' => 'blih blih blih',
476
                    'c' => 'blih blih blih',
477
                ],
478
                '3' => [
479
                    'a' => 'bloh bloh bloh',
480
                    'b' => 'bloh bloh bloh',
481
                    'c' => 'bloh bloh bloh',
482
                ],
483
                '4' => [
484
                    'a' => 'bluh bluh bluh',
485
                    'b' => 'bluh bluh bluh',
486
                    'c' => 'bluh bluh bluh',
487
                ]
488
            ]
489
        ];
490
491
        $this->post('/test', $fillable)->assertRedirect('/test');
492
        $user = \RafflesArgentina\ResourceController\Models\User::with('morphToManyRelated')->first();
493
        $this->assertTrue($user->morphToManyRelated->count() === 5);
0 ignored issues
show
The property morphToManyRelated does not exist on RafflesArgentina\ResourceController\Models\User. Did you mean morphManyRelated?
Loading history...
494
495
        foreach ($fillable['morphToManyRelated'] as $index => $fields) {
496
            foreach ($fields as $k => $v) {
497
                $this->assertTrue($user->morphToManyRelated[$index]->{$k} == $v);
498
            }
499
        }
500
501
        $user->forceDelete();
502
503
        $this->json('POST', '/test', $fillable)->assertStatus(200);
504
        $user = \RafflesArgentina\ResourceController\Models\User::first();
505
        $this->assertTrue($user->morphToManyRelated->count() === 5);
506
507
        foreach ($fillable['morphToManyRelated'] as $index => $fields) {
508
            foreach ($fields as $k => $v) {
509
                $this->assertTrue($user->morphToManyRelated[$index]->{$k} == $v);
510
            }
511
        }
512
    }
513
514
    /**
515
     * @coversNothing
516
     */
517
    function testUpdateHasOne()
518
    {
519
        $fillable = [
520
            'name' => 'Mario',
521
            'email' => '[email protected]',
522
            'password' => bcrypt(str_random()),
523
            'hasOneRelated' => [
524
                'a' => 'blah blah blah',
525
                'b' => 'blah blah blah',
526
                'c' => 'blah blah blah',
527
            ]
528
        ];
529
530
        $user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create();
531
        $this->put('/test/1', $fillable)->assertRedirect('/test');
532
        $this->assertTrue(!is_null($user->hasOneRelated));
533
534
        $user->forceDelete();
535
536
        $user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create();
537
        $this->json('PUT', '/test/2', $fillable)->assertStatus(200);
538
        $this->assertTrue(!is_null($user->hasOneRelated));
539
    }
540
541
    /**
542
     * @coversNothing
543
     */
544
    function testUpdateMorphOne()
545
    {
546
        $fillable = [
547
            'name' => 'Mario',
548
            'email' => '[email protected]',
549
            'password' => bcrypt(str_random()),
550
            'morphOneRelated' => [
551
                'a' => 'blah blah blah',
552
                'b' => 'blah blah blah',
553
                'c' => 'blah blah blah',
554
            ]
555
        ];
556
557
        $user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create();
558
        $this->put('/test/1', $fillable)->assertRedirect('/test');
559
        $this->assertTrue(!is_null($user->morphOneRelated));
560
561
        $user->forceDelete();
562
563
        $user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create();
564
        $this->json('PUT', '/test/2', $fillable)->assertStatus(200);
565
        $this->assertTrue(!is_null($user->morphOneRelated));
566
    }
567
568
    /**
569
     * @coversNothing
570
     */
571
    function testUpdateBelongsTo()
572
    {
573
        $fillable = [
574
            'name' => 'Mario',
575
            'email' => '[email protected]',
576
            'password' => bcrypt(str_random()),
577
            'belongsToRelated' => [
578
                'a' => 'blah blah blah',
579
                'b' => 'blah blah blah',
580
                'c' => 'blah blah blah',
581
            ]
582
        ];
583
584
        $user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create();
0 ignored issues
show
The assignment to $user is dead and can be removed.
Loading history...
585
        $this->put('/test/1', $fillable)->assertRedirect('/test');
586
        $user = \RafflesArgentina\ResourceController\Models\User::first();
587
        $this->assertTrue(!is_null($user->belongsToRelated));
588
589
        $user->forceDelete();
590
591
        $user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create();
592
        $this->json('PUT', '/test/2', $fillable)->assertStatus(200);
593
        $user = \RafflesArgentina\ResourceController\Models\User::first();
594
        $this->assertTrue(!is_null($user->belongsToRelated));
595
    }
596
597
    /**
598
     * @coversNothing
599
     */
600
    function testUpdateHasMany()
601
    {
602
        $fillable = [
603
            'name' => 'Mario',
604
            'email' => '[email protected]',
605
            'password' => bcrypt(str_random()),
606
            'hasManyRelated' => [
607
                '0' => [
608
                    'id' => '1',
609
                    'a' => 'blah blah blah',
610
                    'b' => 'blah blah blah',
611
                    'c' => 'blah blah blah',
612
                ],
613
                '1' => [
614
                    'id' => '2',
615
                    'a' => 'bleh bleh bleh',
616
                    'b' => 'bleh bleh bleh',
617
                    'c' => 'bleh bleh bleh',
618
                ],
619
                '2' => [
620
                    'id' => '3',
621
                    'a' => 'blih blih blih',
622
                    'b' => 'blih blih blih',
623
                    'c' => 'blih blih blih',
624
                ],
625
                '3' => [
626
                    'id' => '4',
627
                    'a' => 'bloh bloh bloh',
628
                    'b' => 'bloh bloh bloh',
629
                    'c' => 'bloh bloh bloh',
630
                ],
631
                '4' => [
632
                    'id' => '5',
633
                    'a' => 'bluh bluh bluh',
634
                    'b' => 'bluh bluh bluh',
635
                    'c' => 'bluh bluh bluh',
636
                ]
637
            ]
638
        ];
639
640
        $user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create();
641
        $this->put('/test/1', $fillable)->assertRedirect('/test');
642
        $this->assertTrue($user->hasManyRelated->count() === 5);
643
644
        $user->forceDelete();
645
646
        $user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create();
647
        $this->json('PUT', '/test/2', $fillable)->assertStatus(200);
648
        $this->assertTrue($user->hasManyRelated->count() === 5);
649
    }
650
651
    /**
652
     * @coversNothing
653
     */
654
    function testUpdateMorphMany()
655
    {
656
        $fillable = [
657
            'name' => 'Mario',
658
            'email' => '[email protected]',
659
            'password' => bcrypt(str_random()),
660
            'morphManyRelated' => [
661
                '0' => [
662
                    'id' => '1',
663
                    'a' => 'blah blah blah',
664
                    'b' => 'blah blah blah',
665
                    'c' => 'blah blah blah',
666
                ],
667
                '1' => [
668
                    'id' => '2',
669
                    'a' => 'bleh bleh bleh',
670
                    'b' => 'bleh bleh bleh',
671
                    'c' => 'bleh bleh bleh',
672
                ],
673
                '2' => [
674
                    'id' => '3',
675
                    'a' => 'blih blih blih',
676
                    'b' => 'blih blih blih',
677
                    'c' => 'blih blih blih',
678
                ],
679
                '3' => [
680
                    'id' => '4',
681
                    'a' => 'bloh bloh bloh',
682
                    'b' => 'bloh bloh bloh',
683
                    'c' => 'bloh bloh bloh',
684
                ],
685
                '4' => [
686
                    'id' => '5',
687
                    'a' => 'bluh bluh bluh',
688
                    'b' => 'bluh bluh bluh',
689
                    'c' => 'bluh bluh bluh',
690
                ]
691
            ]
692
        ];
693
694
        $user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create();
695
        $this->put('/test/1', $fillable)->assertRedirect('/test');
696
        $this->assertTrue($user->morphManyRelated->count() === 5);
697
698
        $user->forceDelete();
699
700
        $user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create();
701
        $this->json('PUT', '/test/2', $fillable)->assertStatus(200);
702
        $this->assertTrue($user->morphManyRelated->count() === 5);
703
    }
704
705
    /**
706
     * @coversNothing
707
     */
708
    function testUpdateBelongsToManyExistent()
709
    {
710
        $related = factory(\RafflesArgentina\ResourceController\Models\Related::class, 5)->create();
711
712
        $existent = [];
713
        foreach ($related as $k => $model) {
714
            $existent[$k] = $model->getAttributes();
715
        }
716
717
        $fillable = [
718
            'name' => 'Mario',
719
            'email' => '[email protected]',
720
            'password' => bcrypt(str_random()),
721
            'belongsToManyRelated' => [
722
                '0' => [
723
                    'id' => '1',
724
                    'a' => 'blah blah blah',
725
                    'b' => 'blah blah blah',
726
                    'c' => 'blah blah blah',
727
                ],
728
                '1' => [
729
                    'id' => '2',
730
                    'a' => 'bleh bleh bleh',
731
                    'b' => 'bleh bleh bleh',
732
                    'c' => 'bleh bleh bleh',
733
                ],
734
                '2' => [
735
                    'id' => '3',
736
                    'a' => 'blih blih blih',
737
                    'b' => 'blih blih blih',
738
                    'c' => 'blih blih blih',
739
                ],
740
                '3' => [
741
                    'id' => '4',
742
                    'a' => 'bloh bloh bloh',
743
                    'b' => 'bloh bloh bloh',
744
                    'c' => 'bloh bloh bloh',
745
                ],
746
                '4' => [
747
                    'id' => '5',
748
                    'a' => 'bluh bluh bluh',
749
                    'b' => 'bluh bluh bluh',
750
                    'c' => 'bluh bluh bluh',
751
                ]
752
            ]
753
        ];
754
755
        $user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create();
756
        $this->put('/test/1', $fillable)->assertRedirect('/test');
757
        $this->assertTrue($user->belongsToManyRelated->count() === 5);
758
759
        foreach ($fillable['belongsToManyRelated'] as $index => $fields) {
760
            foreach ($fields as $k => $v) {
761
                $this->assertTrue($user->belongsToManyRelated[$index]->{$k} == $v);
762
            }
763
        }
764
765
        $user->forceDelete();
766
767
        $user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create();
768
        $this->json('PUT', '/test/2', $fillable)->assertStatus(200);
769
        $this->assertTrue($user->belongsToManyRelated->count() === 5);
770
771
        foreach ($fillable['belongsToManyRelated'] as $index => $fields) {
772
            foreach ($fields as $k => $v) {
773
                $this->assertTrue($user->belongsToManyRelated[$index]->{$k} == $v);
774
            }
775
        }
776
    }
777
778
    /**
779
     * @coversNothing
780
     */
781
    function testUpdateBelongsToManyInexistent()
782
    {
783
        $fillable = [
784
            'name' => 'Mario',
785
            'email' => '[email protected]',
786
            'password' => bcrypt(str_random()),
787
            'belongsToManyRelated' => [
788
                '0' => [
789
                    'a' => 'blah blah blah',
790
                    'b' => 'blah blah blah',
791
                    'c' => 'blah blah blah',
792
                ],
793
                '1' => [
794
                    'a' => 'bleh bleh bleh',
795
                    'b' => 'bleh bleh bleh',
796
                    'c' => 'bleh bleh bleh',
797
                ],
798
                '2' => [
799
                    'a' => 'blih blih blih',
800
                    'b' => 'blih blih blih',
801
                    'c' => 'blih blih blih',
802
                ],
803
                '3' => [
804
                    'a' => 'bloh bloh bloh',
805
                    'b' => 'bloh bloh bloh',
806
                    'c' => 'bloh bloh bloh',
807
                ],
808
                '4' => [
809
                    'a' => 'bluh bluh bluh',
810
                    'b' => 'bluh bluh bluh',
811
                    'c' => 'bluh bluh bluh',
812
                ]
813
            ]
814
        ];
815
816
        $user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create();
817
        $this->put('/test/1', $fillable)->assertRedirect('/test');
818
        $this->assertTrue($user->belongsToManyRelated->count() === 5);
819
820
        foreach ($fillable['belongsToManyRelated'] as $index => $fields) {
821
            foreach ($fields as $k => $v) {
822
                $this->assertTrue($user->belongsToManyRelated[$index]->{$k} == $v);
823
            }
824
        }
825
826
        $user->forceDelete();
827
828
        $user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create();
829
        $this->json('PUT', '/test/2', $fillable)->assertStatus(200);
830
        $this->assertTrue($user->belongsToManyRelated->count() === 5);
831
832
        foreach ($fillable['belongsToManyRelated'] as $index => $fields) {
833
            foreach ($fields as $k => $v) {
834
                $this->assertTrue($user->belongsToManyRelated[$index]->{$k} == $v);
835
            }
836
        }
837
    }
838
839
    /**
840
     * @coversNothing
841
     */
842
    function testUpdateMorphToManyExistent()
843
    {
844
        $related = factory(\RafflesArgentina\ResourceController\Models\Related::class, 5)->create();
845
846
        $existent = [];
847
        foreach ($related as $k => $model) {
848
            $existent[$k] = $model->getAttributes();
849
        }
850
851
        $fillable = [
852
            'name' => 'Mario',
853
            'email' => '[email protected]',
854
            'password' => bcrypt(str_random()),
855
            'morphToManyRelated' => [
856
                '0' => [
857
                    'id' => '1',
858
                    'a' => 'blah blah blah',
859
                    'b' => 'blah blah blah',
860
                    'c' => 'blah blah blah',
861
                ],
862
                '1' => [
863
                    'id' => '2',
864
                    'a' => 'bleh bleh bleh',
865
                    'b' => 'bleh bleh bleh',
866
                    'c' => 'bleh bleh bleh',
867
                ],
868
                '2' => [
869
                    'id' => '3',
870
                    'a' => 'blih blih blih',
871
                    'b' => 'blih blih blih',
872
                    'c' => 'blih blih blih',
873
                ],
874
                '3' => [
875
                    'id' => '4',
876
                    'a' => 'bloh bloh bloh',
877
                    'b' => 'bloh bloh bloh',
878
                    'c' => 'bloh bloh bloh',
879
                ],
880
                '4' => [
881
                    'id' => '5',
882
                    'a' => 'bluh bluh bluh',
883
                    'b' => 'bluh bluh bluh',
884
                    'c' => 'bluh bluh bluh',
885
                ]
886
            ]
887
        ];
888
889
        $user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create();
890
        $this->put('/test/1', $fillable)->assertRedirect('/test');
891
        $this->assertTrue($user->morphToManyRelated->count() === 5);
892
893
        foreach ($fillable['morphToManyRelated'] as $index => $fields) {
894
            foreach ($fields as $k => $v) {
895
                $this->assertTrue($user->morphToManyRelated[$index]->{$k} == $v);
896
            }
897
        }
898
899
        $user->forceDelete();
900
901
        $user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create();
902
        $this->json('PUT', '/test/2', $fillable)->assertStatus(200);
903
        $this->assertTrue($user->morphToManyRelated->count() === 5);
904
905
        foreach ($fillable['morphToManyRelated'] as $index => $fields) {
906
            foreach ($fields as $k => $v) {
907
                $this->assertTrue($user->morphToManyRelated[$index]->{$k} == $v);
908
            }
909
        }
910
    }
911
912
    /**
913
     * @coversNothing
914
     */
915
    function testUpdateMorphToManyInexistent()
916
    {
917
        $fillable = [
918
            'name' => 'Mario',
919
            'email' => '[email protected]',
920
            'password' => bcrypt(str_random()),
921
            'morphToManyRelated' => [
922
                '0' => [
923
                    'a' => 'blah blah blah',
924
                    'b' => 'blah blah blah',
925
                    'c' => 'blah blah blah',
926
                ],
927
                '1' => [
928
                    'a' => 'bleh bleh bleh',
929
                    'b' => 'bleh bleh bleh',
930
                    'c' => 'bleh bleh bleh',
931
                ],
932
                '2' => [
933
                    'a' => 'blih blih blih',
934
                    'b' => 'blih blih blih',
935
                    'c' => 'blih blih blih',
936
                ],
937
                '3' => [
938
                    'a' => 'bloh bloh bloh',
939
                    'b' => 'bloh bloh bloh',
940
                    'c' => 'bloh bloh bloh',
941
                ],
942
                '4' => [
943
                    'a' => 'bluh bluh bluh',
944
                    'b' => 'bluh bluh bluh',
945
                    'c' => 'bluh bluh bluh',
946
                ]
947
            ]
948
        ];
949
950
        $user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create();
951
        $this->put('/test/1', $fillable)->assertRedirect('/test');
952
        $this->assertTrue($user->morphToManyRelated->count() === 5);
953
954
        foreach ($fillable['morphToManyRelated'] as $index => $fields) {
955
            foreach ($fields as $k => $v) {
956
                $this->assertTrue($user->morphToManyRelated[$index]->{$k} == $v);
957
            }
958
        }
959
960
        $user->forceDelete();
961
962
        $user = factory(\RafflesArgentina\ResourceController\Models\User::class)->create();
963
        $this->json('PUT', '/test/2', $fillable)->assertStatus(200);
964
        $this->assertTrue($user->morphToManyRelated->count() === 5);
965
966
        foreach ($fillable['morphToManyRelated'] as $index => $fields) {
967
            foreach ($fields as $k => $v) {
968
                $this->assertTrue($user->morphToManyRelated[$index]->{$k} == $v);
969
            }
970
        }
971
    }
972
}
973