Browse Source

Remove dead code in ORM.

These classes were undocumented, and aren't part of the current ORM
implementation.

Refs #5755
Mark Story 11 years ago
parent
commit
35a7038bd8

+ 0 - 189
src/ORM/EntityValidator.php

@@ -1,189 +0,0 @@
-<?php
-/**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link          http://cakephp.org CakePHP(tm) Project
- * @since         3.0.0
- * @license       http://www.opensource.org/licenses/mit-license.php MIT License
- */
-namespace Cake\ORM;
-
-use ArrayObject;
-use Cake\Datasource\EntityInterface;
-use Cake\ORM\Association;
-use Cake\ORM\Table;
-use Cake\Validation\ValidatableInterface;
-
-/**
- * Contains logic for validating entities and their associations.
- *
- * This class is generally used by the internals of the ORM. It
- * provides methods for traversing a set of entities and their associated
- * properties.
- */
-class EntityValidator
-{
-
-    /**
-     * The table instance this validator is for.
-     *
-     * @var \Cake\ORM\Table
-     */
-    protected $_table;
-
-    /**
-     * Constructor.
-     *
-     * @param \Cake\ORM\Table $table The table this validator is for
-     */
-    public function __construct(Table $table)
-    {
-        $this->_table = $table;
-    }
-
-    /**
-     * Build the map of property => association names.
-     *
-     * @param array $include The array of included associations.
-     * @return array
-     */
-    protected function _buildPropertyMap($include)
-    {
-        if (empty($include['associated'])) {
-            return [];
-        }
-
-        $map = [];
-        foreach ($include['associated'] as $key => $options) {
-            if (is_int($key) && is_scalar($options)) {
-                $key = $options;
-                $options = [];
-            }
-
-            $options += ['validate' => true, 'associated' => []];
-            $assoc = $this->_table->association($key);
-            if ($assoc) {
-                $map[$assoc->property()] = [
-                    'association' => $assoc,
-                    'options' => $options
-                ];
-            }
-        }
-        return $map;
-    }
-
-    /**
-     * Validates a single entity by getting the correct validator object from
-     * the table and traverses associations passed in $options to validate them
-     * as well.
-     *
-     * @param \Cake\Datasource\EntityInterface $entity The entity to be validated
-     * @param array|\ArrayObject $options options for validation, including an optional key of
-     *   associations to also be validated. This argument should use the same format as the $options
-     *   argument to \Cake\ORM\Table::save().
-     * @return bool true if all validations passed, false otherwise
-     */
-    public function one(EntityInterface $entity, $options = [])
-    {
-        $valid = true;
-        $types = [Association::ONE_TO_ONE, Association::MANY_TO_ONE];
-        $propertyMap = $this->_buildPropertyMap($options);
-        $options = new ArrayObject($options);
-
-        foreach ($propertyMap as $key => $assoc) {
-            $value = $entity->get($key);
-            $association = $assoc['association'];
-
-            if (!$value) {
-                continue;
-            }
-            $isOne = in_array($association->type(), $types);
-            if ($isOne && !($value instanceof EntityInterface)) {
-                $valid = false;
-                continue;
-            }
-
-            $validator = new self($association->target());
-            if ($isOne) {
-                $valid = $validator->one($value, $assoc['options']) && $valid;
-            } else {
-                $valid = $validator->many($value, $assoc['options']) && $valid;
-            }
-        }
-
-        if (!isset($options['validate'])) {
-            $options['validate'] = true;
-        }
-
-        if (!($entity instanceof ValidatableInterface)) {
-            return $valid;
-        }
-
-        return $this->_processValidation($entity, $options) && $valid;
-    }
-
-    /**
-     * Validates a list of entities by getting the correct validator for the related
-     * table and traverses associations passed in $include to validate them as well.
-     *
-     * If any of the entities in `$entities` does not implement `Cake\Datasource\EntityInterface`,
-     * it will be treated as an invalid result.
-     *
-     * @param array $entities List of entities to be validated
-     * @param array|\ArrayObject $options options for validation, including an optional key of
-     *   associations to also be validated. This argument should use the same format as the $options
-     *   argument to \Cake\ORM\Table::save().
-     * @return bool true if all validations passed, false otherwise
-     */
-    public function many(array $entities, $options = [])
-    {
-        $valid = true;
-        foreach ($entities as $entity) {
-            if (!($entity instanceof EntityInterface)) {
-                return false;
-            }
-            $valid = $this->one($entity, $options) && $valid;
-        }
-        return $valid;
-    }
-
-    /**
-     * Validates the $entity if the 'validate' key is not set to false in $options
-     * If not empty it will construct a default validation object or get one with
-     * the name passed in the key
-     *
-     * @param \Cake\ORM\Entity $entity The entity to validate
-     * @param \ArrayObject $options The option for processing validation
-     * @return bool true if the entity is valid, false otherwise
-     */
-    protected function _processValidation($entity, $options)
-    {
-        $type = is_string($options['validate']) ? $options['validate'] : 'default';
-        $validator = $this->_table->validator($type);
-        $pass = compact('entity', 'options', 'validator');
-        $event = $this->_table->dispatchEvent('Model.beforeValidate', $pass);
-
-        if ($event->isStopped()) {
-            return (bool)$event->result;
-        }
-
-        if (!count($validator)) {
-            return true;
-        }
-
-        $success = !$entity->validate($validator);
-
-        $event = $this->_table->dispatchEvent('Model.afterValidate', $pass);
-        if ($event->isStopped()) {
-            $success = (bool)$event->result;
-        }
-
-        return $success;
-    }
-}

