From 2b04e9efc6d9611a04d8603b4433688d691d5050 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dipl=2E=20jur=2E=20Sascha=20M=C3=BCller=20=28Sacom=20EDV?= =?UTF-8?q?=29?= Date: Fri, 24 Apr 2020 11:46:59 +0200 Subject: [PATCH] refactored remaining codesniffer problems. --- src/Event/EventListener.php | 2 - src/Event/VersionListener.php | 22 +-- src/Model/Behavior/Version/VersionTrait.php | 3 - src/Model/Behavior/VersionBehavior.php | 41 ++---- tests/Fixture/ArticlesFixture.php | 2 +- tests/Fixture/ArticlesTagsFixture.php | 2 +- .../Model/Behavior/VersionBehaviorTest.php | 128 +++++++++--------- tests/TestCase/Model/Entity/Test.php | 33 +++++ 8 files changed, 121 insertions(+), 112 deletions(-) create mode 100644 tests/TestCase/Model/Entity/Test.php diff --git a/src/Event/EventListener.php b/src/Event/EventListener.php index ed112e0..5e4f66f 100644 --- a/src/Event/EventListener.php +++ b/src/Event/EventListener.php @@ -17,7 +17,6 @@ use Cake\Event\EventListenerInterface; use Cake\Event\EventManagerTrait; - /** * Class EventListener * @@ -66,7 +65,6 @@ public function execute() * Check whether or not a bake call is a certain type. * * @param string|array $type The type of file you want to check. - * * @return bool Whether or not the bake template is the type you are checking. */ public function isType($type) diff --git a/src/Event/VersionListener.php b/src/Event/VersionListener.php index 57edb70..97fb89e 100644 --- a/src/Event/VersionListener.php +++ b/src/Event/VersionListener.php @@ -15,7 +15,6 @@ use Cake\Datasource\ConnectionManager; use Cake\Event\Event; -use Cake\ORM\TableRegistry; use Cake\Utility\Hash; /** @@ -32,8 +31,7 @@ class VersionListener extends EventListener /** * Called before the entity template is rendered * - * @param Event $event An Event instance - * + * @param \Cake\Event\Event $event An Event instance * @return void */ public function beforeRenderEntity(Event $event) @@ -44,8 +42,7 @@ public function beforeRenderEntity(Event $event) /** * Called before the test case template is rendered * - * @param Event $event An Event instance - * + * @param \Cake\Event\Event $event An Event instance * @return void */ public function beforeRenderTestCase(Event $event) @@ -62,8 +59,7 @@ public function beforeRenderTestCase(Event $event) /** * Called before the table template is rendered * - * @param Event $event An Event instance - * + * @param \Cake\Event\Event $event An Event instance * @return void */ public function beforeRenderTable(Event $event) @@ -75,8 +71,7 @@ public function beforeRenderTable(Event $event) /** * Removes unnecessary associations * - * @param Event $event An Event instance - * + * @param \Cake\Event\Event $event An Event instance * @return void */ protected function fixVersionTables(Event $event) @@ -96,9 +91,8 @@ protected function fixVersionTables(Event $event) /** * Attaches the behavior and modifies associations as necessary * - * @param Event $event An Event instance + * @param \Cake\Event\Event $event An Event instance * @param string $tableSuffix a suffix for the primary table - * * @return bool true if modified, false otherwise */ protected function checkAssociation(Event $event, $tableSuffix) @@ -125,8 +119,7 @@ protected function checkAssociation(Event $event, $tableSuffix) /** * Removes unnecessary belongsTo associations * - * @param Event $event An Event instance - * + * @param \Cake\Event\Event $event An Event instance * @return array */ protected function modifyBelongsTo(Event $event) @@ -147,8 +140,7 @@ protected function modifyBelongsTo(Event $event) /** * Removes unnecessary rulesChecker entries * - * @param Event $event An Event instance - * + * @param \Cake\Event\Event $event An Event instance * @return array */ protected function modifyRulesChecker(Event $event) diff --git a/src/Model/Behavior/Version/VersionTrait.php b/src/Model/Behavior/Version/VersionTrait.php index a164fdb..dd380d5 100644 --- a/src/Model/Behavior/Version/VersionTrait.php +++ b/src/Model/Behavior/Version/VersionTrait.php @@ -13,7 +13,6 @@ namespace Josegonzalez\Version\Model\Behavior\Version; -use Cake\Collection\Collection; use Cake\ORM\TableRegistry; /** @@ -32,7 +31,6 @@ trait VersionTrait * * @param int $versionId The version number to retrieve * @param bool $reset If true, will re-retrieve the related version collection - * * @return \Cake\ORM\Entity|null */ public function version($versionId, $reset = false) @@ -49,7 +47,6 @@ public function version($versionId, $reset = false) * Retrieves the related versions for the current entity * * @param bool $reset If true, will re-retrieve the related version collection - * * @return \Cake\Collection\CollectionInterface */ public function versions($reset = false) diff --git a/src/Model/Behavior/VersionBehavior.php b/src/Model/Behavior/VersionBehavior.php index 1b63188..f1778c4 100644 --- a/src/Model/Behavior/VersionBehavior.php +++ b/src/Model/Behavior/VersionBehavior.php @@ -1,5 +1,6 @@ $table + 'table' => $table, ]; foreach ($this->fields() as $field) { @@ -121,7 +118,6 @@ public function setupFieldAssociations($table) * * @param string|null $field Field name for per-field association. * @param array $options Association options. - * * @return \Cake\ORM\Association */ public function versionAssociation($field = null, $options = []) @@ -135,7 +131,7 @@ public function versionAssociation($field = null, $options = []) 'className' => $this->_config['versionTable'], 'foreignKey' => $this->_config['foreignKey'], 'strategy' => 'subquery', - 'dependent' => true + 'dependent' => true, ]; if ($field) { @@ -144,14 +140,14 @@ public function versionAssociation($field = null, $options = []) $name . '.model' => $model, $name . '.field' => $field, ], - 'propertyName' => $field . '_version' + 'propertyName' => $field . '_version', ]; } else { $options += [ 'conditions' => [ - $name . '.model' => $model + $name . '.model' => $model, ], - 'propertyName' => '__version' + 'propertyName' => '__version', ]; } @@ -168,7 +164,6 @@ public function versionAssociation($field = null, $options = []) * @param \Cake\Event\Event $event The beforeSave event that was fired * @param \Cake\Datasource\EntityInterface $entity The entity that is going to be saved * @param \ArrayObject $options the options passed to the save method - * * @return void */ public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $options) @@ -214,9 +209,10 @@ public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $o } $new[$field] = new $entityClass( - $data, [ + $data, + [ 'useSetters' => false, - 'markNew' => true + 'markNew' => true, ] ); } @@ -232,7 +228,6 @@ public function beforeSave(Event $event, EntityInterface $entity, ArrayObject $o * * @param \Cake\Event\Event $event The beforeSave event that was fired * @param \Cake\Datasource\EntityInterface $entity The entity that is going to be saved - * * @return void */ public function afterSave(Event $event, EntityInterface $entity) @@ -245,7 +240,6 @@ public function afterSave(Event $event, EntityInterface $entity) * Return the last version id * * @param \Cake\Datasource\EntityInterface $entity Entity. - * * @return int */ public function getVersionId(EntityInterface $entity) @@ -266,7 +260,7 @@ public function getVersionId(EntityInterface $entity) ->select(['version_id']) ->where( [ - 'model' => $this->_config['referenceName'] + 'model' => $this->_config['referenceName'], ] + $extractedKey ) ->order(['id desc']) @@ -292,7 +286,6 @@ public function getVersionId(EntityInterface $entity) * * @param \Cake\ORM\Query $query The original query to modify * @param array $options Options - * * @return \Cake\ORM\Query */ public function findVersions(Query $query, array $options) @@ -328,7 +321,6 @@ public function findVersions(Query $query, array $options) * into each entity under the `_versions` key * * @param \Cake\Datasource\ResultSetInterface $results Results to modify. - * * @return \Cake\Collection\CollectionInterface */ public function groupVersions($results) @@ -345,12 +337,12 @@ function (EntityInterface $row) use ($property) { foreach ($grouped->combine('field', 'content', 'version_id') as $versionId => $keys) { $entityClass = $this->_table->getEntityClass(); $versionData = [ - $versionField => $versionId + $versionField => $versionId, ]; $keys = $this->convertFieldsToType($keys, 'toPHP'); - /* @var \Cake\Datasource\EntityInterface $versionRow */ + /** @var \Cake\Datasource\EntityInterface $versionRow */ $versionRow = $grouped->match(['version_id' => $versionId])->first(); foreach ($this->_config['additionalVersionFields'] as $mappedField => $field) { @@ -361,10 +353,11 @@ function (EntityInterface $row) use ($property) { } $version = new $entityClass( - $keys + $versionData, [ + $keys + $versionData, + [ 'markNew' => false, 'useSetters' => false, - 'markClean' => true + 'markClean' => true, ] ); $result[$versionId] = $version; @@ -384,7 +377,6 @@ function (EntityInterface $row) use ($property) { * Returns the versions of a specific entity. * * @param \Cake\Datasource\EntityInterface $entity Entity. - * * @return \Cake\Collection\CollectionInterface */ public function getVersions(EntityInterface $entity) @@ -429,7 +421,6 @@ protected function fields() * Returns an array with foreignKey value. * * @param \Cake\Datasource\EntityInterface $entity Entity. - * * @return array */ protected function extractForeignKey($entity) @@ -445,7 +436,6 @@ protected function extractForeignKey($entity) * Returns default version association name. * * @param string $field Field name. - * * @return string */ protected function associationName($field = null) @@ -482,7 +472,6 @@ protected function referenceName() * * @param array $fields Fields to convert * @param string $direction Direction (toPHP or toDatabase) - * * @return array */ protected function convertFieldsToType(array $fields, $direction) diff --git a/tests/Fixture/ArticlesFixture.php b/tests/Fixture/ArticlesFixture.php index f7fb59f..cc7139c 100644 --- a/tests/Fixture/ArticlesFixture.php +++ b/tests/Fixture/ArticlesFixture.php @@ -40,7 +40,7 @@ class ArticlesFixture extends TestFixture 'body' => 'text', 'published' => ['type' => 'string', 'length' => 1, 'default' => 'N'], 'settings' => ['type' => 'json', 'null' => true], - '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]] + '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['id']]], ]; /** diff --git a/tests/Fixture/ArticlesTagsFixture.php b/tests/Fixture/ArticlesTagsFixture.php index 33ba8ba..08443d8 100644 --- a/tests/Fixture/ArticlesTagsFixture.php +++ b/tests/Fixture/ArticlesTagsFixture.php @@ -38,7 +38,7 @@ class ArticlesTagsFixture extends TestFixture 'tag_id' => ['type' => 'integer'], 'version_id' => ['type' => 'integer', 'null' => true], 'sort_order' => ['type' => 'integer', 'default' => 1], - '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['article_id', 'tag_id']]] + '_constraints' => ['primary' => ['type' => 'primary', 'columns' => ['article_id', 'tag_id']]], ]; /** diff --git a/tests/TestCase/Model/Behavior/VersionBehaviorTest.php b/tests/TestCase/Model/Behavior/VersionBehaviorTest.php index a3c764c..7ea5fd1 100644 --- a/tests/TestCase/Model/Behavior/VersionBehaviorTest.php +++ b/tests/TestCase/Model/Behavior/VersionBehaviorTest.php @@ -13,35 +13,14 @@ namespace Josegonzalez\Version\Test\TestCase\Model\Behavior; -use Cake\Collection\Collection; -use Cake\Event\Event; use Cake\Event\EventManager; use Cake\Http\BaseApplication; -use Cake\I18n\I18n; -use Cake\ORM\Entity; use Cake\ORM\TableRegistry; use Cake\TestSuite\TestCase; use InvalidArgumentException; use Josegonzalez\Version\Model\Behavior\VersionBehavior; -use Josegonzalez\Version\Model\Behavior\Version\VersionTrait; use ReflectionObject; -/** - * Class TestEntity - * - * A test entity to test with. - * - * @category CakePHP-Plugin - * @package Josegonzalez\Version\Test\TestCase\Model\Behavior - * @author Jose Diaz-Gonzalez - * @license MIT License (https://github.com/josegonzalez/cakephp-version/blob/master/LICENSE.txt) - * @link https://github.com/josegonzalez/cakephp-version - */ -class TestEntity extends Entity -{ - use VersionTrait; -} - /** * Class VersionBehaviorTest * The tests for this package. @@ -71,14 +50,14 @@ class VersionBehaviorTest extends TestCase * Overwritten method to load plugins needed for the tests. * * @param array $plugins The plugins to load. - * * @return \Cake\Http\BaseApplication */ public function loadPlugins(array $plugins = []): BaseApplication { $plugins = [ - 'Josegonzalez/Version' + 'Josegonzalez/Version', ]; + return parent::loadPlugins($plugins); // TODO: Change the autogenerated stub } @@ -101,8 +80,9 @@ public function tearDown(): void public function testSaveNew() { $table = TableRegistry::getTableLocator()->get( - 'Articles', [ - 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Behavior\TestEntity' + 'Articles', + [ + 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Entity\Test', ] ); $table->addBehavior('Josegonzalez/Version.Version'); @@ -137,8 +117,9 @@ public function testSaveNew() public function testFindVersion() { $table = TableRegistry::getTableLocator()->get( - 'Articles', [ - 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Behavior\TestEntity' + 'Articles', + [ + 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Entity\Test', ] ); $table->addBehavior('Josegonzalez/Version.Version'); @@ -156,8 +137,9 @@ public function testFindVersion() public function testFindVersionX() { $table = TableRegistry::getTableLocator()->get( - 'Articles', [ - 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Behavior\TestEntity', + 'Articles', + [ + 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Entity\Test', ] ); $config = [ @@ -183,8 +165,9 @@ public function testFindVersionX() public function testFindVersions() { $table = TableRegistry::getTableLocator()->get( - 'Articles', [ - 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Behavior\TestEntity' + 'Articles', + [ + 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Entity\Test', ] ); $table->addBehavior('Josegonzalez/Version.Version'); @@ -227,8 +210,9 @@ public function testFindVersions() public function testSaveLimitFields() { $table = TableRegistry::getTableLocator()->get( - 'Articles', [ - 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Behavior\TestEntity' + 'Articles', + [ + 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Entity\Test', ] ); $table->addBehavior('Josegonzalez/Version.Version', ['fields' => 'title']); @@ -256,8 +240,9 @@ public function testSaveLimitFields() public function testSaveDirtyFields() { $table = TableRegistry::getTableLocator()->get( - 'Articles', [ - 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Behavior\TestEntity' + 'Articles', + [ + 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Entity\Test', ] ); $table->addBehavior('Josegonzalez/Version.Version', ['onlyDirty' => true]); @@ -286,8 +271,9 @@ public function testSaveDirtyFields() public function testFindVersionLimitFields() { $table = TableRegistry::getTableLocator()->get( - 'Articles', [ - 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Behavior\TestEntity' + 'Articles', + [ + 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Entity\Test', ] ); $table->addBehavior('Josegonzalez/Version.Version', ['fields' => 'title']); @@ -306,8 +292,9 @@ public function testFindVersionLimitFields() public function testSaveWithValidMetaData() { $table = TableRegistry::getTableLocator()->get( - 'Articles', [ - 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Behavior\TestEntity' + 'Articles', + [ + 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Entity\Test', ] ); $table->addBehavior('Josegonzalez/Version.Version'); @@ -346,8 +333,9 @@ function ($event) { public function testSaveWithInvalidMetaData() { $table = TableRegistry::getTableLocator()->get( - 'Articles', [ - 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Behavior\TestEntity' + 'Articles', + [ + 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Entity\Test', ] ); $table->addBehavior('Josegonzalez/Version.Version'); @@ -386,15 +374,17 @@ function ($event) { public function testFindWithCompositeKeys() { $table = TableRegistry::getTableLocator()->get( - 'ArticlesTags', [ - 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Behavior\TestEntity' + 'ArticlesTags', + [ + 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Entity\Test', ] ); $table->addBehavior( - 'Josegonzalez/Version.Version', [ + 'Josegonzalez/Version.Version', + [ 'fields' => 'sort_order', 'versionTable' => 'articles_tags_versions', - 'foreignKey' => ['article_id', 'tag_id'] + 'foreignKey' => ['article_id', 'tag_id'], ] ); @@ -411,15 +401,17 @@ public function testFindWithCompositeKeys() public function testSaveWithCompositeKeys() { $table = TableRegistry::getTableLocator()->get( - 'ArticlesTags', [ - 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Behavior\TestEntity' + 'ArticlesTags', + [ + 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Entity\Test', ] ); $table->addBehavior( - 'Josegonzalez/Version.Version', [ + 'Josegonzalez/Version.Version', + [ 'fields' => 'sort_order', 'versionTable' => 'articles_tags_versions', - 'foreignKey' => ['article_id', 'tag_id'] + 'foreignKey' => ['article_id', 'tag_id'], ] ); @@ -439,12 +431,14 @@ public function testSaveWithCompositeKeys() public function testGetAdditionalMetaData() { $table = TableRegistry::getTableLocator()->get( - 'Articles', [ - 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Behavior\TestEntity' + 'Articles', + [ + 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Entity\Test', ] ); $table->addBehavior( - 'Josegonzalez/Version.Version', [ + 'Josegonzalez/Version.Version', + [ 'versionTable' => 'versions_with_user', 'additionalVersionFields' => ['created', 'user_id'], ] @@ -467,8 +461,9 @@ public function testGetAdditionalMetaData() public function testAssociations() { $table = TableRegistry::getTableLocator()->get( - 'Articles', [ - 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Behavior\TestEntity' + 'Articles', + [ + 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Entity\Test', ] ); $table->addBehavior('Josegonzalez/Version.Version'); @@ -493,8 +488,9 @@ public function testGetVersionId() { // init test data $table = TableRegistry::getTableLocator()->get( - 'Articles', [ - 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Behavior\TestEntity', + 'Articles', + [ + 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Entity\Test', ] ); $table->addBehavior('Josegonzalez/Version.Version'); @@ -517,8 +513,9 @@ public function testGetVersionId() public function testSaveNonScalarType() { $table = TableRegistry::getTableLocator()->get( - 'Articles', [ - 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Behavior\TestEntity', + 'Articles', + [ + 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Entity\Test', ] ); $schema = $table->getSchema(); @@ -543,8 +540,9 @@ public function testSaveNonScalarType() public function testVersionConvertsType() { $table = TableRegistry::getTableLocator()->get( - 'Articles', [ - 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Behavior\TestEntity', + 'Articles', + [ + 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Entity\Test', ] ); $table->addBehavior('Josegonzalez/Version.Version'); @@ -562,8 +560,9 @@ public function testVersionConvertsType() public function testConvertFieldsToType() { $table = TableRegistry::getTableLocator()->get( - 'Articles', [ - 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Behavior\TestEntity', + 'Articles', + [ + 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Entity\Test', ] ); $schema = $table->getSchema(); @@ -610,8 +609,9 @@ public function testConvertFieldsToTypeInvalidDirection() $this->expectException(InvalidArgumentException::class); $table = TableRegistry::getTableLocator()->get( - 'Articles', [ - 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Behavior\TestEntity', + 'Articles', + [ + 'entityClass' => 'Josegonzalez\Version\Test\TestCase\Model\Entity\Test', ] ); $behavior = new VersionBehavior($table); diff --git a/tests/TestCase/Model/Entity/Test.php b/tests/TestCase/Model/Entity/Test.php new file mode 100644 index 0000000..9861a23 --- /dev/null +++ b/tests/TestCase/Model/Entity/Test.php @@ -0,0 +1,33 @@ + + * @license MIT License (https://github.com/josegonzalez/cakephp-version/blob/master/LICENSE.txt) + * @link https://github.com/josegonzalez/cakephp-version + */ + +namespace Josegonzalez\Version\Test\TestCase\Model\Entity; + +use Cake\ORM\Entity; +use Josegonzalez\Version\Model\Behavior\Version\VersionTrait; + +/** + * Class Test + * + * A test entity to test with. + * + * @category CakePHP-Plugin + * @package Josegonzalez\Version\Test\TestCase\Model\Behavior + * @author Jose Diaz-Gonzalez + * @license MIT License (https://github.com/josegonzalez/cakephp-version/blob/master/LICENSE.txt) + * @link https://github.com/josegonzalez/cakephp-version + */ +class Test extends Entity +{ + use VersionTrait; +}