CakePHP3 testing behavior - how do I mock a query?
I have the following test for my behavior in tests\TestCase\Model\Behavior\SluggableBehaviorTest.php config = [ 'field' => 'title', 'slug' => 'slug', 'scope' => array(), 'separator' => '-', 'unique' => true, 'update' => false, 'trigger' => false ]; } /** * tearDown method * * @return void */ public function tearDown() { unset($this->Sluggable); parent::tearDown(); } /** * Test initial setup * * @return void */ public function testInitialization() { $this->markTestIncomplete('Not implemented yet.'); } /** * Data provider method for testing valid file uploads * * @return array */ public function slugEntityProvider() { return [ [ [], ['title' => 'Test', 'slug' => ''], ['title' => 'Test', 'slug' => 'test'] ], [ ['field' => 'other_title', 'slug' => 'other_slug'], ['other_title' => 'Test Name', 'other_slug' => ''], ['other_title' => 'Test Name', 'other_slug' => 'test-name'] ], [ ['separator' => '_'], ['title' => 'Test Name', 'slug' => ''], ['title' => 'Test Name', 'slug' => 'test_name'] ] ]; } /** * Test that correct field is slugged * * @dataProvider slugEntityProvider */ public function testSlug(array $configUpdates, array $entityData, array $expected) { $config = $this->config; foreach($configUpdates as $key => $value) { $config[$key] = $value; } $table = $this->getMock('Cake\ORM\Table', null); $Sluggable = $this->getMockBuilder( 'App\Model\Behavior\SluggableBehavior') ->setConstructorArgs([$table, $config]) ->setMethods(['makeUniqueSlug']) ->getMock(); $Sluggable->expects($this->once()) ->method('makeUniqueSlug') ->will($this->returnValue($expected[$config['slug']])); $entity = new Entity($entityData); $Sluggable->slug($entity); $this->assertEquals($expected[$config['slug']], $entity[$config[ 'slug']]); } /** * Data provider method for testing valid file uploads * * @return array */ public function beforeSaveEntityProvider() { return [ [ ['update' => false], ['slug' => 1], ['title' => 'Test Name', 'slug' => ''], ['title' => 'Test Name', 'slug' => 'test-name'] ], [ ['update' => false], ['slug' => 0], ['id' => 1, 'title' => 'Test Name', 'slug' => 'test'], ['id' => 1, 'title' => 'Test Name', 'slug' => 'test'] ], [ ['update' => true], ['slug' => 1], ['id' => 1, 'title' => 'Test Name', 'slug' => 'test'], ['id' => 1, 'title' => 'Test Name', 'slug' => 'test-name'] ], [ ['trigger' => false], ['slug' => 1], ['title' => 'Test Name', 'slug' => '', 'trigger' => false], ['title' => 'Test Name', 'slug' => 'test', 'trigger' => false] ], [ ['trigger' => 'use_slug'], ['slug' => 0], ['title' => 'Test Name', 'slug' => '', 'use_slug' => false], ['title' => 'Test Name', 'slug' => '', 'use_slug' => false] ] ]; } /** * Test that field is only slugged when config specifies * * @dataProvider beforeSaveEntityProvider */ public function testBeforeSave(array $configUpdates, array $methodCalls, array $entityData, array $expected) { $config = $this->config; foreach($configUpdates as $key => $value) { $config[$key] = $value; } $table = $this->getMock('Cake\ORM\Table', null); $Sluggable = $this->getMockBuilder( 'App\Model\Behavior\SluggableBehavior') ->setConstructorArgs([$table, $config]) ->setMethods(['slug']) ->getMock(); if ($methodCalls['slug'] == 1) { $Sluggable->expects($this->once()) ->method('slug') ->will($this->returnValue($expected[$config['slug']])); } elseif ($methodCalls['slug'] == 0) { $Sluggable->expects($this->never()) ->method('slug') ->w
Re: [Noob!] Acl Tutorial Question
Do you have a record in your aro table with a value of "Group" in the model field and "1" in the foreign key field? -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
Re: Fat Model Skinny Controlller? Is this really correct?
I'm certainly no expert but my understanding is that the placing as much logic in the model as possible is the best way as many controllers can use the same model and often many controller methods save/edit/delete model data. For example, if you want to store a serializaed array into the db, if the logic to serialize the array is placed into the controller you would have to serialize the value everywhere that the model is saved. If you have ten functions in the controller that save the model then you would have to repeat the code ten times. However, if the code to serialize the array is placed in the model instead you only have to write the code once and can prety much forget about it from then on. Also when you come to refactor your code you only have to change the code in one place, not in every controller using the model It adhears to the DRY (Don't Repeat Yourself) principle and will make your life easier and hopefully less buggy. At least that is my understanding. -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
unit tests Cakephp 2.0 using Router::parse
I am trying to built a test for a helper method which uses Router:parse() The method works fine in the app but when I debug(Router::parse($url) in the test case it always returns an empty array. How do I initialize the Router class in a test helper case and which properties would I need to set. I am very new to testing and have found the learning curve steep. My knowledge of oop is also basic at best. Please help. Thanks. -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
Accessing $this->request in Model Cakephp 2.0 Unit Testing
I am trying to build a test for a custom validation in Cakephp 2.0 in the AppModel The problem is that the validation method uses values from $this->request to compare different fields, i.e check that password == comfirm_password. How do I create a test for this, i.e how do I set the value of $this->request? -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
Re: ckaephp 2.0 using Configure::read in model construct method
Thanks for you help but I am not sure that that is correct. Whilst I by no means pretend to be an expert at the inner workings of cakephp, the construct function in UserModel is called as the user model is first constructed, The parent::__construct() function in Model actually initializes the behaviours so my theory was that if I change the value of actsAs prior to parent::__construct() then I should be able to change the settings of the behavior. Also if I simply echo the value of configure::read(..) then the correct value is printed to the screen showing that the configure values have actually been set when the UserModel __construct() function is called. This at least makes sense to me, whether I am right or not is a completely different matter. -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
Re: ckaephp 2.0 using Configure::read in model construct method
Thanks. I have tried this and I still get the same issue unfortunately. any ideas of where to start looking if it is a problem with my setup? On Sep 9, 10:23 am, Thomas Ploch wrote: > Hi, since you are setting Model::actsAs before the parent Model is > initialized this is doomed to fail. > > Try the following: > > function __construct($id = false, $table = null, $ds = null) { > parent::__construct($id, $table, $ds); > $this->actsAs['Revision']['limit'] > =Configure::read('Setting.System.revision_limit'); > > } > > Kind regards > Thomas > > Am 09.09.2011 09:47, schrieb badben: > > > > > > > > > I am building a new application in cakephp 2.0 and am struggling using > > the configure function. > > > I have a function in AppModel beforeFilter that load the settings from > > db into Configure using configure::write. I also have the following > > in my User Model: > > > function __construct($id = false, $table = null, $ds = null) { > > $test = > > (integer)Configure::read('Setting.System.revision_limit'); > > > $this->actsAs['Revision']['limit'] = $test; > > echo Configure::read('Setting.System.revision_limit'); > > parent::__construct($id = false, $table = null, $ds = null); > > } > > > The problem is that $this->actsAs['Revision']['limit'] is set to null > > even though when I echo > > Configure::read('Setting.System.revision_limit'); it returns 20. > > > However, if I change the __construct function to: > > > function __construct($id = false, $table = null, $ds = null) { > > $test = 20; > > > $this->actsAs['Revision']['limit'] = $test; > > //echo Configure::read('Setting.System.revision_limit'); > > parent::__construct($id = false, $table = null, $ds = null); > > } > > > it seems to work but of course this will not help me. > > > Does anybody know where I have gone wrong? > > > Thanks -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
ckaephp 2.0 using Configure::read in model construct method
I am building a new application in cakephp 2.0 and am struggling using the configure function. I have a function in AppModel beforeFilter that load the settings from db into Configure using configure::write. I also have the following in my User Model: function __construct($id = false, $table = null, $ds = null) { $test = (integer)Configure::read('Setting.System.revision_limit'); $this->actsAs['Revision']['limit'] = $test; echo Configure::read('Setting.System.revision_limit'); parent::__construct($id = false, $table = null, $ds = null); } The problem is that $this->actsAs['Revision']['limit'] is set to null even though when I echo Configure::read('Setting.System.revision_limit'); it returns 20. However, if I change the __construct function to: function __construct($id = false, $table = null, $ds = null) { $test = 20; $this->actsAs['Revision']['limit'] = $test; //echo Configure::read('Setting.System.revision_limit'); parent::__construct($id = false, $table = null, $ds = null); } it seems to work but of course this will not help me. Does anybody know where I have gone wrong? Thanks -- Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions. To unsubscribe from this group, send email to cake-php+unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php