+ 0 - 41
src/ORM/EntityValidatorTrait.php

@@ -1,41 +0,0 @@
-<?php
-/**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link          http://cakephp.org CakePHP(tm) Project
- * @since         3.0.0
- * @license       http://www.opensource.org/licenses/mit-license.php MIT License
- */
-namespace Cake\ORM;
-
-use Cake\Validation\Validator;
-
-/**
- * Contains a method that can be used to apply a validator to an entity's internal
- * properties. This trait can be used to satisfy the Cake\Validation\ValidatableInterface
- */
-trait EntityValidatorTrait
-{
-
-    /**
-     * Validates the internal properties using a validator object and returns any
-     * validation errors found.
-     *
-     * @param \Cake\Validation\Validator $validator The validator to use when validating the entity.
-     * @return array
-     */
-    public function validate(Validator $validator)
-    {
-        $data = $this->_properties;
-        $new = $this->isNew();
-        $validator->provider('entity', $this);
-        $this->errors($validator->errors($data, $new === null ? true : $new));
-        return $this->_errors;
-    }
-}

+ 0 - 53
tests/TestCase/ORM/EntityTest.php

@@ -19,7 +19,6 @@ use Cake\TestSuite\TestCase;
 use Cake\Validation\Validator;
 use TestApp\Model\Entity\Extending;
 use TestApp\Model\Entity\NonExtending;
