Browse Source

Merge pull request #5784 from deizel/3.0

Allow configuring Table objects with Validator(s)
José Lorenzo Rodríguez 11 years ago
parent
commit
d200a96d1c
2 changed files with 62 additions and 1 deletions
  1. 20 1
      src/ORM/Table.php
  2. 42 0
      tests/TestCase/ORM/TableRegistryTest.php

+ 20 - 1
src/ORM/Table.php

@@ -121,6 +121,13 @@ class Table implements RepositoryInterface, EventListenerInterface
     use EventManagerTrait;
 
     /**
+     * Name of default validation set.
+     *
+     * @var string
+     */
+    const DEFAULT_VALIDATOR = 'default';
+
+    /**
      * Name of the table as it can be found in the database
      *
      * @var string
@@ -213,6 +220,9 @@ class Table implements RepositoryInterface, EventListenerInterface
      * - eventManager: An instance of an event manager to use for internal events
      * - behaviors: A BehaviorRegistry. Generally not used outside of tests.
      * - associations: An AssociationCollection instance.
+     * - validator: A Validator instance which is assigned as the "default"
+     *   validation set, or an associative array, where key is the name of the
+     *   validation set and value the Validator instance.
      *
      * @param array $config List of options for this table
      */
@@ -243,6 +253,15 @@ class Table implements RepositoryInterface, EventListenerInterface
         if (!empty($config['associations'])) {
             $associations = $config['associations'];
         }
+        if (!empty($config['validator'])) {
+            if (!is_array($config['validator'])) {
+                $this->validator(self::DEFAULT_VALIDATOR, $config['validator']);
+            } else {
+                foreach ($config['validator'] as $name => $validator) {
+                    $this->validator($name, $validator);
+                }
+            }
+        }
         $this->_eventManager = $eventManager ?: new EventManager();
         $this->_behaviors = $behaviors ?: new BehaviorRegistry($this);
         $this->_associations = $associations ?: new AssociationCollection();
@@ -1130,7 +1149,7 @@ class Table implements RepositoryInterface, EventListenerInterface
      *   use null to get a validator.
      * @return \Cake\Validation\Validator
      */
-    public function validator($name = 'default', Validator $validator = null)
+    public function validator($name = self::DEFAULT_VALIDATOR, Validator $validator = null)
     {
         if ($validator === null && isset($this->_validators[$name])) {
             return $this->_validators[$name];

+ 42 - 0
tests/TestCase/ORM/TableRegistryTest.php

@@ -20,6 +20,7 @@ use Cake\Datasource\ConnectionManager;
 use Cake\ORM\Table;
 use Cake\ORM\TableRegistry;
 use Cake\TestSuite\TestCase;
+use Cake\Validation\Validator;
 
 /**
  * Used to test correct class is instantiated when using TableRegistry::get();
@@ -320,6 +321,47 @@ class TableRegistryTest extends TestCase
         $this->assertEquals('users', $table->alias());
         $this->assertSame($connection, $table->connection());
         $this->assertEquals(array_keys($schema), $table->schema()->columns());
+        $this->assertEquals($schema['id']['type'], $table->schema()->column('id')['type']);
+    }
+
+    /**
+     * Tests that table options can be pre-configured with a single validator
+     *
+     * @return void
+     */
+    public function testConfigWithSingleValidator()
+    {
+        $validator = new Validator();
+
+        TableRegistry::config('users', ['validator' => $validator]);
+        $table = TableRegistry::get('users');
+
+        $this->assertSame($table->validator('default'), $validator);
+    }
+
+    /**
+     * Tests that table options can be pre-configured with multiple validators
+     *
+     * @return void
+     */
+    public function testConfigWithMultipleValidators()
+    {
+        $validator1 = new Validator();
+        $validator2 = new Validator();
+        $validator3 = new Validator();
+
+        TableRegistry::config('users', [
+            'validator' => [
+                'default' => $validator1,
+                'secondary' => $validator2,
+                'tertiary' => $validator3,
+            ]
+        ]);
+        $table = TableRegistry::get('users');
+
+        $this->assertSame($table->validator('default'), $validator1);
+        $this->assertSame($table->validator('secondary'), $validator2);
+        $this->assertSame($table->validator('tertiary'), $validator3);
     }
 
     /**