Skip to content

Commit 23fe8be

Browse files
committedMar 14, 2016
Initial commit
0 parents  commit 23fe8be

18 files changed

+856
-0
lines changed
 

‎.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
/vendor
2+
/composer.lock
3+
/phpunit.xml

‎.travis.yml

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
language: php
2+
3+
php:
4+
- 5.5
5+
- 5.6
6+
- 7.0
7+
8+
sudo: false
9+
10+
env:
11+
- DB=mysql db_dsn='mysql://travis@0.0.0.0/cakephp_test'
12+
- DB=pgsql db_dsn='postgres://travis@127.0.0.1/cakephp_test'
13+
- DB=sqlite
14+
15+
matrix:
16+
fast_finish: true
17+
include:
18+
- php: 7.0
19+
env: PHPCS=1
20+
21+
install:
22+
- composer self-update
23+
- composer install --prefer-dist
24+
25+
before_script:
26+
- sh -c "if [ '$DB' = 'mysql' ]; then mysql -e 'CREATE DATABASE cakephp_test;'; fi"
27+
- sh -c "if [ '$DB' = 'pgsql' ]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi"
28+
- phpenv rehash
29+
- set +H
30+
31+
script:
32+
- sh -c "if [ '$PHPCS' != '1' ]; then phpunit; fi"
33+
- sh -c "if [ '$PHPCS' = '1' ]; then ./vendor/bin/phpcs -n -p --extensions=php --standard=vendor/cakephp/cakephp-codesniffer/CakePHP ./src ./tests; fi"
34+
35+
notifications:
36+
email: false

‎LICENSE

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
Copyright (C) Jeremy Harris
2+
3+
Permission is hereby granted, free of charge, to any person obtaining a copy
4+
of this software and associated documentation files (the "Software"), to deal
5+
in the Software without restriction, including without limitation the rights
6+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
7+
copies of the Software, and to permit persons to whom the Software is furnished
8+
to do so, subject to the following conditions:
9+
10+
The above copyright notice and this permission notice shall be included in all
11+
copies or substantial portions of the Software.
12+
13+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
14+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
16+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
17+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
19+
THE SOFTWARE.

‎README.md

