Unit Testing Laravel need help on how to pass my variables properly - json

So I'm getting into Unit Testing and I have created a test for creating and adjustment line in my DB. Here is the code:
$response = $this->json('POST', '/quotes/3/adjustment',
[
'adjustments' => array([
'description' => 'TEST-Description',
'amount' => 1000,
'quote_id' => 3
])
]
);
$response->assertStatus(201);
When creating it hits my controller then an instance of my Adjustment model is created, and in that model I have this code for the creating of it:
foreach($request->adjustments as $adjustment) {
if(array_key_exists('id', $adjustment)) {
$this->find($adjustment['id'])->update([
'description' => $adjustment['description'],
'amount' => $adjustment['amount'],
'quote_id' => $quote->id
]);
} else {
$this->create([
'description' => $adjustment['description'],
'amount' => $adjustment['amount'],
'quote_id' => $quote->id,
]);
}
}
return $quote;
So it expects adjustments to be an array and I thought I had it coded properly in the test but i get back a 200 response, which is not the 201 as expected. Any ideas on how to properly pass the single array in my test file so that it passes the test?
Here is my controller:
$adjustment = new Adjustment();
return $adjustment->newAdjustment($quote, $request)->adjustments;
On a side note if I run this in postman as raw JSON(applicatoin/json) it creates the resource in the DB:
{
"adjustments": [{
"description": "testing-postman",
"amount": 1000,
"quote_id": 1
}
]
}

