Functional test: how to set JSON data in a POST method? - json

I want to create a functional test for an action that receives a POST method with data in JSON format.
This is what I have:
info('set car')->
post('/user/'.$user->getId().'/set-car/'.$car->getId()'->
with('request')->ifFormat('json')->begin()->
isParameter('module', 'myModule')->
isParameter('action', 'myAction')->
end()->
But..where should I set the receiving json data?
sf 1.4
Regards
Javi

Did you checked out the page from Jobeet tutorial?
The first example is
$browser = new sfBrowser();
$browser->
get('/')->
click('Design')->
get('/category/programming?page=2')->
get('/category/programming', array('page' => 2))->
post('search', array('keywords' => 'php'))
;
It may be that I didn't understand you question at all.
Correct me if I'm wrong.

More of a workaround...
If you get the content in your action like this:
protected function getContent() {
$content = $this->request->getParameter('content');
if(!$content) $content = $this->request->getContent();
return $content;
}
public function executeIndex(sfWebRequest $request) {
$content = $this->getContent();
//do something with your content :D
}
that should allow you to test what you want by passing
post('search', array('content' => 'whatever my content is'));

Related

Create dynamic request object in Laravel

I was looking for help creating an object of the type Illuminate\Http\Request. This article helped me understand the mechanism of the class, but I did not get the desired result.
Create a Laravel Request object on the fly
I'm editing the development code passed to me by the customer. The code has a function that gets a request parameter from vue and translates it to JSON:
$json = $request->json()->get('data');
$json['some_key'];
This code returned an empty array of data:
$json = $request->request->add([some data]);
or
$json = $request->request->replace([some data]);
this one returned an error for missing the add parameter
$json = $request->json->replace([some data]);
A variant was found by trying and errors. Maybe, it help someone save time:
public function index() {
$myRequest = new \Illuminate\Http\Request();
$myRequest->json()->replace(['data' => ['some_key' => $some_data]]);
$data = MyClass::getData($myRequest);
}
..
class MyClass extends ...
{
public static function getData(Request $request) {
$json = $request->json()->get('data');
$json['some_key'];
In addition, there are other fields in the class that you can also slip data into so that you can pass everything you want via Request
$myRequest->json()->replace(['data' => ['some_key' => $some_data]]);
..
$myRequest->request->replace(['data' => ['some_key' => $some_data]]);
..
$myRequest->attributes->replace(['data' => ['some_key' => $some_data]]);
..
$myRequest->query->replace(['data' => ['some_key' => $some_data]]);
$myRequest = new \Illuminate\Http\Request();
$myRequest->setMethod('POST');
$myRequest->request->add(['foo' => 'bar']);
dd($request->foo);
This from the link you shared works, give it a try. Thanks for sharing that link!

Laravels Artisan::call('migrate:status') as json response

I want to get the migration status of my laravel app in a controller as json response and I try
$migratiorns = \Artisan::call('migrate:status');
return response()->json($migratiorns);
but \Artisan::call returns an integer 0.
What should I use for my case to get the desired response?
The value of $migration will be the output you see on the command line, which is sort of a table, its basically a string value which cannot be converted to json.
An easy way is the following
\Artisan::call('migrate:status');
dd(Artisan::output());
The question is quite old but I got the same problem and didn't find any solution so I made a little helper function to get pending migrations on the fly, maybe it will help someone else:
function getPendingMigration($migrationsFolderPath = false, $toJson = false)
{
$migrationsFolderPath = $migrationsFolderPath ?: database_path('/migrations');
$migrations = app('migrator')->getMigrationFiles($migrationsFolderPath);
$pendingMigrations = [];
foreach ($migrations as $migration => $fullpath){
if(!\Illuminate\Support\Facades\DB::table('migrations')->where('migration', $migration)->exists())
array_push($pendingMigrations, $migration);
}
return $toJson ? json_encode($pendingMigrations) : $pendingMigrations;
}
Here ini my console code
protected $signature = 'command:name {json?}';
ini handle function
public function handle()
{
$json = $this->argument('json');
if($json){
$a = ['foo'=>
['bar', $json]
];
$a = collect($a);
$this->info($a->toJson());
} else {
$this->info('Display this on the screen');
}
}
just run
php artisan command:name -json
will get json output

Using Mojolicious: access a perl hash, passed through an ajax call as json, in javascript [duplicate]

Using DBIx::Class, I found a solution to my issue, thankfully. But I'm sure there has to be a nicer way.
my $record = $schema->resultset("food")->create({name=>"bacon"});
How would I turn this record into a simple hashref instead of having to make this call right after.
my record = $schema->resultset("food")->search({name=>"bacon"})->hashref_array();
Ideally I want to be able to write a code snippet as simple as
{record=> $record}
instead of
{record => {name => $record->name, $record->food_id, ...}}
This would drive me insane with a table that has alot more columns.
I assume you're talking about DBIx::Class?
my $record = $schema->resultset("food")->create({name=>"bacon"});
my %record_columns = $record->get_columns;
# or, to get a HashRef directly
my $cols = { $record->get_columns };
# or, as you've asked for
my $foo = { record => { $record->get_columns } };
What you're looking for is included in DBIx::Class as DBIx::Class::ResultClass::HashRefInflator.

Json call in joomla 2.5 component

I'm making a joomla 2.5 component and i trying to set in model or controller (what's the most appropriate ?) a json response of my DB request (for later get the json with angularJS).
Here's my model (with DB response):
<?php
defined('_JEXEC') or die();
jimport( 'joomla.application.component.modelList' );
class MediastoreModelList extends JModelList
{
function getListQuery()
{
$db = JFactory::getDBO();
$query = $db->getQuery(true);
$query->select('id, type, designation', 'marque', 'prix');
$query->from('produits');
return $query;
}
}
My empty controller:
<?PHP
defined('_JEXEC') or die('Restricted access');
jimport('joomla.application.component.controller');
class MediastoreController extends JController
{
}
My view
<?php
// No direct access to this file
defined('_JEXEC') or die('Restricted access');
jimport( 'joomla.application.component.view' );
class MediastoreViewList extends JView
{
function display($tpl = null)
{
$this->items = $this->get('items');
parent::display($tpl);
}
}
and my template
<?php
defined('_JEXEC') or die('Restricted access');
JHTML::script('media/com_mediastore/js/angular.min.js');
JHTML::script('media/com_mediastore/js/app.js');
?>
<?php
echo $this->items;
?>
<div class="content">
<p>Nothing</p>
</div>
How can i do that ?
Thanks a lot,
Antoine
Bit late .....
Hi I am working on something similar.
In your views (sitepart) add an other file called view.raw.php.
You can browse to this file by appending 'format=raw' at the end of the url.
For example: if your component view page in the browser is 'http://test.com/test( IF SEF is on ) then after appending th url shoul be like this
http://test.com/test?format=raw and in case SEF is not on the use & instead of ?
You can use this URL in the angularjs for http.get service.
In view.raw.php file make sure you just echo the results rather than parent::display($tpl);.
This did work for me.
PS: I am using angualrjs too but want to use the component with many menu items and the url keeps changing so am stuck on that part.
Hope this solves your issue.
Regards,
Jai

HTML response when passing an Object in Zend Controller using end_Json_Encoder::encode

I Have two problem in this Case :
I want to pass a JSON object that i created in Zend Controller
public function exampleAction() {
$answers = array();
for($i = 0 ; $i < 3 ; $i++)
{
$answer = new Answer();
$answer->answer_id = 5 ; // for example
$answer->thanked = 'true';// for example
$answers[] = $answer;
}
echo Zend_Json_Encoder::encode($answers);
}
the Jquery Post function is :
$.post(
"/memberactions/getthanks/",
{values:values},
function(res){
alert(123);
}
, 'json')
First Question :
why the return response is HTML ? the response must be in JSON ?
Second Question
the HTML response is like this
[{"__className":"Answer","thanked":"true","answer_id":"5"}]
How can I make the response like this :
- answer
thanked : true
answer_id : 5
as a JSON Object without the __className:"Answer" (does it hurt to have the class name in the response) ?
Have you disabled layout, viewRenderer etc.? Also, you should send appropriate headers. You can do all this at once using the JSON action helper:
$this->_helper->json($answers);
You could provide a toArray() method in Answer, which would return an array of relevant properties and then use it in your action:
$answers[] = $answer->toArray();