I've been using PHPUnit 4.8 on Laravel 5.2, wondering if it's possible to see if an API call does NOT have a JSON object in its response.
You can see if the response has a particular object, but how about doing the opposite?
$this->json('GET', 'api/items')
->seeJson(['id' => "100"])
->notSeeJson(['id' => "222"])//Is there anything like it?
->assertResponseOk();
I've been reading the documentation of PHPUnit and Laravel 5.2, but haven't found how to achieve it.
Any advice will be appreciated.
PS
In order to make sure that a particular object isn't included in the response, it'll suffice to count the total number of objects the response has.
With newer versions of PHPUnit, it's possible to do so with assertJsonCount(2, 'data').
But how about PHPUnit4/Laravel5.2?
It's important to note these are Laravel 5.2 methods, not PHPUnit methods, defined in Illuminate\Foundation\Testing\TestCase.
The inverse of seeJson is dontSeeJson.
$this->json('GET', 'api/items')
->seeJson(['id' => "100"])
->dontSeeJson(['id' => "222"])
->assertResponseOk();
Related
I am trying to implement Policies in my project and I have a custom method askFriend that I want to add to my UserRelationPolicy.
So I implemented in my UserRelationPolicy the askFriend method but when trying to call it from the UserRelationPolicy#askFriend I asked myself how to call it from this method.
Something like $this->authorize('askFriend', $friend); but it was not working, kind of ignoring it at all. So I searched further in the documentation and found that I could bind with a Gate method the specific method in the UserRelationPolicy to a resource name like this :
Gate::resource('userrelation', 'UserRelationPolicy', [
'userrelation.askfriendrelation' => 'askFriendRelation'
]);
You can find the representation here : Documentation Writing Gate
When I try to execute this code I get the following error :
Call to undefined method Illuminate\Auth\Access\Gate::resource()
And nothing more. The Resource method doesn't seem to exist at all. After many search, trying to include every Gate in the header. Trying to call it staticly or with an instance. Nothing work and the method is nowhere near to be found...
Is it something forgotten ? How can I call a custom method from a controller in a policy class ?
Are you sure you are using 5.4? The method Gate::resource was implemented only in 5.4.
If you are using any version behind you will have to use the Gate::define.
Set the Gate abilities in the App\Providers\AuthServiceProvider like this:
Gate::define('userrelation.askfriendrelation', 'UserRelationPolicy#askFriend');
I was using this function in 5.3 and now when I try to use it in 5.4 like this:
$post->tags()->getRelatedIds();
I am getting errors that function does not exist, I checked the documentation for 5.4 and it's not there anymore.
Anyone knows why this usefull function was removed and what I can do to get all ids from related model?
In Laravel 5.4 and 5.5 the getRelatedIds is replaced by allRelatedIds.
$post->tags()->allRelatedIds();
I can't speak for reasons why it has been removed but if you know the primary key name ahead of time (i.e. all your tables have an id column) you can simply do
$post->tags()->select('id')->pluck('id');
if you want a more generic way you'd need to jump through some hoops
$related = $post->tags();
$post->tags()
->select($related->getQualifiedKeyName())
->pluck($related->getKeyName());
I'm working with legacy code. The old report engine uses associative arrays, Laravel's query builder returns an array of objects.
I need to turn objects into arrays. I've tried using:
\DB::connection('tars-test') //->setFetchMode(PDO::FETCH_ASSOC)
but that gets me Class 'App\Http\Controllers\PDO' not found
It's been suggested to put ->all() at the end of the query but that throws error Call to a member function all() on array
The most efficient way would be to set the fetchmode at runtime, for the legacy function and just for the legacy function. How do I do it?
You can use 'toArray' method:
https://laravel.com/docs/5.3/collections#method-toarray
Laravel 5.3 and lower
You went the right way but as you can see laravel is trying to find PDO in App\Http\Controllers\PDO which probably means you forgot to add use Illuminate\Database\PDO;
Laravel 5.4 and up
Since laravel 5.4 this is not an option. But you still can set fetch mode globally: Laravel 5.4 - How to set PDO Fetch Mode?
Or if you still wish to change it just locally:
Return values only (no keys/associative array) in Laravel
I'm working on a Laravel project and I'm using https://laravel.com/docs/5.3/scout with ElasticSearch on a model Offer.
I already have some offers in my database, so I just run the command
`php artisan scout:import "App\Models\Offer"`
for generate the index for use my datas with ElasticSearch.
After that it's ok, I can search in my offers with, for example :
`$offers = Offer::search($request->keywords)->get();`
Now I have a function for create other offers in my database, and I dont know how can I refresh the index for use these new datas ?
In the documentation https://laravel.com/docs/5.3/scout#adding-records, I can read
all you need to do is save a model instance and it will automatically be added to your search index
I tried this and no, when I save() a new Offer, I can't find it in my index.
I tried to re run the command php artisan scout:import "App\Models\Offer" after add anew OFfer, but it's the same, I can't find it in my index.
Did I miss something ? Any ideas ?
Sorry for a late response on this but I ran into this issue myself when I attempted to use Scout. Everything went fine until I realized that the project's data would scale at a rate that went far beyond what scout could handle. In this case, however you can use the push() method instead of save(). I'm not sure why this isn't documented at all and it's rather frustrating but there's an answer at least.
So use:
->push()
instead of:
->save()
to update your indexes.
If that does not work for your specific version there is another way you can do it but it is "slightly" redundant. It involves a mix of using the queue system with the Artisan system and a command. In this you:
Create a queue/job php artisan make:job JobNameHere (As of Laravel 5.2 - 5.4)
Add use Artisan; to the top of that newly created Job Class so you can pull in the Artisan's functionality
In the handle of that Job Class add
class JobNameHere implements ShouldQueue {
...
...
public function handle() {
Artisan::call('scout:import', ['name' => "App\YourModelNameHere"]);
}
}
Add a dispatch call to that job class right after your DB push() process is called.
Example:
class YourController extends Controller {
public function yourUpdateMethod(Request $request) {
//Some code you wrote
//Some more code you wrote
$update_obj->push( $array_to_update_obj);
dispatch(new JobNameHere());
}
}
Test your index by searching on it
If all went well then you should no longer get any errors. Please leave a comment and let me know how it went... provided you're still having this issue.
I would also like to mention that Laravel Scout does not support ElasticSearch anymore as of August of 2016 (I believe). No one really knows why the support was removed but there are a few tutorials out there to help you get Laravel and Elastic search working together again.
I will also note, based on my research, that if your project is small then you should be fine to use Scout and not need to use ElasticSearch. If your project is going to get huge, then you're probably better off finding a package that supports and well documents how handle Laravel's relationships between models. Elastic search is capable of accomplishing this but there are tons of docs that make figuring it out difficult. Here are some semi-decent tutorials to help get you going down the right path.
https://tirdeamihai.com/blog/laravel-and-elasticsearch
https://laravel-news.com/laravel-and-elasticsearch
Plastic is a package that I would currently recommend as it's being actively worked on. Elasticquent hasn't been touched or updated since last year in June.
https://github.com/sleimanx2/plastic#1---create-a-new-index
Is it possible to use Cartalyst/Sentinel with Jenssegers/MongoDB on Laravel 4.2?
I'm currently using Sentry, but I want to try Sentinel with new features.
After installation, I tried this:
$user = Sentinel::register([
'email' => $email,
'password' => $password,
]);
But I've got the following error:
Argument 2 passed to Illuminate\Database\Query\Builder::__construct()
must be an instance of Illuminate\Database\Query\Grammars\Grammar,
null given, called in
/home/vagrant/shared/muzza/vendor/laravel/framework/src/Illuminate/Database/Eloquent/Model.php
on line 1853 and defined
Sentinel doesn't have integration with Jenssegers MongoDB lib.
Dan Syme commented on 2 Jul:
You would have to write the implementations for the different models
used throughout Sentinel.
Then switch out the models for the different entities on the config
file with your own implementations, Sentinel should take care of
everything else.
Maxime-BHA commented on 3 Jul:
As #jenssegers already did for Sentry package, you have to create your
own Models extending the originals models from Sentinel and bind the
attributes defined in those models for relationships/persistances to
the new models etc.. like static $usersModel =
'Cartalyst\Sentinel\Users\EloquentUser'; and also change the config
file.
See exemple for Sentry there :
https://github.com/jenssegers/laravel-mongodb-sentry
I think it's the exact same procedure.
More information you can find here.