Skip to content

Commit

Permalink
Solves issue #607 (#608)
Browse files Browse the repository at this point in the history
Fix exception with CakePHP 5.1

Closes #607
  • Loading branch information
MolbioUnige authored Sep 19, 2024
1 parent 324dc73 commit 90675b2
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
4 changes: 3 additions & 1 deletion src/Model/Behavior/UploadBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ public function initialize(array $config): void
$schema = $this->_table->getSchema();
/** @var string $field */
foreach (array_keys($this->getConfig()) as $field) {
$schema->setColumnType($field, 'upload.file');
if ($schema->hasColumn($field)) {
$schema->setColumnType($field, 'upload.file');
}
}
$this->_table->setSchema($schema);
}
Expand Down
18 changes: 15 additions & 3 deletions tests/TestCase/Model/Behavior/UploadBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,9 +68,13 @@ public function testInitialize()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$schema = $this->getMockBuilder('Cake\Database\Schema\TableSchema')
->onlyMethods(['setColumnType'])
->onlyMethods(['setColumnType', 'hasColumn'])
->disableOriginalConstructor()
->getMock();
$schema->expects($this->once())
->method('hasColumn')
->with('field')
->willReturn(true);
$schema->expects($this->once())
->method('setColumnType')
->with('field', 'upload.file');
Expand Down Expand Up @@ -110,9 +114,13 @@ public function testInitializeIndexedConfig()
$settings = ['field'];
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$schema = $this->getMockBuilder('Cake\Database\Schema\TableSchema')
->onlyMethods(['setColumnType'])
->onlyMethods(['setColumnType', 'hasColumn'])
->disableOriginalConstructor()
->getMock();
$schema->expects($this->once())
->method('hasColumn')
->with('field')
->willReturn(true);
$schema->expects($this->once())
->method('setColumnType')
->with('field', 'upload.file');
Expand Down Expand Up @@ -142,9 +150,13 @@ public function testInitializeAddBehaviorOptionsInterfaceConfig()
];
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$schema = $this->getMockBuilder('Cake\Database\Schema\TableSchema')
->onlyMethods(['setColumnType'])
->onlyMethods(['setColumnType', 'hasColumn'])
->disableOriginalConstructor()
->getMock();
$schema->expects($this->once())
->method('hasColumn')
->with('field')
->willReturn(true);
$schema->expects($this->once())
->method('setColumnType')
->with('field', 'upload.file');
Expand Down
1 change: 1 addition & 0 deletions tests/schema.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
'columns' => [
'id' => ['type' => 'integer'],
'filename' => ['type' => 'string'],
'field' => ['type' => 'string'],
'created' => ['type' => 'datetime', 'null' => true],
],
'constraints' => [
Expand Down

0 comments on commit 90675b2

Please sign in to comment.