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

Fixed a bug with string setters being executed as callables #35

Merged
merged 3 commits into from
Oct 16, 2018
Merged
Show file tree
Hide file tree
Changes from 2 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
2 changes: 1 addition & 1 deletion src/Model/Behavior/DuplicatableBehavior.php
Original file line number Diff line number Diff line change
Expand Up @@ -292,7 +292,7 @@ protected function _doAction($action, EntityInterface $entity, $prop, $value = n
break;

case 'set':
if (is_callable($value)) {
if (is_callable($value) && !is_string($value)) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Micro optimisation, the !is_string() check could be done first.

$value = $value($entity);
}
$entity->set($prop, $value);
Expand Down
22 changes: 22 additions & 0 deletions tests/TestCase/Model/Behavior/DuplicatableBehaviorTest.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<?php
namespace Duplicatable\Test\TestCase\Model\Behavior;

use Cake\Datasource\EntityInterface;
use Cake\ORM\TableRegistry;
use Cake\TestSuite\TestCase;

Expand Down Expand Up @@ -180,4 +181,25 @@ public function testWithTranslation()
->all();
$this->assertEquals(2, $records->count());
}

public function testDuplicateWithSetters()
Copy link
Collaborator

@ADmad ADmad Oct 15, 2018

Choose a reason for hiding this comment

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

This new test passes even without the change you have done to the behavior, so it's not really useful 🙂.

In general when fixing a bug write a failing test first and then fix the code.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

🤔 I'll have to revise this test.

{
$this->Invoices->removeBehavior('Duplicatable');
$this->Invoices->addBehavior('Duplicatable.Duplicatable', [
'set' => [
'copied' => true,
'name' => 'mail',
'contact_name' => function (EntityInterface $entity) {
return strrev($entity->get('contact_name'));
}
]
]);

$result = $this->Invoices->duplicate(1);
$invoice = $this->Invoices->get($result->id);

$this->assertEquals('mail', $invoice->name);
$this->assertEquals('eman tcatnoC', $invoice->contact_name);
$this->assertEquals(1, $invoice->copied);
}
}