Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Solves issue #607 #608

Merged
merged 4 commits into from
Sep 19, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
6 changes: 3 additions & 3 deletions tests/TestCase/Model/Behavior/UploadBehaviorTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public function testInitialize()
->onlyMethods(['setColumnType'])
->disableOriginalConstructor()
->getMock();
$schema->expects($this->once())
$schema->expects($this->any())
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of changing this add the field column to the schema.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you mean adding the field column in tests/schema.php? Doing this does nothing by itself, and test are failing:

  1. Josegonzalez\Upload\Test\TestCase\Model\Behavior\UploadBehaviorTest::testInitialize
    Expectation failed for method name is "setColumnType" when invoked 1 time.
    Method was expected to be called 1 time, actually called 0 times.

What I understand is that, as the schema is a mock, I have to mock the 'hasColumn' method as well, and return true.

$schema = $this->getMockBuilder('Cake\Database\Schema\TableSchema')
   ->onlyMethods(['setColumnType', 'hasColumn'])
   ->disableOriginalConstructor()
   ->getMock();
$schema->expects($this->once())
   ->method('hasColumn')
   ->with('field')
   ->willReturn(true);

The test then passes, but then I don't see the point of modifying the schema in that case, or is there something else that I'm missing?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes you'll need to add expects for hasColum() too.

We need to separately test for the two cases where the column is in the schema and when it's not and not use any() as it's less work :)

->method('setColumnType')
->with('field', 'upload.file');
$table->expects($this->any())
Expand Down Expand Up @@ -113,7 +113,7 @@ public function testInitializeIndexedConfig()
->onlyMethods(['setColumnType'])
->disableOriginalConstructor()
->getMock();
$schema->expects($this->once())
$schema->expects($this->any())
->method('setColumnType')
->with('field', 'upload.file');
$table->expects($this->any())
Expand Down Expand Up @@ -145,7 +145,7 @@ public function testInitializeAddBehaviorOptionsInterfaceConfig()
->onlyMethods(['setColumnType'])
->disableOriginalConstructor()
->getMock();
$schema->expects($this->once())
$schema->expects($this->any())
->method('setColumnType')
->with('field', 'upload.file');
$table->expects($this->any())
Expand Down
Loading