As of Laravel 5.6, if you return a newly created model from your controller, Laravel will automatically set the response status to a 201. This typically is what you would do when building an API that follows RESTful practices.
However that may not suit your case as you may need to return other data from your controller and not just the newly created model and if so, I believe Laravel will return a 200 instead.
So you have a few of options:
In your controller you could force the 201 with return response($myData, 201);
Return only the newly created model and nothing else.
Or just have your test do the following:
$response->assertStatus(200);
$this->assertDatabaseHas('adjustments', $adjustment->toArray());
With the third option, your test is verifying that everything went okay and that the actual model was created and exists in the database (you'll need to adjust it based on your needs).
More info on the Laravel 5.6 201 response: https://laravel.com/docs/5.6/upgrade - Search on the page for: Returning Newly Created Models

Related

How to query .tab pages from local wikidata instance using API

I am using the Extension:JsonConfig on my docker instance of wikidata that has some tables loaded onto it. The configuration for the extension in my LocalSettings.php is as follows,
$wgJsonConfigEnableLuaSupport = true;
$wgJsonConfigModels['Tabular.JsonConfig'] = 'JsonConfig\JCTabularContent';
$wgJsonConfigs['Tabular.JsonConfig'] = [
'namespace' => 486,
'nsName' => 'Data',
// page name must end in ".tab", and contain at least one symbol
'pattern' => '/.\.tab$/',
'license' => 'CC0-1.0',
'isLocal' => true,
'store' => true,
];
When i query the local instance using the following url,
http://<DOMAIN_HERE>/w/api.php?action=query&list=search&srsearch=tab contentmodel:Tabular.JsonConfig &srnamespace=486&srlimit=10&format=json
i receive the following response
{"batchcomplete":"","limits":{"search":10},"query":{"searchinfo":{"totalhits":0},"search":[]}}
which means that no matches have been found even though tables that match the query statement do exist.
This same query works with commons database when the following is done
https://commons.wikimedia.org/w/api.php?action=query&list=search&srsearch=tab%20contentmodel:Tabular.JsonConfig%20&srnamespace=486&srlimit=10&format=json
Can anyone point me out as to what i am doing wrong here?

How to implement Filtering on YII restful GET api?

I am working on Restful APIs of Yii.
My controller name is ProductsController and the Model is Product.
When I call API like this GET /products, I got the listing of all the products.
But, now I want to filter the records inside the listing API.
For Example, I only want those records Which are having a product name as chairs.
How to implement this?
How to apply proper filtering on my Rest API. I am new to this. So, I have no idea how to implement this. I also followed their documentation but unable to understand.
May someone please suggest me a good example or a way to achieve this?
First of all you need to have validation rules in your model as usual.
Then it's the controllers job and depending on the chosen implementation I can give you some hints:
If your ProductsController extends yii\rest\ActiveController
Basically the easiest way because almost everything is already prepared for you. You just need to provide the $modelClass there and tweak actions() method a bit.
public function actions()
{
$actions = parent::actions();
$actions['index']['dataFilter'] = [
'class' => \yii\data\ActiveDataFilter::class,
'searchModel' => $this->modelClass,
];
return $actions;
}
Here we are modifying the configuration for IndexAction which is by default responsible for GET /products request handling. The configuration is defined here and we want to just add dataFilter key configured to use ActiveDataFilter which processes filter query on the searched model which is our Product. The other actions are remaining the same.
Now you can use DataProvider filters like this (assuming that property storing the product's name is name):
GET /products?filter[name]=chairs will return list of all Products where name is chairs,
GET /products?filter[name][like]=chairs will return list of all Products where name contains word chairs.
If your ProductsController doesn't extend yii\rest\ActiveController but you are still using DataProvider to get collection
Hopefully your ProductsController extends yii\rest\Controller because it will already benefit from serializer and other utilities but it's not required.
The solution is the same as above but now you have to add it by yourself so make sure your controller's action contains something like this:
$requestParams = \Yii::$app->getRequest()->getBodyParams(); // [1]
if (empty($requestParams)) {
$requestParams = \Yii::$app->getRequest()->getQueryParams(); // [2]
}
$dataFilter = new \yii\data\ActiveDataFilter([
'searchModel' => Product::class // [3]
]);
if ($dataFilter->load($requestParams)) {
$filter = $dataFilter->build(); // [4]
if ($filter === false) { // [5]
return $dataFilter;
}
}
$query = Product::find();
if (!empty($filter)) {
$query->andWhere($filter); // [6]
}
return new \yii\data\ActiveDataProvider([
'query' => $query,
'pagination' => [
'params' => $requestParams,
],
'sort' => [
'params' => $requestParams,
],
]); // [7]
What is going on here (numbers matching the code comments):
We are gathering request parameters from the body,
If these are empty we take them from the URL,
We are preparing ActiveDataFilter as mentioned above with searched model being the Product,
ActiveDataFilter object is built using the gathered parameters,
If the build process returns false it means there is an error (usually unsuccessful validation) so we return the object to user to see list of errors,
If the filter is not empty we are applying it to the database query for Product,
Finally we are configuring ActiveDataProvider object to return the filtered (and paginated and sorted if applicable) collection.
Now you can use DataProvider filters just as mentioned above.
If your ProductsController doesn't use DataProvider to get collection
You need to create your custom solution.

putting if statement in api with laravel

I'm currently learning on how API works and my mentor gave me a task to create submit the form using API and store the data on database with laravel and the task also require mandatory if logic on some field.
I have succeeded with the first task (storing data to the database ) and I'm having difficulties writing the mandatory if.
I'm confused, on my task paper I'm told to create one controller, one model and two endpoints (request-schedule and request-leaving) where each endpoint should have some parameters.
and for the request-leaving parameter, there are 10 parameters, 6 of them have this requirement like (mandatory if request type Request Day Off)
There are 3 requests typewritten on there
1. Request Day off
2. Request Schedule
3. Change Schedule
I'm a super newbie in programming, does anyone know how to solve this?
public function CreateReqSchedule(Request $request)
{
$reqschedule = new B777();
$reqschedule->reqtype = $request->input('reqtype');
$reqschedule->startdate = $request->input('startdate');
$reqschedule->enddate = $request->input('enddate');
$reqschedule->reason = $request->input('reason');
$reqschedule->route = $request->input('route');
$reqschedule->actualschedule = $request->input('actualschedule');
$reqschedule->changetoschedule = $request->input('changetoschedule');
$reqschedule->swapcrewid = $request->input('swapcrewid');
$reqschedule->swapcrewschedule = $request->input('swapcrewschedule');
$reqschedule->note = $request->input('note');
$reqschedule->save();
return response()->json($reqschedule);
}
code above is my only work, I'm feeling anxious, because I've googled it myself but I'm still stuck.
So you are talking about validations. you can put laravel validation into it like below
Use required validation for mandotory data and return response in json
// Making validation for fields
$validator = \Validator::make($request->all(), [
'fields1' => 'required',
'fields2' => 'required',
'fields3' => 'required',
]);
if ($validator->fails())
{
// return response on validaton fails
return response()->json(['status'=>400,'errors'=>$validator->errors()->all()]);
}
// If validation passes store your data in database

Laravel: Store error messages in database

Any one know how to send error messages to database in laravel which generate from app/exceptions/handler.php ?
I need to send what error massages generated in report() method to database.
If you are interested doing this manually, you can do something as following.
Step 1 -
Create a model to store errors that has a DB structure as following.
class Error extends Model
{
protected $fillable = ['user_id' , 'code' , 'file' , 'line' , 'message' , 'trace' ];
}
Step 2
Locate the App/Exceptions/Handler.php file, include Auth, and the Error model you created. and replace the report function with the following code.
public function report(Exception $exception) {
// Checks if a user has logged in to the system, so the error will be recorded with the user id
$userId = 0;
if (Auth::user()) {
$userId = Auth::user()->id;
}
$data = array(
'user_id' => $userId,
'code' => $exception->getCode(),
'file' => $exception->getFile(),
'line' => $exception->getLine(),
'message' => $exception->getMessage(),
'trace' => $exception->getTraceAsString(),
);
Error::create($data);
parent::report($exception);
}
(I am demonstrating this using laravel 5.6)
Because Laravel uses Monolog for handling logging it seems that writing Monolog Handler would be the cleanest way.
I was able to find something that exists already, please have a look at monolog-mysql package. I did not use it, so I don't know whether it works and if it works well, but it's definitely good starting point.

The default remember_me token in Laravel is too long

TL;DR How can I use my own way of generating the remember_me token?
I have an old site, written without any framework, and I have been given the job to rewrite it in Laravel (5.4.23). The DB is untouchable, cannot be refactored, cannot be modified in any way.
I was able to customise the Laravel authentication process using a different User model, one that reflect the old DB. But when it comes to the "Remember me" functionality, I have an issue with the length of the token.
The old site already uses the "Remember me" functionality but its DB field has been defined as BINARY(25). The token generated by the SessionGuard class is 60 characters long.
My first attempt was to try and find a way to shorten the token before writing it into the DB, and expand it again after reading it from the DB. I couldn't find such a way (and I'm not even sure there is such a way).
Then I looked into writing my own guard to override the cycleRememberToken (where the token is generated). I couldn't make it work, I think because the SessionGuard class is actually instantiated in a couple of places (as opposed to instantiate a class based on configuration).
So, I am stuck. I need a shorten token and I don't know how to get it.
Well, I was on the right track at one point.
I had to create my own guard, register it and use it. My problem, when I tried the first time, was that I did not register it in the right way. Anyway, this is what I did.
I put the following in AuthServiceProvides
Auth::extend('mysession', function ($app, $name, array $config) {
$provider = Auth::createUserProvider($config['provider']);
$guard = new MyGuard('lrb', $provider, app()->make('session.store'));
$guard->setCookieJar($this->app['cookie']);
$guard->setDispatcher($this->app['events']);
$guard->setRequest($this->app->refresh('request', $guard, 'setRequest'));
return $guard;
});
I change the guard in config/auth.php as
'guards' => [
'web' => [
'driver' => 'mysession',
'provider' => 'users',
],
'api' => [
'driver' => 'token',
'provider' => 'users',
],
],
and finally my new guard
class MyGuard extends SessionGuard implements StatefulGuard, SupportsBasicAuth
{
/**
* #inheritdoc
*/
protected function cycleRememberToken(AuthenticatableContract $user)
{
$user->setRememberToken($token = Str::random(25));
$this->provider->updateRememberToken($user, $token);
}
}