+64
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
[![Build Status](https://secure.travis-ci.org/jeremyharris/cakephp-lazyload.png?branch=master)](http://travis-ci.org/jeremyharris/cakephp-lazyload)
2+
3+
# CakePHP LazyLoad Plugin
4+
5+
A lazy loader for CakePHP entities.
6+
7+
## Installation
8+
9+
Requirements
10+
11+
- CakePHP >= 3.1.x, < 4.0.0
12+
- sloth
13+
14+
`$ composer require jeremyharris/cakephp-lazyload`
15+
16+
Load it up in your bootstrap:
17+
18+
`CakePlugin::load('JeremyHarris\LazyLoad');`
19+
20+
## Usage
21+
22+
Add the trait to the entity you wish to lazily load association data for. Or,
23+
attach it to your base entity if you have one to affect all entities:
24+
25+
**src/Model/Entity/User.php**
26+
```php
27+
<?php
28+
namespace App\Model\Entity;
29+
30+
use Cake\ORM\Entity;
31+
use JeremyHarris\LazyLoad\ORM\LazyLoadEntityTrait;
32+
33+
class User extends Entity
34+
{
35+
use LazyLoadEntityTrait;
36+
}
37+
```
38+
39+
Associations to the Users table can now be lazily loaded from the entity!
40+
41+
### Example
42+
43+
Assuming your Users table belongsTo Groups, you can lazily load Groups instead
44+
of using `contain()`.
45+
46+
```php
47+
<?php
48+
// get an entity, don't worry about contain
49+
$user = $this->Users->get(1);
50+
// group is lazily loaded when you call it here
51+
$groupName = $user->group->name;
52+
```
53+
54+
## Notes
55+
56+
This is not a replacement for contain, which can write complex queries to dictate
57+
what data to contain. The lazy loader obeys the association's conditions that
58+
you set when defining the association on the table, but apart from that it grabs
59+
all associated data.
60+
61+
*The lazy loader requires that your result set is hydrated in order to
62+
provide lazy loading functionality.*
63+
64+
> Special thanks to @lorenzo for reviewing the plugin!

‎composer.json

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
{
2+
"name": "jeremyharris/cakephp-lazyload",
3+
"description": "An association lazy-loader for CakePHP",
4+
"type": "cakephp-plugin",
5+
"keywords": [
6+
"cakephp", "plugin", "lazy load"
7+
],
8+
"require-dev": {
9+
"phpunit/phpunit": "4.1.*",
10+
"cakephp/cakephp-codesniffer": "dev-master"
11+
},
12+
"require": {
13+
"cakephp/cakephp": ">=3.1.0 <4.0"
14+
},
15+
"autoload": {
16+
"psr-4": {
17+
"JeremyHarris\\LazyLoad\\": "src"
18+
}
19+
},
20+
"autoload-dev": {
21+
"psr-4": {
22+
"JeremyHarris\\LazyLoad\\Test\\": "tests",
23+
"JeremyHarris\\LazyLoad\\TestApp\\": "tests/test_app",
24+
"Cake\\Test\\": "./vendor/cakephp/cakephp/tests"
25+
}
26+
}
27+
}

‎phpunit.xml.dist

+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<phpunit
3+
colors="true"
4+
processIsolation="false"
5+
stopOnFailure="false"
6+
syntaxCheck="false"
7+
bootstrap="./tests/bootstrap.php"
8+
>
9+
10+
<testsuites>
11+
<testsuite name="Plugin Test Suite">
12+
<directory>./tests/TestCase</directory>
13+
</testsuite>
14+
</testsuites>
15+
16+
<listeners>
17+
<listener
18+
class="\Cake\TestSuite\Fixture\FixtureInjector"
19+
file="./vendor/cakephp/cakephp/src/TestSuite/Fixture/FixtureInjector.php"
20+
>
21+
<arguments>
22+
<object class="\Cake\TestSuite\Fixture\FixtureManager" />
23+
</arguments>
24+
</listener>
25+
</listeners>
26+
27+
</phpunit>

‎src/ORM/LazyLoadEntityTrait.php

+88
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
<?php
2+
namespace JeremyHarris\LazyLoad\ORM;
3+
4+
use Cake\ORM\Association;
5+
use Cake\ORM\Table;
6+
use Cake\ORM\TableRegistry;
7+
use Cake\Utility\Inflector;
8+
9+
/**
10+
* LazyLoadEntity trait
11+
*
12+
* Lazily loads associated data when it doesn't exist and is requested on the
13+
* entity
14+
*/
15+
trait LazyLoadEntityTrait
16+
{
17+
18+
/**
19+
* Overrides magic get to check for associated data to lazy load, if that
20+
* property doesn't already exist
21+
*
22+
* @param strig $property Property
23+
* @return mixed
24+
*/
25+
public function &__get($property)
26+
{
27+
$entityHas = parent::has($property);
28+
29+
if ($entityHas === false) {
30+
$this->_lazyLoad($property);
31+
}
32+
33+
return parent::__get($property);
34+
}
35+
36+
/**
37+
* Overrides has method to account for a lazy loaded property
38+
*
39+
* @param string $property Property
40+
* @return bool
41+
*/
42+
public function has($property)
43+
{
44+
$entityHas = parent::has($property);
45+
46+
if ($entityHas === false) {
47+
$this->_lazyLoad($property);
48+
}
49+
50+
return parent::has($property);
51+
}
52+
53+
/**
54+
* Lazy loads association data onto the entity
55+
*
56+
* @param string $property Property
57+
* @return void
58+
*/
59+
protected function _lazyLoad($property)
60+
{
61+
$repository = $this->_repository($property);
62+
63+
$association = $repository
64+
->associations()
65+
->getByProperty($property);
66+
67+
if ($association === null) {
68+
return;
69+
}
70+
71+
$repository->loadInto($this, [$association->name()]);
72+
}
73+
74+
/**
75+
* Gets the repository for this entity
76+
*
77+
* @return Table
78+
*/
79+
protected function _repository()
80+
{
81+
$source = $this->source();
82+
if ($source === null) {
83+
list(, $class) = namespaceSplit(get_class($this));
84+
$source = Inflector::pluralize($class);
85+
}
86+
return TableRegistry::get($source);
87+
}
88+
}

‎tests/Fixture/ArticlesFixture.php

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
<?php
2+
/**
3+
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4+
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5+
*
6+
* Licensed under The MIT License
7+
* For full copyright and license information, please see the LICENSE.txt
8+
* Redistributions of files must retain the above copyright notice
9+
*
10+
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11+
* @link http://cakephp.org CakePHP(tm) Project
12+
* @since 1.2.0
13+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
14+
*/
15+
namespace JeremyHarris\LazyLoad\Test\Fixture;
16+
17+
use Cake\TestSuite\Fixture\TestFixture;
18+
19+
class ArticlesFixture extends TestFixture
20+
{
21+
22+
/**
23+
* fields property
24+
*
25+
* @var array
26+
*/
27+
public $fields = [
28+
'id' => ['type' => 'integer'],
29+
'author_id' => ['type' => 'integer', 'null' => true],
30+
'title' => ['type' => 'string', 'null' => true],
31+
'body' => 'text',
32+
'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
33+
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
34+
];
35+
36+
/**
37+
* records property
38+
*
39+
* @var array
40+
*/
41+
public $records = [
42+
['author_id' => 1, 'title' => 'First Article', 'body' => 'First Article Body', 'published' => 'Y'],
43+
['author_id' => 3, 'title' => 'Second Article', 'body' => 'Second Article Body', 'published' => 'Y'],
44+
['author_id' => 1, 'title' => 'Third Article', 'body' => 'Third Article Body', 'published' => 'Y']
45+
];
46+
}

‎tests/Fixture/ArticlesTagsFixture.php

+57
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
<?php
2+
/**
3+
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4+
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5+
*
6+
* Licensed under The MIT License
7+
* For full copyright and license information, please see the LICENSE.txt
8+
* Redistributions of files must retain the above copyright notice
9+
*
10+
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11+
* @link http://cakephp.org CakePHP(tm) Project
12+
* @since 1.2.0
13+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
14+
*/
15+
namespace JeremyHarris\LazyLoad\Test\Fixture;
16+
17+
use Cake\TestSuite\Fixture\TestFixture;
18+
19+
/**
20+
* Short description for class.
21+
*
22+
*/
23+
class ArticlesTagsFixture extends TestFixture
24+
{
25+
26+
/**
27+
* fields property
28+
*
29+
* @var array
30+
*/
31+
public $fields = [
32+
'article_id' => ['type' => 'integer', 'null' => false],
33+
'tag_id' => ['type' => 'integer', 'null' => false],
34+
'_constraints' => [
35+
'unique_tag' => ['type' => 'primary', 'columns' => ['article_id', 'tag_id']],
36+
'tag_id_fk' => [
37+
'type' => 'foreign',
38+
'columns' => ['tag_id'],
39+
'references' => ['tags', 'id'],
40+
'update' => 'cascade',
41+
'delete' => 'cascade',
42+
]
43+
]
44+
];
45+
46+
/**
47+
* records property
48+
*
49+
* @var array
50+
*/
51+
public $records = [
52+
['article_id' => 1, 'tag_id' => 1],
53+
['article_id' => 1, 'tag_id' => 2],
54+
['article_id' => 2, 'tag_id' => 1],
55+
['article_id' => 2, 'tag_id' => 3]
56+
];
57+
}

‎tests/Fixture/AuthorsFixture.php

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
<?php
2+
/**
3+
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4+
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5+
*
6+
* Licensed under The MIT License
7+
* For full copyright and license information, please see the LICENSE.txt
8+
* Redistributions of files must retain the above copyright notice
9+
*
10+
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11+
* @link http://cakephp.org CakePHP(tm) Project
12+
* @since 1.2.0
13+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
14+
*/
15+
namespace JeremyHarris\LazyLoad\Test\Fixture;
16+
17+
use Cake\TestSuite\Fixture\TestFixture;
18+
19+
/**
20+
* Short description for class.
21+
*
22+
*/
23+
class AuthorsFixture extends TestFixture
24+
{
25+
26+
/**
27+
* fields property
28+
*
29+
* @var array
30+
*/
31+
public $fields = [
32+
'id' => ['type' => 'integer'],
33+
'name' => ['type' => 'string', 'default' => null],
34+
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
35+
];
36+
37+
/**
38+
* records property
39+
*
40+
* @var array
41+
*/
42+
public $records = [
43+
['name' => 'mariano'],
44+
['name' => 'nate'],
45+
['name' => 'larry'],
46+
['name' => 'garrett'],
47+
];
48+
}

‎tests/Fixture/CommentsFixture.php

+55
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
<?php
2+
/**
3+
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4+
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5+
*
6+
* Licensed under The MIT License
7+
* For full copyright and license information, please see the LICENSE.txt
8+
* Redistributions of files must retain the above copyright notice
9+
*
10+
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11+
* @link http://cakephp.org CakePHP(tm) Project
12+
* @since 1.2.0
13+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
14+
*/
15+
namespace JeremyHarris\LazyLoad\Test\Fixture;
16+
17+
use Cake\TestSuite\Fixture\TestFixture;
18+
19+
/**
20+
* Short description for class.
21+
*
22+
*/
23+
class CommentsFixture extends TestFixture
24+
{
25+
26+
/**
27+
* fields property
28+
*
29+
* @var array
30+
*/
31+
public $fields = [
32+
'id' => ['type' => 'integer'],
33+
'article_id' => ['type' => 'integer', 'null' => false],
34+
'user_id' => ['type' => 'integer', 'null' => false],
35+
'comment' => ['type' => 'text'],
36+
'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'],
37+
'created' => ['type' => 'datetime'],
38+
'updated' => ['type' => 'datetime'],
39+
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
40+
];
41+
42+
/**
43+
* records property
44+
*
45+
* @var array
46+
*/
47+
public $records = [
48+
['article_id' => 1, 'user_id' => 2, 'comment' => 'First Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:45:23', 'updated' => '2007-03-18 10:47:31'],
49+
['article_id' => 1, 'user_id' => 4, 'comment' => 'Second Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:47:23', 'updated' => '2007-03-18 10:49:31'],
50+
['article_id' => 1, 'user_id' => 1, 'comment' => 'Third Comment for First Article', 'published' => 'Y', 'created' => '2007-03-18 10:49:23', 'updated' => '2007-03-18 10:51:31'],
51+
['article_id' => 1, 'user_id' => 1, 'comment' => 'Fourth Comment for First Article', 'published' => 'N', 'created' => '2007-03-18 10:51:23', 'updated' => '2007-03-18 10:53:31'],
52+
['article_id' => 2, 'user_id' => 1, 'comment' => 'First Comment for Second Article', 'published' => 'Y', 'created' => '2007-03-18 10:53:23', 'updated' => '2007-03-18 10:55:31'],
53+
['article_id' => 2, 'user_id' => 2, 'comment' => 'Second Comment for Second Article', 'published' => 'Y', 'created' => '2007-03-18 10:55:23', 'updated' => '2007-03-18 10:57:31']
54+
];
55+
}

‎tests/Fixture/TagsFixture.php

+47
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
<?php
2+
/**
3+
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4+
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5+
*
6+
* Licensed under The MIT License
7+
* For full copyright and license information, please see the LICENSE.txt
8+
* Redistributions of files must retain the above copyright notice
9+
*
10+
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11+
* @link http://cakephp.org CakePHP(tm) Project
12+
* @since 1.2.0
13+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
14+
*/
15+
namespace JeremyHarris\LazyLoad\Test\Fixture;
16+
17+
use Cake\TestSuite\Fixture\TestFixture;
18+
19+
/**
20+
* Class TagFixture
21+
*
22+
*/
23+
class TagsFixture extends TestFixture
24+
{
25+
26+
/**
27+
* fields property
28+
*
29+
* @var array
30+
*/
31+
public $fields = [
32+
'id' => ['type' => 'integer', 'null' => false],
33+
'name' => ['type' => 'string', 'null' => false],
34+
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
35+
];
36+
37+
/**
38+
* records property
39+
*
40+
* @var array
41+
*/
42+
public $records = [
43+
['name' => 'tag1'],
44+
['name' => 'tag2'],
45+
['name' => 'tag3']
46+
];
47+
}

‎tests/Fixture/UsersFixture.php

+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
<?php
2+
/**
3+
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
4+
* Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
5+
*
6+
* Licensed under The MIT License
7+
* For full copyright and license information, please see the LICENSE.txt
8+
* Redistributions of files must retain the above copyright notice
9+
*
10+
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
11+
* @link http://cakephp.org CakePHP(tm) Project
12+
* @since 1.2.0
13+
* @license http://www.opensource.org/licenses/mit-license.php MIT License
14+
*/
15+
namespace JeremyHarris\LazyLoad\Test\Fixture;
16+
17+
use Cake\TestSuite\Fixture\TestFixture;
18+
19+
/**
20+
* Class UserFixture
21+
*
22+
*/
23+
class UsersFixture extends TestFixture
24+
{
25+
26+
/**
27+
* fields property
28+
*
29+
* @var array
30+
*/
31+
public $fields = [
32+
'id' => ['type' => 'integer'],
33+
'username' => ['type' => 'string', 'null' => true],
34+
'password' => ['type' => 'string', 'null' => true],
35+
'created' => ['type' => 'timestamp', 'null' => true],
36+
'updated' => ['type' => 'timestamp', 'null' => true],
37+
'_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]]
38+
];
39+
40+
/**
41+
* records property
42+
*
43+
* @var array
44+
*/
45+
public $records = [
46+
['username' => 'mariano', 'password' => '$2a$10$u05j8FjsvLBNdfhBhc21LOuVMpzpabVXQ9OpC2wO3pSO0q6t7HHMO', 'created' => '2007-03-17 01:16:23', 'updated' => '2007-03-17 01:18:31'],
47+
['username' => 'nate', 'password' => '$2a$10$u05j8FjsvLBNdfhBhc21LOuVMpzpabVXQ9OpC2wO3pSO0q6t7HHMO', 'created' => '2008-03-17 01:18:23', 'updated' => '2008-03-17 01:20:31'],
48+
['username' => 'larry', 'password' => '$2a$10$u05j8FjsvLBNdfhBhc21LOuVMpzpabVXQ9OpC2wO3pSO0q6t7HHMO', 'created' => '2010-05-10 01:20:23', 'updated' => '2010-05-10 01:22:31'],
49+
['username' => 'garrett', 'password' => '$2a$10$u05j8FjsvLBNdfhBhc21LOuVMpzpabVXQ9OpC2wO3pSO0q6t7HHMO', 'created' => '2012-06-10 01:22:23', 'updated' => '2012-06-12 01:24:31'],
50+
];
51+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,164 @@
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+
}

‎tests/bootstrap.php

+92
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
use Cake\Cache\Cache;
3+
use Cake\Core\Configure;
4+
use Cake\Core\Plugin;
5+
use Cake\Datasource\ConnectionManager;
6+
use Cake\Log\Log;
7+
8+
require_once 'vendor/autoload.php';
9+
10+
// Path constants to a few helpful things.
11+
define('ROOT', dirname(__DIR__) . DS);
12+
define('CAKE_CORE_INCLUDE_PATH', ROOT . 'vendor' . DS . 'cakephp' . DS . 'cakephp');
13+
define('CORE_PATH', ROOT . 'vendor' . DS . 'cakephp' . DS . 'cakephp' . DS);
14+
define('CAKE', CORE_PATH . 'src' . DS);
15+
define('TESTS', ROOT . 'tests');
16+
define('APP', ROOT . 'tests' . DS . 'test_app' . DS);
17+
define('APP_DIR', 'test_app');
18+
define('WEBROOT_DIR', 'webroot');
19+
define('WWW_ROOT', APP . 'webroot' . DS);
20+
define('TMP', sys_get_temp_dir() . DS);
21+
define('CONFIG', APP . 'config' . DS);
22+
define('CACHE', TMP);
23+
define('LOGS', TMP);
24+
25+
require_once CORE_PATH . 'config/bootstrap.php';
26+
27+
date_default_timezone_set('UTC');
28+
mb_internal_encoding('UTF-8');
29+
30+
Configure::write('debug', true);
31+
Configure::write('App', [
32+
'namespace' => 'JeremyHarris\\LazyLoad\\TestApp',
33+
'encoding' => 'UTF-8',
34+
'base' => false,
35+
'baseUrl' => false,
36+
'dir' => 'src',
37+
'webroot' => 'webroot',
38+
'www_root' => APP . 'webroot',
39+
'fullBaseUrl' => 'http://localhost',
40+
'imageBaseUrl' => 'img/',
41+
'jsBaseUrl' => 'js/',
42+
'cssBaseUrl' => 'css/',
43+
'paths' => [
44+
'plugins' => [APP . 'Plugin' . DS],
45+
'templates' => [APP . 'Template' . DS]
46+
]
47+
]);
48+
Configure::write('Session', [
49+
'defaults' => 'php'
50+
]);
51+
Cache::config([
52+
'_cake_core_' => [
53+
'engine' => 'File',
54+
'prefix' => 'cake_core_',
55+
'serialize' => true
56+
],
57+
'_cake_model_' => [
58+
'engine' => 'File',
59+
'prefix' => 'cake_model_',
60+
'serialize' => true
61+
],
62+
'default' => [
63+
'engine' => 'File',
64+
'prefix' => 'default_',
65+
'serialize' => true
66+
]
67+
]);
68+
69+
// Ensure default test connection is defined
70+
if (!getenv('db_dsn')) {
71+
putenv('db_dsn=sqlite://127.0.0.0/' . TMP . 'cakephp-lazyload.sqlite');
72+
}
73+
74+
ConnectionManager::config('test', [
75+
'url' => getenv('db_dsn'),
76+
'timezone' => 'UTC',
77+
]);
78+
79+
Log::config([
80+
'debug' => [
81+
'engine' => 'Cake\Log\Engine\FileLog',
82+
'levels' => ['notice', 'info', 'debug'],
83+
'file' => 'debug',
84+
],
85+
'error' => [
86+
'engine' => 'Cake\Log\Engine\FileLog',
87+
'levels' => ['warning', 'error', 'critical', 'alert', 'emergency'],
88+
'file' => 'error',
89+
]
90+
]);
91+
92+
Plugin::load('JeremyHarris\\LazyLoad', ['path' => ROOT]);
+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
<?php
2+
namespace JeremyHarris\LazyLoad\TestApp\Model\Entity;
3+
4+
use JeremyHarris\LazyLoad\TestApp\Model\Entity\LazyLoadableEntity;
5+
6+
class Comment extends LazyLoadableEntity
7+
{
8+
9+
protected function _getAccessor()
10+
{
11+
return 'accessor';
12+
}
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
<?php
2+
namespace JeremyHarris\LazyLoad\TestApp\Model\Entity;
3+
4+
use Cake\ORM\Entity;
5+
use JeremyHarris\LazyLoad\ORM\LazyLoadEntityTrait;
6+
7+
class LazyLoadableEntity extends Entity
8+
{
9+
use LazyLoadEntityTrait;
10+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
<?php
2+
namespace JeremyHarris\LazyLoad\TestApp\Model\Table;
3+
4+
use Cake\ORM\Table;
5+
6+
class ArticlesTable extends Table
7+
{
8+
9+
}

0 commit comments

Comments
 (0)
Please sign in to comment.