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

Adding an option builder for the save options #9143

Merged
merged 11 commits into from
Aug 18, 2016
Merged

Adding an option builder for the save options #9143

merged 11 commits into from
Aug 18, 2016

Conversation

burzum
Copy link
Contributor

@burzum burzum commented Jul 20, 2016

The option builder allows building the options through a chainable interface and does checks on some of the options to prevent mistakes like types in the associations. This helps the developer and ensures everything that is supposed to be saved gets really set up and saved right.

Florian Krämer added 4 commits July 19, 2016 16:08
This will check associations and validation and throw exceptions if something is not set up right. This helps to prevent typos in the options and gives clear error messages for the developer.
Also added more doc blocks and tests.
foreach ($array as $key => $value) {
$this->{$key}($value);
}
return $this;
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing blank line before return statement

@codecov-io
Copy link

codecov-io commented Jul 20, 2016

Current coverage is 94.90% (diff: 97.95%)

Merging #9143 into master will decrease coverage by 0.04%

@@             master      #9143   diff @@
==========================================
  Files           368        370     +2   
  Lines         27003      27084    +81   
  Methods        3233       3253    +20   
  Messages          0          0          
  Branches          0          0          
==========================================
+ Hits          25640      25704    +64   
- Misses         1363       1380    +17   
  Partials          0          0          

Powered by Codecov. Last update 2fd23df...16b1316

@lorenzo
Copy link
Member

lorenzo commented Jul 20, 2016

I'm not sure __call is the right thing for custom options. for example I have options like _auditLog, and I would not like to call $builder->_autditLog($arg) as it would look like I'm calling a protected method

@lorenzo lorenzo closed this Jul 20, 2016
@lorenzo lorenzo reopened this Jul 20, 2016
@burzum
Copy link
Contributor Author

burzum commented Jul 20, 2016

@lorenzo how about simply set($key, $value) then and checking that no implemented method of the same name as $key exists? If it exists we could call it.

return $this->{$option}($value);
}
$this->_options[$option] = $value;
return $this;
Copy link
Contributor

Choose a reason for hiding this comment

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

Missing blank line before return statement

{
$table = TableRegistry::get('Authors');
$result = $table->getSaveOptionsBuilder();
$this->assertInstanceOf('Cake\ORM\SaveOptionsBuilder', $result);
Copy link
Member

Choose a reason for hiding this comment

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

Could we have an integration test for the saveOptionBuilder() being used with save()?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@markstory I'm not sure how to test this better. Any recommendations?

    /**
     * Test that a save call takes a SaveOptionBuilder object as well.
     *
     * @group save
     * @return void
     */
    public function testSaveWithOptionBuilder()
    {
        $table = $this->getMockBuilder('\Cake\ORM\Table')
            ->setMethods(['_processSave'])
            ->getMock();

        $optionBuilder = new SaveOptionsBuilder($table, [
            'associated' => []
        ]);

        $entity = new \Cake\ORM\Entity(
            ['id' => 'foo'],
            ['markNew' => false, 'markClean' => true]
        );

        $this->assertSame($entity, $table->save($entity, $optionBuilder));
    }

Copy link
Member

Choose a reason for hiding this comment

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

That looks good, I wouldn't use any mocks though. Set some options into the builder and check that the record and associations perhaps get saved as you'd expect.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Is it now OK?

*/
public function validate($validate)
{
$this->_table->validator($validate);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this a side-effect?

Copy link
Member

Choose a reason for hiding this comment

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

Yes. This will mutate the table by building a validator and triggering the Model.buildValidator event if the validator hasn't already been built.

@markstory
Copy link
Member

PHPCS builds are still failing. They'll need to be fixed before this can be merged.

@lorenzo
Copy link
Member

lorenzo commented Aug 6, 2016

@burzum poke

@burzum
Copy link
Contributor Author

burzum commented Aug 8, 2016

@lorenzo ping :) I'm still alive, I've been just busy with tournament preparations and other things than programming in my free time recently.

@burzum
Copy link
Contributor Author

burzum commented Aug 14, 2016

@markstory @lorenzo is something missing?

@lorenzo
Copy link
Member

lorenzo commented Aug 18, 2016

Thanks burzum!

@lorenzo lorenzo merged commit 68d0b4a into cakephp:master Aug 18, 2016
@lorenzo
Copy link
Member

lorenzo commented Aug 18, 2016

@burzum any chance you can add docs for this?

@inoas
Copy link
Contributor

inoas commented Aug 18, 2016

This is very good stuff and all the option interfaces should move to setter methods or any other approach that is dependable / not-error prone. Thanks for doing it!

Edit: @ypnos-web well not as long as there are no atoms, symbols, tuples/pattern-matching. Maybe we can move to class constants as keys (no idea if any good).

@burzum
Copy link
Contributor Author

burzum commented Aug 18, 2016

@lorenzo when I find the time and mood to do so. Also I agree with @inoas. Maybe let's define an interface like \Cake\Core\OptionsInterface or an abstract class that will be used in the future (4.0) for all options passed to certain methods? I know this is a little more typing but I prefer to type a few chars more than ending up with wrong options because I had a typo in an array or having an option not checked. Also the default option values we have pretty often could be move into the options object itself. I had a nice debugging session because of that mistake and this was the reason for me to build this option builder. And since I'm in love with #c strict typed code feels more appealing to me. :)

@ypnos-web
Copy link
Contributor

Let's face it, option arrays are not proper OO design, they are hard to document, hard to debug, and especially error-prone.

@markstory markstory deleted the feature/entity-option-builder branch August 18, 2016 18:59
*
* @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @since 3.3.0
Copy link
Member

Choose a reason for hiding this comment

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

3.3.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

9 participants