Skip to content

Commit

Permalink
Merge pull request #24 from KainGNX/Cake4.x-prop-compatibility
Browse files Browse the repository at this point in the history
Fixed compatibility with cakephp/orm 4.x
  • Loading branch information
jeremyharris authored Feb 20, 2020
2 parents 9c4298a + 202bce1 commit 32c6e73
Show file tree
Hide file tree
Showing 6 changed files with 39 additions and 41 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
/vendor
/composer.lock
/phpunit.xml
/.phpunit.result.cache
/.vscode
37 changes: 16 additions & 21 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,39 +1,34 @@
language: php

services:
- postgresql
- mysql

php:
- 5.6
- 7.0
- 7.1
- 7.2
- 7.3
- 7.4

sudo: false

env:
- DB=mysql db_dsn='mysql://travis@0.0.0.0/cakephp_test'
- DB=pgsql db_dsn='postgres://travis@127.0.0.1/cakephp_test'
- DB=sqlite
matrix:
- DB=mysql DB_DSN='mysql://root@127.0.0.1/cakephp_test'
- DB=pgsql DB_DSN='postgres://postgres@127.0.0.1/cakephp_test'
- DB=sqlite DB_DSN='sqlite:///:memory:'

matrix:
fast_finish: true
include:
- php: 7.0
env: COVERAGE=1

before_install:
- if [[ $DB == 'mysql' ]]; then mysql -u root -e 'CREATE DATABASE cakephp_test;'; fi
- if [[ $DB == 'pgsql' ]]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi

install:
- composer self-update
- composer install --prefer-dist --no-interaction

before_script:
- if [[ $DB == 'mysql' ]]; then mysql -e 'CREATE DATABASE cakephp_test;'; fi
- if [[ $DB == 'pgsql' ]]; then psql -c 'CREATE DATABASE cakephp_test;' -U postgres; fi
- phpenv rehash
- set +H

script:
- if [[ $COVERAGE == 1 ]]; then phpdbg -qrr vendor/bin/phpunit --coverage-clover=coverage.xml; fi
- if [[ $COVERAGE != 1 ]]; then vendor/bin/phpunit; fi

after_success:
- if [[ $COVERAGE == 1 ]]; then bash <(curl -s https://codecov.io/bash); fi
- vendor/bin/phpunit

notifications:
email: false
6 changes: 3 additions & 3 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,12 @@
"cakephp", "plugin", "lazy load"
],
"require-dev": {
"phpunit/phpunit": "^5.7|^6.0",
"phpunit/phpunit": "^8.5",
"cakephp/cakephp-codesniffer": "dev-master",
"cakephp/cakephp": ">=3.6.0 <4.0"
"cakephp/cakephp": "^4.0"
},
"require": {
"cakephp/orm": ">=3.6.0 <4.0"
"cakephp/orm": "^4.0"
},
"autoload": {
"psr-4": {
Expand Down
1 change: 0 additions & 1 deletion phpunit.xml.dist
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
colors="true"
processIsolation="false"
stopOnFailure="false"
syntaxCheck="false"
bootstrap="./tests/bootstrap.php"
>

Expand Down
12 changes: 6 additions & 6 deletions src/ORM/LazyLoadEntityTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ protected function &_parentGet($property)
* @param string|array $property Property
* @return bool
*/
public function has($property)
public function has($property): bool
{
foreach ((array)$property as $prop) {
$has = $this->_parentHas($prop);
Expand Down Expand Up @@ -115,8 +115,8 @@ protected function _lazyLoad($property)
}

// check if the property was set as null to begin with
if (array_key_exists($property, $this->_properties)) {
return $this->_properties[$property];
if (array_key_exists($property, $this->_fields)) {
return $this->_fields[$property];
}

$repository = $this->_repository($property);
Expand All @@ -135,11 +135,11 @@ protected function _lazyLoad($property)
$repository->loadInto($this, [$association->getName()]);

// check if the association didn't exist and therefore didn't load
if (!isset($this->_properties[$property])) {
if (!isset($this->_fields[$property])) {
return null;
}

return $this->_properties[$property];
return $this->_fields[$property];
}

/**
Expand All @@ -150,7 +150,7 @@ protected function _lazyLoad($property)
protected function _repository()
{
$source = $this->getSource();
if ($source === null) {
if (empty($source)) {
list(, $class) = namespaceSplit(get_class($this));
$source = Inflector::pluralize($class);
}
Expand Down
22 changes: 12 additions & 10 deletions tests/TestCase/ORM/LazyLoadEntityTraitTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
use Cake\Datasource\EntityInterface;
use Cake\ORM\Entity;
use Cake\ORM\Locator\LocatorAwareTrait;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;
use JeremyHarris\LazyLoad\TestApp\Model\Entity\Comment;
use JeremyHarris\LazyLoad\TestApp\Model\Entity\LazyLoadableEntity;
Expand All @@ -27,20 +26,20 @@ class LazyLoadEntityTraitTest extends TestCase
* @var array
*/
public $fixtures = [
'plugin.JeremyHarris\LazyLoad.articles',
'plugin.JeremyHarris\LazyLoad.articles_tags',
'plugin.JeremyHarris\LazyLoad.authors',
'plugin.JeremyHarris\LazyLoad.comments',
'plugin.JeremyHarris\LazyLoad.tags',
'plugin.JeremyHarris\LazyLoad.users',
'plugin.JeremyHarris\LazyLoad.Articles',
'plugin.JeremyHarris\LazyLoad.ArticlesTags',
'plugin.JeremyHarris\LazyLoad.Authors',
'plugin.JeremyHarris\LazyLoad.Comments',
'plugin.JeremyHarris\LazyLoad.Tags',
'plugin.JeremyHarris\LazyLoad.Users',
];

/**
* setUp
*
* @return void
*/
public function setUp()
public function setUp(): void
{
parent::setUp();

Expand Down Expand Up @@ -259,7 +258,7 @@ public function testEntityMethodGet()
{
$article = $this->Articles->get(1);
$comments = $article->comments;
$this->assertInternalType('array', $comments);
$this->assertIsArray($comments);
$this->assertCount(4, $comments);
$this->assertInstanceOf(\Cake\Datasource\EntityInterface::class, $comments[0]);
}
Expand Down Expand Up @@ -358,7 +357,10 @@ public function testHasArray()
*/
public function testDontInterfereWithContain()
{
$this->Articles = $this->getMockForModel('Articles', ['_lazyLoad'], ['table' => 'articles']);
$this->Articles = $this->getMockBuilder(ArticlesTable::class)
->addMethods(['_lazyLoad'])
->setConstructorArgs([['table' => 'articles']])
->getMock();
$this->Articles->belongsTo('Authors');

$this->Articles
Expand Down

0 comments on commit 32c6e73

Please sign in to comment.