|
| 1 | +<?php |
| 2 | +namespace JeremyHarris\LazyLoad\Test\TestCase\ORM; |
| 3 | + |
| 4 | +use Cake\ORM\TableRegistry; |
| 5 | +use Cake\TestSuite\TestCase; |
| 6 | +use JeremyHarris\LazyLoad\TestApp\Model\Entity\Comment; |
| 7 | + |
| 8 | +/** |
| 9 | + * LazyLoadEntityTrait test |
| 10 | + */ |
| 11 | +class LazyLoadEntityTraitTest extends TestCase |
| 12 | +{ |
| 13 | + |
| 14 | + /** |
| 15 | + * Fixtures |
| 16 | + * |
| 17 | + * @var array |
| 18 | + */ |
| 19 | + public $fixtures = [ |
| 20 | + 'plugin.JeremyHarris\LazyLoad.articles', |
| 21 | + 'plugin.JeremyHarris\LazyLoad.articles_tags', |
| 22 | + 'plugin.JeremyHarris\LazyLoad.authors', |
| 23 | + 'plugin.JeremyHarris\LazyLoad.comments', |
| 24 | + 'plugin.JeremyHarris\LazyLoad.tags', |
| 25 | + 'plugin.JeremyHarris\LazyLoad.users', |
| 26 | + ]; |
| 27 | + |
| 28 | + /** |
| 29 | + * setUp |
| 30 | + * |
| 31 | + * @return void |
| 32 | + */ |
| 33 | + public function setUp() |
| 34 | + { |
| 35 | + parent::setUp(); |
| 36 | + |
| 37 | + $this->Articles = TableRegistry::get('Articles'); |
| 38 | + $this->Articles->entityClass('\JeremyHarris\LazyLoad\TestApp\Model\Entity\LazyLoadableEntity'); |
| 39 | + $this->Articles->belongsTo('Authors'); |
| 40 | + $this->Articles->hasMany('Comments'); |
| 41 | + $this->Articles->belongsToMany('Tags', [ |
| 42 | + 'joinTable' => 'articles_tags', |
| 43 | + ]); |
| 44 | + } |
| 45 | + |
| 46 | + /** |
| 47 | + * tests that lazyload doesn't interfere with existing accessor methods |
| 48 | + * |
| 49 | + * @return void |
| 50 | + */ |
| 51 | + public function testGetAccessor() |
| 52 | + { |
| 53 | + $this->Comments = TableRegistry::get('Comments'); |
| 54 | + $this->Comments->entityClass('\JeremyHarris\LazyLoad\TestApp\Model\Entity\Comment'); |
| 55 | + $comment = $this->Comments->get(1); |
| 56 | + |
| 57 | + $this->assertEquals('accessor', $comment->accessor); |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * tests get() when property isn't associated |
| 62 | + * |
| 63 | + * @return void |
| 64 | + */ |
| 65 | + public function testGet() |
| 66 | + { |
| 67 | + $article = $this->Articles->get(1); |
| 68 | + |
| 69 | + $this->assertNull($article->not_associated); |
| 70 | + } |
| 71 | + |
| 72 | + /** |
| 73 | + * tests cases where `source()` is empty, caused when an entity is manually |
| 74 | + * created |
| 75 | + * |
| 76 | + * @return void |
| 77 | + */ |
| 78 | + public function testEmptySource() |
| 79 | + { |
| 80 | + $this->Comments = TableRegistry::get('Comments'); |
| 81 | + $this->Comments->belongsTo('Authors', [ |
| 82 | + 'foreignKey' => 'user_id' |
| 83 | + ]); |
| 84 | + |
| 85 | + $comment = new Comment(['id' => 1, 'user_id' => 2]); |
| 86 | + $author = $comment->author; |
| 87 | + |
| 88 | + $this->assertEquals(2, $author->id); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * tests deep associations with lazy loaded entities |
| 93 | + * |
| 94 | + * @return void |
| 95 | + */ |
| 96 | + public function testDeepLazyLoad() |
| 97 | + { |
| 98 | + $this->Comments = TableRegistry::get('Comments'); |
| 99 | + $this->Comments->entityClass('\JeremyHarris\LazyLoad\TestApp\Model\Entity\LazyLoadableEntity'); |
| 100 | + $this->Comments->belongsTo('Users'); |
| 101 | + |
| 102 | + $article = $this->Articles->get(1); |
| 103 | + |
| 104 | + $comments = $article->comments; |
| 105 | + |
| 106 | + $expected = [ |
| 107 | + 1 => 'nate', |
| 108 | + 2 => 'garrett', |
| 109 | + 3 => 'mariano', |
| 110 | + 4 => 'mariano', |
| 111 | + ]; |
| 112 | + foreach ($comments as $comment) { |
| 113 | + $this->assertEquals($expected[$comment->id], $comment->user->username); |
| 114 | + } |
| 115 | + } |
| 116 | + |
| 117 | + /** |
| 118 | + * tests lazy loading |
| 119 | + * |
| 120 | + * @return void |
| 121 | + */ |
| 122 | + public function testLazyLoad() |
| 123 | + { |
| 124 | + $article = $this->Articles->get(1); |
| 125 | + $tags = $article->tags; |
| 126 | + |
| 127 | + $this->assertEquals(2, count($tags)); |
| 128 | + } |
| 129 | + |
| 130 | + /** |
| 131 | + * tests has() |
| 132 | + * |
| 133 | + * @return void |
| 134 | + */ |
| 135 | + public function testHas() |
| 136 | + { |
| 137 | + $article = $this->Articles->get(1); |
| 138 | + |
| 139 | + $serialized = $article->toArray(); |
| 140 | + $this->assertArrayNotHasKey('author', $serialized); |
| 141 | + |
| 142 | + $this->assertTrue($article->has('author')); |
| 143 | + } |
| 144 | + |
| 145 | + /** |
| 146 | + * tests that if we contain an association, the lazy loader doesn't overwrite |
| 147 | + * it |
| 148 | + * |
| 149 | + * @return void |
| 150 | + */ |
| 151 | + public function testDontInterfereWithContain() |
| 152 | + { |
| 153 | + $this->Articles = $this->getMockForModel('Articles', ['_lazyLoad'], ['table' => 'articles']); |
| 154 | + $this->Articles->belongsTo('Authors'); |
| 155 | + |
| 156 | + $this->Articles |
| 157 | + ->expects($this->never()) |
| 158 | + ->method('_lazyLoad'); |
| 159 | + |
| 160 | + $article = $this->Articles->find()->contain('Authors')->first(); |
| 161 | + |
| 162 | + $this->assertEquals('mariano', $article->author->name); |
| 163 | + } |
| 164 | +} |
0 commit comments