Browse Source

Add useful error message when invalid associations are used.

When an invalid association is used in an existsIn rule no fatal error
should be raised.

Refs #7901
Mark Story 10 years ago
parent
commit
02a7a1c5ea
2 changed files with 34 additions and 2 deletions
  1. 11 2
      src/ORM/Rule/ExistsIn.php
  2. 23 0
      tests/TestCase/ORM/RulesCheckerIntegrationTest.php

+ 11 - 2
src/ORM/Rule/ExistsIn.php

@@ -16,6 +16,7 @@ namespace Cake\ORM\Rule;
 
 
 use Cake\Datasource\EntityInterface;
 use Cake\Datasource\EntityInterface;
 use Cake\ORM\Association;
 use Cake\ORM\Association;
+use RuntimeException;
 
 
 /**
 /**
  * Checks that the value provided in a field exists as the primary key of another
  * Checks that the value provided in a field exists as the primary key of another
@@ -57,16 +58,24 @@ class ExistsIn
      * @param \Cake\Datasource\EntityInterface $entity The entity from where to extract the fields
      * @param \Cake\Datasource\EntityInterface $entity The entity from where to extract the fields
      * @param array $options Options passed to the check,
      * @param array $options Options passed to the check,
      * where the `repository` key is required.
      * where the `repository` key is required.
+     * @throws \RuntimeException When the rule refers to an undefined association.
      * @return bool
      * @return bool
      */
      */
     public function __invoke(EntityInterface $entity, array $options)
     public function __invoke(EntityInterface $entity, array $options)
     {
     {
         if (is_string($this->_repository)) {
         if (is_string($this->_repository)) {
-            $this->_repository = $options['repository']->association($this->_repository);
+            $alias = $this->_repository;
+            $this->_repository = $options['repository']->association($alias);
+
+            if (empty($this->_repository)) {
+                throw new RuntimeException(sprintf(
+                    "ExistsIn rule for 'author_id' is invalid. The '%s' association is not defined.",
+                    $alias
+                ));
+            }
         }
         }
 
 
         $source = !empty($options['repository']) ? $options['repository'] : $this->_repository;
         $source = !empty($options['repository']) ? $options['repository'] : $this->_repository;
-
         $source = $source instanceof Association ? $source->source() : $source;
         $source = $source instanceof Association ? $source->source() : $source;
         $target = $this->_repository;
         $target = $this->_repository;
 
 

+ 23 - 0
tests/TestCase/ORM/RulesCheckerIntegrationTest.php

@@ -461,6 +461,29 @@ class RulesCheckerIntegrationTest extends TestCase
     }
     }
 
 
     /**
     /**
+     * Tests existsIn with invalid associations
+     *
+     * @group save
+     * @expectedException RuntimeException
+     * @expectedExceptionMessage ExistsIn rule for 'author_id' is invalid. The 'NotValid' association is not defined.
+     * @return void
+     */
+    public function testExistsInInvalidAssociation()
+    {
+        $entity = new Entity([
+            'title' => 'An Article',
+            'author_id' => 500
+        ]);
+
+        $table = TableRegistry::get('Articles');
+        $table->belongsTo('Authors');
+        $rules = $table->rulesChecker();
+        $rules->add($rules->existsIn('author_id', 'NotValid'));
+
+        $table->save($entity);
+    }
+
+    /**
      * Tests the checkRules save option
      * Tests the checkRules save option
      *
      *
      * @group save
      * @group save