2
2
3
3
namespace Illuminate \Tests \Database ;
4
4
5
+ use Illuminate \Database \Capsule \Manager as DB ;
5
6
use Illuminate \Database \Eloquent \Builder ;
6
7
use Illuminate \Database \Eloquent \Collection ;
7
8
use Illuminate \Database \Eloquent \Model ;
9
+ use Illuminate \Database \Eloquent \Model as Eloquent ;
8
10
use Illuminate \Support \Collection as BaseCollection ;
9
11
use LogicException ;
10
12
use Mockery as m ;
13
15
14
16
class DatabaseEloquentCollectionTest extends TestCase
15
17
{
18
+ /**
19
+ * Setup the database schema.
20
+ *
21
+ * @return void
22
+ */
23
+ protected function setUp (): void
24
+ {
25
+ $ db = new DB ;
26
+
27
+ $ db ->addConnection ([
28
+ 'driver ' => 'sqlite ' ,
29
+ 'database ' => ':memory: ' ,
30
+ ]);
31
+
32
+ $ db ->bootEloquent ();
33
+ $ db ->setAsGlobal ();
34
+
35
+ $ this ->createSchema ();
36
+ }
37
+
38
+ protected function createSchema ()
39
+ {
40
+ $ this ->schema ()->create ('users ' , function ($ table ) {
41
+ $ table ->increments ('id ' );
42
+ $ table ->string ('email ' )->unique ();
43
+ });
44
+
45
+ $ this ->schema ()->create ('articles ' , function ($ table ) {
46
+ $ table ->increments ('id ' );
47
+ $ table ->integer ('user_id ' );
48
+ $ table ->string ('title ' );
49
+ });
50
+
51
+ $ this ->schema ()->create ('comments ' , function ($ table ) {
52
+ $ table ->increments ('id ' );
53
+ $ table ->integer ('article_id ' );
54
+ $ table ->string ('content ' );
55
+ });
56
+ }
57
+
16
58
protected function tearDown (): void
17
59
{
60
+ $ this ->schema ()->drop ('users ' );
61
+ $ this ->schema ()->drop ('articles ' );
62
+ $ this ->schema ()->drop ('comments ' );
18
63
m::close ();
19
64
}
20
65
@@ -536,6 +581,54 @@ public function testConvertingEmptyCollectionToQueryThrowsException()
536
581
$ c = new Collection ;
537
582
$ c ->toQuery ();
538
583
}
584
+
585
+ public function testLoadExistsShouldCastBool ()
586
+ {
587
+ $ this ->seedData ();
588
+ $ user = EloquentTestUserModel::with ('articles ' )->first ();
589
+ $ user ->articles ->loadExists ('comments ' );
590
+ $ commentsExists = $ user ->articles ->pluck ('comments_exists ' )->toArray ();
591
+ $ this ->assertContainsOnly ('bool ' , $ commentsExists );
592
+ }
593
+
594
+ /**
595
+ * Helpers...
596
+ */
597
+ protected function seedData ()
598
+ {
599
+ $ user = EloquentTestUserModel::
create ([
'id ' =>
1 ,
'email ' =>
'[email protected] ' ]);
600
+
601
+ EloquentTestArticleModel::query ()->insert ([
602
+ ['user_id ' => 1 , 'title ' => 'Another title ' ],
603
+ ['user_id ' => 1 , 'title ' => 'Another title ' ],
604
+ ['user_id ' => 1 , 'title ' => 'Another title ' ],
605
+ ]);
606
+
607
+ EloquentTestCommentModel::query ()->insert ([
608
+ ['article_id ' => 1 , 'content ' => 'Another comment ' ],
609
+ ['article_id ' => 2 , 'content ' => 'Another comment ' ],
610
+ ]);
611
+ }
612
+
613
+ /**
614
+ * Get a database connection instance.
615
+ *
616
+ * @return \Illuminate\Database\ConnectionInterface
617
+ */
618
+ protected function connection ()
619
+ {
620
+ return Eloquent::getConnectionResolver ()->connection ();
621
+ }
622
+
623
+ /**
624
+ * Get a schema builder instance.
625
+ *
626
+ * @return \Illuminate\Database\Schema\Builder
627
+ */
628
+ protected function schema ()
629
+ {
630
+ return $ this ->connection ()->getSchemaBuilder ();
631
+ }
539
632
}
540
633
541
634
class TestEloquentCollectionModel extends Model
@@ -548,3 +641,34 @@ public function getTestAttribute()
548
641
return 'test ' ;
549
642
}
550
643
}
644
+
645
+ class EloquentTestUserModel extends Model
646
+ {
647
+ protected $ table = 'users ' ;
648
+ protected $ guarded = [];
649
+ public $ timestamps = false ;
650
+
651
+ public function articles ()
652
+ {
653
+ return $ this ->hasMany (EloquentTestArticleModel::class, 'user_id ' );
654
+ }
655
+ }
656
+
657
+ class EloquentTestArticleModel extends Model
658
+ {
659
+ protected $ table = 'articles ' ;
660
+ protected $ guarded = [];
661
+ public $ timestamps = false ;
662
+
663
+ public function comments ()
664
+ {
665
+ return $ this ->hasMany (EloquentTestCommentModel::class, 'article_id ' );
666
+ }
667
+ }
668
+
669
+ class EloquentTestCommentModel extends Model
670
+ {
671
+ protected $ table = 'comments ' ;
672
+ protected $ guarded = [];
673
+ public $ timestamps = false ;
674
+ }
0 commit comments