Testing Laravel (5.1) console commands with phpunit - laravel-5

What is the best way to test Laravel console commands?
Here is an example of a command I'm running. It takes in a value in the constructor and in the handle method.
class DoSomething extends Command
{
protected $signature = 'app:do-something';
protected $description = 'Does something';
public function __construct(A $a)
{
...
}
public function handle(B $b)
{
...
}
}
In my test class, I can mock both A and B, but I can't figure out how to pass $a in.
$this->artisan('app:do-something', [$b]);
Is it possible? Or am I going about this all wrong? Should I pass everything in thought the handle() method?
Thanks.

You will have to change around how you call the command in testing, but it is possible to mock an object passed through.
If the class used by Artisan is dependency-injected like this:
public function __construct(ActualObject $mocked_A)
{
//
}
Then write up the test case like this:
$mocked_A = Mockery::mock('ActualObject');
$this->app->instance('ActualObject', $mocked_A);
$kernel = $this->app->make(Illuminate\Contracts\Console\Kernel::class);
$status = $kernel->handle(
$input = new Symfony\Component\Console\Input\ArrayInput([
'command' => 'app:do-something',
]),
$output = new Symfony\Component\Console\Output\BufferedOutput
);
$console_output = $output->fetch();
The $this->app->instance('ActualObject', $mocked_A); line is where you are able to call upon and use the mocked version of your class, or object, instead of the actual.
This will work in Laravel or Lumen.

Related

how can I pass parameter to laravel getAttribute when I am appending it

