Browse Source

Merge pull request #16739 from cakephp/5.x-table-initialize-schema

Remove unneeded method.
othercorey 3 years ago
parent
commit
5edd53bc0b
2 changed files with 11 additions and 48 deletions
  1. 3 29
      src/ORM/Table.php
  2. 8 19
      tests/TestCase/ORM/TableTest.php

+ 3 - 29
src/ORM/Table.php

@@ -515,11 +515,9 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
     public function getSchema(): TableSchemaInterface
     {
         if ($this->_schema === null) {
-            $this->_schema = $this->_initializeSchema(
-                $this->getConnection()
-                    ->getSchemaCollection()
-                    ->describe($this->getTable())
-            );
+            $this->_schema = $this->getConnection()
+                ->getSchemaCollection()
+                ->describe($this->getTable());
             if (Configure::read('debug')) {
                 $this->checkAliasLengths();
             }
@@ -595,30 +593,6 @@ class Table implements RepositoryInterface, EventListenerInterface, EventDispatc
     }
 
     /**
-     * Override this function in order to alter the schema used by this table.
-     * This function is only called after fetching the schema out of the database.
-     * If you wish to provide your own schema to this table without touching the
-     * database, you can override schema() or inject the definitions though that
-     * method.
-     *
-     * ### Example:
-     *
-     * ```
-     * protected function _initializeSchema(\Cake\Database\Schema\TableSchemaInterface $schema) {
-     *  $schema->setColumnType('preferences', 'json');
-     *  return $schema;
-     * }
-     * ```
-     *
-     * @param \Cake\Database\Schema\TableSchemaInterface $schema The table definition fetched from database.
-     * @return \Cake\Database\Schema\TableSchemaInterface the altered schema
-     */
-    protected function _initializeSchema(TableSchemaInterface $schema): TableSchemaInterface
-    {
-        return $schema;
-    }
-
-    /**
      * Test to see if a Table has a specific field/column.
      *
      * Delegates to the schema object and checks for column presence

+ 8 - 19
tests/TestCase/ORM/TableTest.php

@@ -512,28 +512,17 @@ class TableTest extends TestCase
         $this->assertNotNull($table->setSchema($schema));
     }
 
-    /**
-     * Tests that _initializeSchema can be used to alter the database schema
-     */
-    public function testSchemaInitialize(): void
+    public function testSchemaTypeOverrideInInitialize(): void
     {
-        $schema = $this->connection->getSchemaCollection()->describe('users');
-        $table = $this->getMockBuilder(Table::class)
-            ->onlyMethods(['_initializeSchema'])
-            ->setConstructorArgs([['table' => 'users', 'connection' => $this->connection]])
-            ->getMock();
-        $table->expects($this->once())
-            ->method('_initializeSchema')
-            ->with($schema)
-            ->will($this->returnCallback(function ($schema) {
-                $schema->setColumnType('username', 'integer');
+        $table = new class (['table' => 'users', 'connection' => $this->connection]) extends Table {
+            public function initialize(array $config): void
+            {
+                $this->getSchema()->setColumnType('username', 'foobar');
+            }
+        };
 
-                return $schema;
-            }));
         $result = $table->getSchema();
-        $schema->setColumnType('username', 'integer');
-        $this->assertEquals($schema, $result);
-        $this->assertEquals($schema, $table->getSchema(), '_initializeSchema should be called once');
+        $this->assertSame('foobar', $result->getColumnType('username'));
     }
 
     /**