Skip to content

Commit

Permalink
Simplified as spotted
Browse files Browse the repository at this point in the history
  • Loading branch information
MolbioUnige committed Sep 18, 2024
1 parent bd75476 commit 49f237d
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 28 deletions.
19 changes: 3 additions & 16 deletions src/Model/Behavior/UploadBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
use Cake\Datasource\EntityInterface;
use Cake\Event\EventInterface;
use Cake\ORM\Behavior;
use Cake\ORM\Query\SelectQuery;
use Cake\Utility\Hash;
use Josegonzalez\Upload\File\Path\DefaultProcessor;
use Josegonzalez\Upload\File\Path\ProcessorInterface;
Expand Down Expand Up @@ -56,7 +55,9 @@ public function initialize(array $config): void
$schema = $this->_table->getSchema();
/** @var string $field */
foreach (array_keys($this->getConfig()) as $field) {
$schema->addColumn($field, ['type' => 'upload.file']);
if ($schema->hasColumn($field)) {
$schema->setColumnType($field, 'upload.file');
}
}
$this->_table->setSchema($schema);
}
Expand Down Expand Up @@ -93,20 +94,6 @@ public function beforeMarshal(EventInterface $event, ArrayObject $data, ArrayObj
}
}

/**
* Prevents virtual fields from being added to the query
*
* @param \Cake\Event\EventInterface $event
* @param \Cake\ORM\Query\SelectQuery $query
* @param \ArrayObject $options
* @param bool $primary
* @return void
*/
public function beforeFind(EventInterface $event, SelectQuery $query, ArrayObject $options, bool $primary): void
{
$query->selectAllExcept($this->_table, array_map(fn ($f) => (string)$f, array_keys($this->getConfig())));
}

/**
* Modifies the entity before it is saved so that uploaded file data is persisted
* in the database too.
Expand Down
24 changes: 12 additions & 12 deletions tests/TestCase/Model/Behavior/UploadBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -68,12 +68,12 @@ public function testInitialize()
{
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$schema = $this->getMockBuilder('Cake\Database\Schema\TableSchema')
->onlyMethods(['addColumn'])
->onlyMethods(['setColumnType'])
->disableOriginalConstructor()
->getMock();
$schema->expects($this->once())
->method('addColumn')
->with('field', ['type' => 'upload.file']);
$schema->expects($this->any())
->method('setColumnType')
->with('field', 'upload.file');
$table->expects($this->any())
->method('getSchema')
->will($this->returnValue($schema));
Expand Down Expand Up @@ -110,12 +110,12 @@ public function testInitializeIndexedConfig()
$settings = ['field'];
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$schema = $this->getMockBuilder('Cake\Database\Schema\TableSchema')
->onlyMethods(['addColumn'])
->onlyMethods(['setColumnType'])
->disableOriginalConstructor()
->getMock();
$schema->expects($this->once())
->method('addColumn')
->with('field', ['type' => 'upload.file']);
$schema->expects($this->any())
->method('setColumnType')
->with('field', 'upload.file');
$table->expects($this->any())
->method('getSchema')
->will($this->returnValue($schema));
Expand All @@ -142,12 +142,12 @@ public function testInitializeAddBehaviorOptionsInterfaceConfig()
];
$table = $this->getMockBuilder('Cake\ORM\Table')->getMock();
$schema = $this->getMockBuilder('Cake\Database\Schema\TableSchema')
->onlyMethods(['addColumn'])
->onlyMethods(['setColumnType'])
->disableOriginalConstructor()
->getMock();
$schema->expects($this->once())
->method('addColumn')
->with('field', ['type' => 'upload.file']);
$schema->expects($this->any())
->method('setColumnType')
->with('field', 'upload.file');
$table->expects($this->any())
->method('getSchema')
->will($this->returnValue($schema));
Expand Down

0 comments on commit 49f237d

Please sign in to comment.