How to call job from traits in laravel? - laravel

I want to use a job in a trait . Lumen give me following error:
Call to undefined method App\Consumers\Journal::dispatch()
this is my code
use App\Jobs\CreateFact;
trait Ledger
{
private function save() {
$this->dispatch(new CreateFact($entries));
}
}
Please help to resolve with issue. I am new in lumen

You can dispatch the job in the trait using:
CreateFact::dispatch($entries);

Related

How to exchange factory() function in Laravel?

I learn Laravel from the tutorial and there is use factory(). In my project show me an error, Undefined function factory. I know that in Laravel 9 factory() does not exist, but someone could help me how can I modify the below code that will run in Laravel 9.
public function run(Faker $faker)
{
factory(
Task::class,
$faker->numberBetween(25, 50)
)->create();
}
factory() was used until Laravel 8. In newer versions of Laravel model factories are used: https://laravel.com/docs/9.x/eloquent-factories#main-content

How to mock only one method with Laravel using PhpUnit

I have this test :
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Foundation\Testing\RefreshDatabase;
use App\Services\AccessTokenService;
use App\Services\MemberService;
class BranchTest extends TestCase
public function testPostBranchWithoutErrors()
{
$this->mock(AccessTokenService::class, function ($mock) {
$mock->shouldReceive('introspectToken')->andReturn('introspection OK');
});
$this->mock(MemberService::class, function ($mock) {
$mock->shouldReceive('getMemberRolesFromLdap')->andReturn(self::MOCKED_ROLES);
});
As you see, there are 2 mocks on this test. The 2nd one 'MemberService:class' is my current problem. In this class there are 2 functions : 'createMember' and 'getMemberRolesFromLdap'. I precise that I want to mock only the 'getMemberRolesFromLdap' function.
In the documentation, it is written :
You may use the partialMock method when you only need to mock a few methods of an object. The methods that are not mocked will be executed normally when called:
$this->partialMock(Service::class, function ($mock) {
$mock->shouldReceive('process')->once();
});
But when I use "partialMock", I have this error:
Error: Call to undefined method Tests\Feature\BranchTest::partialMock()
And when I try a classic mock (no partial), I have this error:
Received Mockery_1_App_Services_MemberService::createMember(), but no expectations were specified
certainly because there are 2 functions in this class and so PhpUnit does not know what to do with the function 'createMember'.
What can I try next? I am a beginner to PhpUnit tests.
Edit
Laravel 6.0
PhpUnit 7.5
PartialMock shorthand is firstly added in Laravel 6.2 see the release notes. So an upgrade to 6.2 should fix your problem.
Secondly you can add the following snippet to your Tests\TestCase.php class and it should work.
protected function partialMock($abstract, Closure $mock = null)
{
return $this->instance($abstract, Mockery::mock(...array_filter(func_get_args()))->makePartial());
}

Laravel PHPUnit Call to a member function connection() on null

I am learning testing on Laravel and I am unable to continue testing Eloquent by this error:
Error: Call to a member function connection() on null
This is my code:
public function test_users_can_follow_each_other()
{
$this->userOne = User::find(1);
$this->userTwo = User::find(2);
$this->userOne->followedBy($this->userTwo->id);
$this->assertTrue($this->userTwo->isFollowedBy($this->userOne->id));
}
I am also using the trait use DatabaseTransactions;
The database is already created with 2 records on users table. Do I need to config anything else, thanks!
replace
PHPUnit\Framework\TestCase
with
Tests\TestCase

Laravel - Running method of another controller in one controller

I have UserController and PetController.
In my UserController, I have rewardUser() method.
in my PetController, I'm using the $user variable which indicates the current logged in user.
How I can run my rewardUser() method from my PetController?
I've been trying to user $user->rewardUser(); but for some reasons its not recognizing my method this way.
"Call to undefined method Illuminate\Database\Query\Builder::rewardUser()"
The best way is to use a trait.
Create a trait file, in App\Common.php, for e.g. Then copy the rewardUser() method to the trait.
Your trait file:
namespace App\Forum;
trait Common {
public function rewardUser() {
// Your code here...
}
}
Then in yourUserController.php and PetController.php, use the trait.
// UserController and PetController.php
namespace App\Http\Controllers
use App\Common; // <- Your trait
class UserController extends Controller {
use Common // <- Your trait
public function doSomething() {
// Call the method from both your controllers now.
$this-rewardUser();
}
}
You can use the straight in as many controllers as you want and you can call the method in the straight using $this->methodName().
Very simply and effective.
It seems like you are missing some structure concepts, but if you really need it, you may use the container to do so:
$userController = app()->make(UserController::class);
return app()->call([$userController, 'rewardUser']);
may be you should define the method rewardUser() in the User Model and import it with use App\User

Dependency Injection from Package Command

I am creating a command for my package.
My constructor is:
public function __construct(\Artisan $artisan)
{
parent::__construct();
$this->artisan = $artisan;
}
Protected $artisan property, of course, is present.
In my service provider register() method I have tried several methods for registering the command.
First:
$this->app['custom.command'] = $this->app->share(function ($app)
{
return new CustomCommand(new \Artisan);
});
$this->commands('custom.command');
Second:
$this->app['custom.command'] = $this->app->share(function ($app)
{
return $app->make('CustomCommand');
});
$this->commands('custom.command');
Normally, it is supposed to work. But when I run the command I always get Call to undefined method Illuminate\Support\Facades\Artisan::call() error message as soon as I run $this->artisan->call('migrate') in my fire() method.
However when I write \Artisan::call('migrate') instead of the $this->artisan->call('migrate') everything works fine.
Does someone have an idea what I did wrong?
Thanks in advance.
I think the problem is that you inject the facade of Artisan and not Artisan itself.
Try
public function __construct(Illuminate\Foundation\Artisan $artisan)
and in your service provider:
return new CustomCommand($app['artisan']);

Resources