this is my laravel custom accessor which I am appending using
protected $appends = [leave_balances];
public function getLeaveBalancesAttribute() {
// some code
}
I want to pass a parameter when I am calling this accessor like this
public function getLeaveBalancesAttribute($parameter) {
// use $parameter here
}
$payslip = Payslip::find(1);
\Log::debug($payslip->leave_balances("PARAMETER"));
I have searched and found that it is not possible. please can some one provide any solution to this I need to pass this parameter.
you dont append attribute unless you want it to act as an attribute,
you can just create a method since you are calling it like a method
in you Payslip model
public function leaveBalances( $params ) {
return $params
}
then you can use it like
$payslip = Payslip::find(1);
$payslip->leaveBalances("PARAMETER") // which output PARAMETER
If you declare an Attribute, you can only use it like this (following your example:
protected $appends = ['leave_balances'];
public function getLeaveBalancesAttribute()
{
return 'Hi!';
}
$payslip = Payslip::find(1);
$value = $payslip->leave_balances;
dd($value); // This will output string(Hi!)
What you (I think) want is setLeaveBalancesAttribute, so you can pass a value and do whatever you want with it:
public function setLeaveBalancesAttribute($parameter)
{
return $parameter.' Yes!';
}
$payslip = Payslip::find(1);
$payslip->leave_balances = 'It works!';
dd($payslip->leave_balances); // This will output string(It works! Yes!)
But, if you are using Laravel 9+, please do use the new way of defining attributes, it is better.
You can set the attribute $appends in the model where you have the accessor. Something like this:
protected $appends = ['the name of accessor'];
However, it will be in the most, I think in all, the responses or query you do with the model you declare it.
Another options is creating a single instance of the model using the ::find method. For example:
$model_instance = Model::find($id);
$attribute = $model_instance->attribute;
Here is the documentation reference: https://laravel.com/docs/9.x/eloquent-mutators#defining-an-accessor

Laravel Controller - Check if parameter passed to method inside controller is a Model

Basically, all i want to do is check if the object passed to the method is a model. Doesn't matter what the model is, I just want to make sure its a model. So i tried typecasting the $resource variable/parameter with Model $resource however this returned an error.
Argument 1 passed to App\Http\Controllers\Handlers\FileUploadHandler::replaceFile() must be an instance of App\Http\Controllers\Handlers\Model, instance of App\Models\Resource\Ingredient given, called in E:\Sites\kara\app\Http\Controllers\Resources\IngredientsController.php on line 109
Here is my method:
// Handle File Replacement
public static function replaceFile( $resource, Request $request, $storageFolder = 'noPath', $formName = 'fileUpload') {
// Delete the old file
Storage::delete($resource->fullPath);
// Place the new file
return self::uploadFile($request, $storageFolder, $formName);
}
Here is how i'm calling it:
// Create the object
$ingredient = Ingredient::where('id', $id)->first();
$file = FileUploadHandler::replaceFile($ingredient, $request, 'ingredients');
To check if some variable is pointing to a Model you can use instanceof.
Example:
<?php
class A{};
class B extends A{};
$obj = new B();
if ($obj instanceof A) {
echo 'yes';
}
Output: yes
Figured it out:
You have to add the Illuminate\Database\Eloquent\Model class in your use statements.
So when you call Model $resource it will return ok.

Unit Testing with Model::Find Fails in Laravel

I am creating unit tests for my rather large Laravel application.
This test passes all assertions. It creates a new database entry for the advertising status model.
/** #test **/
public function create_a_new_advertising_status_test()
{
$status = AdvertisingStatus::create(['name' => 'Test']);
$this->assertNotNull($status);
$this->assertCount(1, AdvertisingStatus::all());
$this->assertEquals($status->name, $this->advertisingStatus['name']);
}
This test fails the NotNull assertion as it says $status is null, which means that the Find method is failing.
/** #test **/
public function edit_an_existing_advertising_status_entry()
{
$status = AdvertisingStatus::create(['name' => 'Test']);
// $status = AdvertisingStatus::first(); // This successfully finds the database entry
$status = AdvertisingStatus::find(1); // This fails to find the database entry
$status->name = 'Edit';
$status->save();
$this->assertNotNull($status);
$this->assertCount(1, AdvertisingStatus::all());
$this->assertEquals($status->name, "Edit");
}
It appears like the find function and subsequently the where function takes too long to locate the entry, so the test renders the $status variable null.
Aside from using the Model::first() function, does anyone have any idea how to overcome this?
I'm wondering if it's because my application is extremely large and takes a long time to run because I'm using RefreshDatabase
You have to create a factory first, then you will use it like this:
$status = factory(AdvertisingStatus::class)->create(['name' => 'Test']);

Testing Laravel View Composers with Mockery

I am trying to test my View Composers. Whenever I pass an object to the $view->with('string', $object), my test fails. This is when I do the test like this:
$view
->shouldReceive('with')
->with('favorites', $this->user->favorites(Ad::class)->get())
->once();
I'm pretty sure this is due to strict checking. So I looked around and saw this issue. However, I can't seem to get it working. The closure return true, but the test fails:
Mockery\Exception\InvalidCountException : Method with('favorites',
< Closure===true >) from Mockery_3_Illuminate_View_View should be called
exactly 1 times but called 0 times.
Here is my current test
public function it_passes_favorites_to_the_view()
{
$this->setUpUser(); // basically sets $this->user to a User object
Auth::shouldReceive('user')
->once()
->andReturn($this->user);
$composer = new FavoritesComposer();
$view = Mockery::spy(View::class);
$view
->shouldReceive('with')
->with('favorites', Mockery::on(function($arg) {
$this->assertEquals($this->user->favorites(Ad::class)->get(), $arg);
}))
->once();
$composer->compose($view);
}
FavoritesComposer class:
public function compose(View $view)
{
$user = Auth::user();
$favorites = $user
? $user->favorites(Ad::class)->get()
: collect([]);
$view->with('favorites', $favorites);
}
How do I test object like this?
I fixed the issue by replacing $view->with('favorites', $favorites); with $view->with(['favorites' => $favorites]); and then testing it like this:
$view
->shouldReceive('with')
->with(['favorites' => $this->user->favorites(Ad::class)->get()])
->once();
So, essentially using only one parameter in the with()-method is what fixed it for me.

Adding methods to Eloquent Model in Laravel

I'm a bit confused how I am to add methods to Eloquent models. Here is the code in my controller:
public function show($id)
{
$limit = Input::get('limit', false);
try {
if ($this->isExpand('posts')) {
$user = User::with(['posts' => function($query) {
$query->active()->ordered();
}])->findByIdOrUsernameOrFail($id);
} else {
$user = User::findByIdOrUsernameOrFail($id);
}
$userTransformed = $this->userTransformer->transform($user);
} catch (ModelNotFoundException $e) {
return $this->respondNotFound('User does not exist');
}
return $this->respond([
'item' => $userTransformed
]);
}
And the code in the User model:
public static function findByIdOrUsernameOrFail($id, $columns = array('*')) {
if (is_int($id)) return static::findOrFail($id, $columns);
if ( ! is_null($user = static::whereUsername($id)->first($columns))) {
return $user;
}
throw new ModelNotFoundException;
}
So essentially I'm trying to allow the user to be retrieved by either user_id or username. I want to preserve the power of findOrFail() by creating my own method which checks the $id for an int or string.
When I am retrieving the User alone, it works with no problem. When I expand the posts then I get the error:
Call to undefined method
Illuminate\Database\Query\Builder::findByIdOrUsernameOrFail()
I'm not sure how I would go about approaching this problem.
You are trying to call your method in a static and a non-static context, which won't work. To accomplish what you want without duplicating code, you can make use of Query Scopes.
public function scopeFindByIdOrUsernameOrFail($query, $id, $columns = array('*')) {
if (is_int($id)) return $query->findOrFail($id, $columns);
if ( ! is_null($user = $query->whereUsername($id)->first($columns))) {
return $user;
}
throw new ModelNotFoundException;
}
You can use it exactly in the way you are trying to now.
Also, you can use firstOrFail:
public function scopeFindByIdOrUsernameOrFail($query, $id, $columns = array('*')) {
if (is_int($id)) return $query->findOrFail($id, $columns);
return $query->whereUsername($id)->firstOrFail($columns);
}
Your method is fine, but you're trying to use it in two conflicting ways. The one that works as you intended is the one in the else clause, like you realised.
The reason the first mention doesn't work is because of two things:
You wrote the method as a static method, meaning that you don't call it on an instantiated object. In other words: User::someStaticMethod() works, but $user->someStaticMethod() doesn't.
The code User::with(...) returns an Eloquent query Builder object. This object can't call your static method.
Unfortunately, you'll either have to duplicate the functionality or circumvent it someway. Personally, I'd probably create a user repository with a non-static method to chain from. Another option is to create a static method on the User model that starts the chaining and calls the static method from there.
Edit: Lukas's suggestion of using a scope is of course by far the best option. I did not consider that it would work in this situation.

Resources