-use TestApp\Model\Entity\ValidatableEntity;
 
 /**
  * Entity test case.
@@ -777,58 +776,6 @@ class EntityTest extends TestCase
     }
 
     /**
-     * Tests that missing fields will not be passed as null to the validator
-     *
-     * @return void
-     */
-    public function testValidateMissingFields()
-    {
-        $entity = $this->getMockBuilder('TestApp\Model\Entity\ValidatableEntity')
-            ->setMethods(['getSomething'])
-            ->disableOriginalConstructor()
-            ->getMock();
-        $entity->accessible('*', true);
-
-        $validator = $this->getMock('\Cake\Validation\Validator');
-        $entity->set('a', 'b');
-
-        $validator->expects($this->once())
-            ->method('provider')
-            ->with('entity', $entity);
-        $validator->expects($this->once())->method('errors')
-            ->with(['a' => 'b'], true)
-            ->will($this->returnValue(['a' => ['not valid']]));
-        $this->assertNotEmpty($entity->validate($validator));
-        $this->assertEquals(['a' => ['not valid']], $entity->errors());
-    }
-
-    /**
-     * Tests validate when the validator returns no errors
-     *
-     * @return void
-     */
-    public function testValidateSuccess()
-    {
-        $validator = $this->getMock('\Cake\Validation\Validator');
-        $data = [
-            'a' => 'b',
-            'cool' => false,
-            'something' => true
-        ];
-        $entity = new ValidatableEntity($data);
-        $entity->isNew(true);
-
-        $validator->expects($this->once())
-            ->method('provider')
-            ->with('entity', $entity);
-        $validator->expects($this->once())->method('errors')
-            ->with($data, true)
-            ->will($this->returnValue([]));
-        $this->assertEmpty($entity->validate($validator));
-        $this->assertEquals([], $entity->errors());
-    }
-
-    /**
      * Tests the errors method
      *
      * @return void

+ 0 - 391
tests/TestCase/ORM/EntityValidatorTest.php

@@ -1,391 +0,0 @@
-<?php
-/**
- * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
- * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- *
- * Licensed under The MIT License
- * For full copyright and license information, please see the LICENSE.txt
- * Redistributions of files must retain the above copyright notice.
- *
- * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
- * @link          http://cakephp.org CakePHP(tm) Project
- * @since         3.0.0
- * @license       http://www.opensource.org/licenses/mit-license.php MIT License
- */
-namespace Cake\Test\TestCase\ORM;
-
-use Cake\ORM\Entity;
-use Cake\ORM\EntityValidator;
-use Cake\ORM\Table;
-use Cake\ORM\TableRegistry;
-use Cake\TestSuite\TestCase;
-
-/**
- * EntityValidator test
- */
-class EntityValidatorTest extends TestCase
-{
-
-    /**
-     * setup
-     *
-     * @return void
-     */
-    public function setUp()
-    {
-        parent::setUp();
-        $articles = TableRegistry::get('Articles');
-        $users = TableRegistry::get('Users');
-        $articles->belongsTo('Users');
-        $articles->hasMany('Comments');
-
-        $comments = TableRegistry::get('Comments');
-        $comments->belongsTo('Articles');
-        $comments->belongsTo('Users');
-
-        $this->articles = $articles;
-        $this->comments = $comments;
-        $this->users = $users;
-    }
-
-    /**
-     * Teardown
-     *
-     * @return void
-     */
-    public function tearDown()
-    {
-        parent::tearDown();
-        TableRegistry::clear();
-        unset($this->articles, $this->comments, $this->users);
-    }
-
-    /**
-     * Test one() with successful validate
-     *
-     * @return void
-     */
-    public function testOneSuccess()
-    {
-        $entity = $this->getMock('TestApp\Model\Entity\ValidatableEntity', ['validate']);
-        $validator = $this->getMock('\Cake\Validation\Validator');
-        $this->articles->validator('default', $validator);
-        $entityValidator = new EntityValidator($this->articles);
-
-        $validator->expects($this->once())
-            ->method('count')
-            ->will($this->returnValue(1));
-        $entity->expects($this->once())
-            ->method('validate')
-            ->with($validator)
-            ->will($this->returnValue([]));
-
-        $this->assertTrue($entityValidator->one($entity));
-    }
-
-    /**
-     * Test one() with failing validate
-     *
-     * @return void
-     */
-    public function testOneFail()
-    {
-        $entity = $this->getMock('TestApp\Model\Entity\ValidatableEntity', ['validate']);
-        $validator = $this->getMock('\Cake\Validation\Validator');
-        $this->articles->validator('default', $validator);
-        $entityValidator = new EntityValidator($this->articles);
-
-        $validator->expects($this->once())
-            ->method('count')
-            ->will($this->returnValue(1));
-        $entity->expects($this->once())
-            ->method('validate')
-            ->with($validator)
-            ->will($this->returnValue(['one' => ['error']]));
-
-        $this->assertFalse($entityValidator->one($entity));
-    }
-
-    /**
-     * test one() with association data.
-     *
-     * @return void
-     */
-    public function testOneAssociationsSuccess()
-    {
-        $class = 'TestApp\Model\Entity\ValidatableEntity';
-        $article = $this->getMock($class, ['validate']);
-        $comment1 = $this->getMock($class, ['validate']);
-        $comment2 = $this->getMock($class, ['validate']);
-        $user = $this->getMock($class, ['validate']);
-        $article->set('comments', [$comment1, $comment2]);
-        $article->set('user', $user);
-
-        $validator1 = $this->getMock('\Cake\Validation\Validator');
-        $validator2 = $this->getMock('\Cake\Validation\Validator');
-        $validator3 = $this->getMock('\Cake\Validation\Validator');
-
-        $validator1->expects($this->once())
-            ->method('count')
-            ->will($this->returnValue(1));
-        $validator2->expects($this->exactly(2))
-            ->method('count')
-            ->will($this->returnValue(1));
-        $validator3->expects($this->once())
-            ->method('count')
-            ->will($this->returnValue(1));
-
-        $this->articles->validator('default', $validator1);
-        $this->comments->validator('default', $validator2);
-        $this->users->validator('default', $validator3);
-
-        $entityValidator = new EntityValidator($this->articles);
-
-        $article->expects($this->once())
-            ->method('validate')
-            ->with($validator1)
-            ->will($this->returnValue([]));
-
-        $comment1->expects($this->once())
-            ->method('validate')
-            ->with($validator2)
-            ->will($this->returnValue([]));
-
-        $comment2->expects($this->once())
-            ->method('validate')
-            ->with($validator2)
-            ->will($this->returnValue([]));
-
-        $user->expects($this->once())
-            ->method('validate')
-            ->with($validator3)
-            ->will($this->returnValue([]));
-
-        $options = ['associated' => ['Comments', 'Users']];
-        $this->assertTrue($entityValidator->one($article, $options));
-    }
-
-    /**
-     * test one() with associations that are not entities.
-     *
-     * This can happen when request data is not completely marshalled.
-     * incomplete associations should not cause warnings or fatal errors.
-     *
-     * @return void
-     */
-    public function testOneAssociationsNoEntities()
-    {
-        $class = 'TestApp\Model\Entity\ValidatableEntity';
-        $article = $this->getMock($class, ['validate']);
-        $comment1 = ['comment' => 'test'];
-        $comment2 = ['comment' => 'omg'];
-        $user = $this->getMock($class, ['validate']);
-        $article->set('comments', [$comment1, $comment2]);
-
-        $validator1 = $this->getMock('\Cake\Validation\Validator');
-        $validator2 = $this->getMock('\Cake\Validation\Validator');
-
-        $validator1->expects($this->once())
-            ->method('count')
-            ->will($this->returnValue(1));
-
-        // Should not be called as comments are not entities.
-        $validator2->expects($this->never())
-            ->method('count');
-
-        $this->articles->validator('default', $validator1);
-        $this->comments->validator('default', $validator2);
-
-        $entityValidator = new EntityValidator($this->articles);
-
-        $article->expects($this->once())
-            ->method('validate')
-            ->with($validator1)
-            ->will($this->returnValue([]));
-
-        $options = ['associated' => ['Comments']];
-        $this->assertFalse($entityValidator->one($article, $options));
-    }
-
-    /**
-     * test one() with association data and one of them failing  validation.
-     *
-     * @return void
-     */
-    public function testOneAssociationsFail()
-    {
-        $class = 'TestApp\Model\Entity\ValidatableEntity';
-        $article = $this->getMock($class, ['validate']);
-        $comment1 = $this->getMock($class, ['validate']);
-        $comment2 = $this->getMock($class, ['validate']);
-        $user = $this->getMock($class, ['validate']);
-        $article->set('comments', [$comment1, $comment2]);
-        $article->set('user', $user);
-
-        $validator1 = $this->getMock('\Cake\Validation\Validator');
-        $validator2 = $this->getMock('\Cake\Validation\Validator');
-        $validator3 = $this->getMock('\Cake\Validation\Validator');
-
-        $validator1->expects($this->once())
-            ->method('count')
-            ->will($this->returnValue(1));
-        $validator2->expects($this->exactly(2))
-            ->method('count')
-            ->will($this->returnValue(1));
-        $validator3->expects($this->once())
-            ->method('count')
-            ->will($this->returnValue(1));
-
-        $this->articles->validator('default', $validator1);
-        $this->comments->validator('default', $validator2);
-        $this->users->validator('default', $validator3);
-
-        $entityValidator = new EntityValidator($this->articles);
-
-        $article->expects($this->once())
-            ->method('validate')
-            ->with($validator1)
-            ->will($this->returnValue([]));
-
-        $comment1->expects($this->once())
-            ->method('validate')
-            ->with($validator2)
-            ->will($this->returnValue([]));
-
-        $comment2->expects($this->once())
-            ->method('validate')
-            ->with($validator2)
-            ->will($this->returnValue(['some' => ['error']]));
-
-        $user->expects($this->once())
-            ->method('validate')
-            ->with($validator3)
-            ->will($this->returnValue([]));
-
-        $options = ['associated' => ['Comments', 'Users']];
-        $this->assertFalse($entityValidator->one($article, $options));
-    }
-
-    /**
-     * Test one() with deeper associations and passing the name for custom
-     * validators
-     *
-     * @return void
-     */
-    public function testOneDeepAssociationsAndCustomValidators()
-    {
-        $class = 'TestApp\Model\Entity\ValidatableEntity';
-        $comment = $this->getMock($class, ['validate']);
-        $article = $this->getMock($class, ['validate']);
-        $user = $this->getMock($class, ['validate']);
-
-        $comment->set('article', $article);
-        $article->set('user', $user);
-
-        $validator1 = $this->getMock('\Cake\Validation\Validator');
-        $validator2 = $this->getMock('\Cake\Validation\Validator');
-        $validator3 = $this->getMock('\Cake\Validation\Validator');
-
-        $validator1->expects($this->once())
-            ->method('count')
-            ->will($this->returnValue(1));
-        $validator2->expects($this->once())
-            ->method('count')
-            ->will($this->returnValue(1));
-        $validator3->expects($this->once())
-            ->method('count')
-            ->will($this->returnValue(1));
-
-        $this->articles->validator('crazy', $validator1);
-        $this->comments->validator('default', $validator2);
-        $this->users->validator('funky', $validator3);
-
-        $entityValidator = new EntityValidator($this->comments);
-        $comment->expects($this->once())
-            ->method('validate')
-            ->with($validator2)
-            ->will($this->returnValue([]));
-
-        $article->expects($this->once())
-            ->method('validate')
-            ->with($validator1)
-            ->will($this->returnValue([]));
-
-        $user->expects($this->once())
-            ->method('validate')
-            ->with($validator3)
-            ->will($this->returnValue([]));
-
-        $this->assertTrue($entityValidator->one($comment, [
-            'associated' => [
-                'Articles' => [
-                    'validate' => 'crazy',
-                    'associated' => ['Users' => ['validate' => 'funky']]
-                ]
-            ]
-        ]));
-    }
-
-    /**
-     * Test many() with successful validate
-     *
-     * @return void
-     */
-    public function testManySuccess()
-    {
-        $class = 'TestApp\Model\Entity\ValidatableEntity';
-        $comment1 = $this->getMock($class, ['validate']);
-        $comment2 = $this->getMock($class, ['validate']);
-        $validator = $this->getMock('\Cake\Validation\Validator');
-        $data = [$comment1, $comment2];
-        $this->comments->validator('default', $validator);
-        $entityValidator = new EntityValidator($this->comments);
-
-        $validator->expects($this->exactly(2))
-            ->method('count')
-            ->will($this->returnValue(1));
-        $comment1->expects($this->once())
-            ->method('validate')
-            ->with($validator)
-            ->will($this->returnValue([]));
-
-        $comment2->expects($this->once())
-            ->method('validate')
-            ->with($validator)
-            ->will($this->returnValue([]));
-
-        $this->assertTrue($entityValidator->many($data));
-    }
-
-    /**
-     * Test many() with failure
-     *
-     * @return void
-     */
-    public function testManyFailure()
-    {
-        $class = 'TestApp\Model\Entity\ValidatableEntity';
-        $comment1 = $this->getMock($class, ['validate']);
-        $comment2 = $this->getMock($class, ['validate']);
-        $validator = $this->getMock('\Cake\Validation\Validator');
-        $data = [$comment1, $comment2];
-        $this->comments->validator('default', $validator);
-        $entityValidator = new EntityValidator($this->comments);
-
-        $validator->expects($this->exactly(2))
-            ->method('count')
-            ->will($this->returnValue(1));
-
-        $comment1->expects($this->once())
-            ->method('validate')
-            ->with($validator)
-            ->will($this->returnValue(['some' => ['error']]));
-
-        $comment2->expects($this->once())
-            ->method('validate')
-            ->with($validator)
-            ->will($this->returnValue([]));
-
-        $this->assertFalse($entityValidator->many($data));
-    }
-}

+ 0 - 17
tests/test_app/TestApp/Model/Entity/ValidatableEntity.php

@@ -1,17 +0,0 @@
-<?php
-
-namespace TestApp\Model\Entity;
-
-use Cake\ORM\Entity;
-use Cake\ORM\EntityValidatorTrait;
-use Cake\Validation\ValidatableInterface;
-
-/**
- * Tests entity class used for asserting correct loading
- *
- */
-class ValidatableEntity extends Entity implements ValidatableInterface
-{
-
-    use EntityValidatorTrait;
-}