I am developing a test for a controller function and basically it just acts upon a cake request, is there anyway to mock cake request inside the test function so that whenever the controller tries to access $this->request->data it returns the data i have set in the test case? if there is a way please tell me how.
Regards
The documentation contains an example of how to set the request data. For quick reference:
public function testIndexPostData() {
$data = array(
'Article' => array(
'user_id' => 1,
'published' => 1,
'slug' => 'new-article',
'title' => 'New Article',
'body' => 'New Body'
)
);
$result = $this->testAction(
'/articles/index',
array('data' => $data, 'method' => 'post')
);
debug($result);
}
Related
I am trying to use query string structure in drupal services API. it's not working for me.
I have also search most of the solutions, but all failed.
here is m drupal code:
function rapid_services_resources() {
$resources = array(
'get_data' => array(
'operations' => array(
'retrieve' => array(
'help' => t('Gets user email of uid passed.'),
'callback' => 'my_module_get_user_email',
'args' => array(
array(
'name' => 'nid',
'type' => 'int',
'description' => 'The display ID of the view to get.',
'source' => array('param' => 'nid'),
'optional' => TRUE,
'default value' => 'default',
),
),
'access arguments' => '_blog_access_provide_access',
'access callback' => '_blog_access_provide_access',
//~ 'access arguments append' => FALSE,
),
),
),
);
return $resources;
}
function _blog_access_provide_access() {
return TRUE;
}
function my_module_get_user_email($nid){
var_dump($args);die;
}
I want url like this.:
http://localhost/drupaltest/rapidapi/get_data/?nid=111
Please let me know where i did wrong.
thanks in advance.
Hi here are to reference that will be useful
http://pingv.com/blog/an-introduction-drupal-7-restful-services
https://www.drupal.org/node/783460
Also I am not sure about the query string, but for the retrieve operation you can set it as part of the url
ex:
http://localhost/drupaltest/rapidapi/get_data/<nid should be here>
http://localhost/drupaltest/rapidapi/get_data/111
Also using the source path as source
'source' => array('path' => '0'),
I would think that source param only works for an index operation and not retrieve
I have the following in one of my routes
$rules = array(
'name' => 'Required',
'subject' => 'Required',
'message' => 'Required',
'email' => 'Required|Email',
'recaptcha_response_field' => 'required|recaptcha'
);
$validator = Validator::make(Input::all(), $rules);
if($validator->fails()){
return Redirect::to('contact')->withInput()->withErrors($validator);
}else{
$data = array('name' => Input::get('name'),
'email' => Input::get('email'),
'text' => Input::get('message'),
'subject' => Input::get('subject'));
Queue::push('ContactQueue', $data);
return Redirect::to('contact')->with('success', 'Message sent successfully');
}
I am trying to write a unit test for the success scenario, I have the following:
public function testSuccess(){
Validator::shouldReceive('make')->once()->andReturn(Mockery::mock(['fails' => false]));
Queue::shouldReceive('push')->once();
$this->call('POST', '/contact');
$this->assertRedirectedTo('/contact');
}
But I keep receiving the following error when trying to run phpunit:
BadMethodCallException: Method Illuminate\Queue\QueueManager::connected() does not exist on this mock object
Any ideas?
Putting Queue::shouldReceive('connected')->once();
after Queue::shouldReceive('push')->once();
solved this.
I have a problem validating if my user checked at least one option from a list of checkboxes.
Here is what i tried:
My view looks like this:
echo $this->Form->input('market_segment_targeted', array(
'multiple' => 'checkbox',
'label'=>array('text' => 'Market segment targeted', 'class'=>'w120'),
'options' => array(
'Home users' => 'Home users',
'SOHO' => 'SOHO',
'SMB' => 'SMB',
'Enterprise' => 'Enterprise'
),
));
In my controller i have added this snippet of code:
$validate_on_fly = array(
'market_segment_targeted' => array(
'notEmpty' => array(
'rule' => array('multiple', array('min' => 1)),
'required' => true,
'message' => 'Please select at least one!'
))
)));
$this->Partner->validate = Set::merge(
$this->Partner->validate,
$validate_on_fly
);
Any ideas what am i doing wrong?
Thank you
In CakePHP you can use Model Validation for checkboxes. Here is a quick example.
Your Form can look like:
$this->Form->create('User');
$this->Form->input('User.agree', array('type'=>'checkbox', 'hiddenField'=>false, 'value'=>'0'));
$this->Form->submit('Save'):
$this->Form->end();
Then in your Model under public $validate, use:
'agree'=>array(
'Not empty'=>array(
'rule'=>array('comparison', '!=', 0),
'required'=>true,
'message'=>'You must agree to the ToS'
)
)
I am having troubles to test one of my methods because it uses a vendor class with no model.
Well, the thing is that i want that method to return me what i want.
I have been told that mocking a method is to make it return what i want.
For example, when i call "foo()" method i want it to return me true always.
How can i do it? At CakePHP cookbook i can find this:
$Posts = $this->generate('Posts', array(
'methods' => array(
'isAuthorized'
),
'models' => array(
'Post' => array('save')
),
'components' => array(
'RequestHandler' => array('isPut'),
'Email' => array('send'),
'Session'
)
));
So i guess i should have to use the fist option: method
But... how to make it return what i want?
Thanks.
Refer to the answer I gave you in this question: How can i test an Add function on CakePHP2.0
$Posts = $this->generate('Posts', array(
'methods' => array(
'isAuthorized'
),
'models' => array(
'Post' => array('save')
),
'components' => array(
'RequestHandler' => array('isPut'),
'Email' => array('send'),
'Session'
)
));
// tell PHPUnit that `isAuthorized` should return true any time it's called
$Posts
->expects($this->any())
->method('isAuthorized')
->will($this->returnValue(true));
// tell PHPUnit to expect `isPut` once, and to return false
$Posts
->RequestHandler
->expects($this->once())
->method('isPut')
->will($this->returnValue(false));
For more information on mocking: http://www.phpunit.de/manual/3.0/en/mock-objects.html
So i'm working with CakePHP v1.2.5. On my current project, I decided to start writing tests as I code the functionality (yay TDD). I'm having trouble with fixture loading though.
To aid in the process, I'll describe my code (Really quite simple right now). My model is defined like so
// app/models/newsitem.php
<?php
class NewsItem extends AppModel
{
var $name='NewsItem';
}
?>
// app/tests/fixtures/newsitem_fixture.php
<?php
class NewsItemFixture extends CakeTestFixture
{
var $name = 'NewsItem';
var $import = 'NewsItem';
var $records = array(
array('id' => '1', 'title' => 'News Item 1', 'body' => 'This is the first piece of news', 'created' => '2007-03-18 10:39:23', 'modified' => '2007-03-18 10:41:31'),
array('id' => '2', 'title' => 'News 2', 'body' => 'This is some other piece of news', 'created' => '2009-05-04 9:00:00', 'modified' => '2009-05-05 12:34:56')
);
}
?>
// app/tests/models/newsitem.test.php
<?php
App::Import('Model', 'NewsItem');
class NewsItemTestCase extends CakeTestCase
{
var $fixtures = array('app.newsitem');
function setUp()
{
$this->NewsItem =& ClassRegistry::init('NewsItem');
}
function testFindAll()
{
$results = $this->NewsItem->findAll();
$expected = array(
array('NewsItem' => array('id' => '1', 'title' => 'News Item 1', 'body' => 'This is the first piece of news', 'created' => '2007-03-18 10:39:23', 'modified' => '2007-03-18 10:41:31')),
array('NewsItem' => array('id' => '2', 'title' => 'News 2', 'body' => 'This is some other piece of news', 'created' => '2009-05-04 9:00:00', 'modified' => '2009-05-05 12:34:56'))
);
print_r($results);
$this->assertEqual($results, $expected);
}
}
?>
Anyway, my problem is, when I run the test suite in a browser (going to http://localhost/test.php), the test case runner tries to load my app's layout (which is weird cuz I'm just testing the model) which references another model which is obviously not loaded in the test database and I get an error.
And if I remove the
var $fixtures = array('app.newsitem') line from my NewsItemTestCase file, the test case runs properly, BUT it doesn't load the fixtures (for obvious reasons).
Any ideas, suggestions? To be honest I'm having a little trouble finding more than 3 tutorials on this matter.
this was long ago but the issue is the naming conventions, if the fixture is called 'NewsItemFixture' the file should be news_item_fixture, not newsitem_fixture. if you want the file called newsitem_fixture the fixture class should be NewsitemFixture.
same goes for all other files, like the model you have there.