error in registering command in laravel 5.0 - laravel

i am trying to make my own command in laravel 5.0, but it generates the following error. i have registerd my command in kernel. following are my codes
//folowing error
[ErrorException]
Argument 1 passed to Illuminate\Console\Application::add() must be an instance of Symfony\Component\Console\Command
\Command, instance of App\console\Commands\qwork given, called in C:\xampp\htdocs\queue_mail\vendor\laravel\framewo
rk\src\Illuminate\Console\Application.php on line 115 and defined
//my command qwork.php
<?php namespace App\console\Commands;
use DB;
use App\Commands\Command;
use Illuminate\Queue\SerializesModels;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Contracts\Bus\SelfHandling;
use Illuminate\Contracts\Queue\ShouldBeQueued;
class qwork extends Command implements SelfHandling, ShouldBeQueued {
use InteractsWithQueue, SerializesModels;
/**
* Create a new command instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Execute the command.
*
* #return void
*/
public function handle()
{
DB::insert('insert into users (name, email) values (?, ?)', ['faran', 'ran.rana#gmail.com']);
}
}
//then defining route by:
Route::get('/run', function()
{
Artisan::queue('command:qwork');
});

Replace
use App\Commands\Command;
with
use Illuminate\Console\Command;

Related

I am getting error about email in Laravel project

I have a problem to solve. However, this problem came to me from sentry.io. So I didn't see the problem directly.
The problem is as follows: Address in mailbox given [] does not comply with RFC 2822, 3.6.2.
enter image description here
The problem with sentry.io is in the picture. However, as far as I understand, the problem is not directly caused by this file. In other words, the problem is in sending mail, so since email is used here, it shows the problem here. I have not used services such as laravel mailing before, so I have no idea right now.
I can share with you the necessary codes, files, etc., so that we can better understand the problem.
Send Mail
Mail::to($user->email)->send(new NothingListenedFor3Days($user));
Mail Class
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class NothingListenedFor3Days extends Mailable
{
use Queueable, SerializesModels;
protected $user;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($user)
{
$this->user = $user;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->subject('Your Subject')
->view('Your blade file path')->with('user',$this->user);
}
}
For reference link
#Ali Raza
I made both files as I shared below. Are there any mistakes I've made?
EmailCronJobController.php
Mail::to($user->email)->send(new NothingListenedFor3Days($user));
NothingListenedFor3Days.php
namespace App\Mail\Listener;
use App\Course;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
class NothingListenedFor3Days extends Mailable
{
use Queueable, SerializesModels;
public $listener;
public $random_contents;
public $fromEmail = 'listener#omnicourse.io';
public $fromName = 'Omnicourse';
/**
* Create a new message instance.
*
* #return void
*/
public function __construct(User $listener)
{
$this->listener = $listener;
$this->random_contents = Course::all()->random(10);
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->from($this->fromEmail,$this->fromName)
->view('email.listener.nothing_listened_for_3_days')->with('user',$this->listener);
->text('email.listener.nothing_listened_for_3_days_plain')
->subject("Let's move ahead together");
}
}

Laravel 8 - class not using mock version of class in test

Hi Latest question on unraveling code - I'm trying to create tests that mock certain classes but struggling to get my first attempt to work. It does not appear to be using the mock in the test but the real class.
There are three questions:
What do I need to change to get this to work and the mock to be used?
Does the mock work for the whole app under test or just the class under test?
If I wanted to return an array of values do I just create the array with the expected values to enable the class under test to use that returned data?
So here's the class I want to mock:
<?php
namespace App\Wedleague\Utility;
use App\Libraries\Utilities\FolderUtility;
use App\Wedleague\LeagueTable\LeagueTableInterface;
use PDF;
/**
* Class PDFGenerator
* Generates a PDF based on a league
* #package App\Libraries\Helpers
*/
class PDFGenerator
{
use FolderUtility;
/**
* set the folder location for the generated pdf
* #var string
*/
private string $storageFolder = 'pdf';
private string $path;
private LeagueTableInterface $leagueTable;
/**
* PDFGenerator constructor.
*/
public function __construct(LeagueTableInterface $leagueTable)
{
$this->path = storage_path('app/' . $this->storageFolder);
$this->setUp();
$this->leagueTable = $leagueTable;
}
/**
* #return \Barryvdh\DomPDF\PDF
*/
public function createLeaguePDF()
{
$this->leagueTable->getLeagueTable();
error_reporting(E_ALL ^ E_DEPRECATED);
return PDF::loadView($this->leagueTable->getViewPath(), $this->leagueTable->getViewData())
->setOptions(['fontSubsetting' => true])
->setPaper('a4', 'landscape')
->save(storage_path('app/pdf/' . $this->leagueTable->getLeagueType() . '_league_update.pdf'));
}
private function setUp()
{
$this->checkFolderExists($this->path);
}
}
Here's the class I'm trying to test that uses this class:
<?php
namespace App\Jobs;
use App\Wedleague\Utility\PDFGenerator;
use App\Mail\MailEclecticUpdate;
use App\Models\League;
use App\Wedleague\LeagueTable\EclecticLeagueTable;
use Exception;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Mail;
/**
* Class ProcessSendEclecticUpdate
* #package App\Jobs
*/
class ProcessSendEclecticUpdateEmail implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
/**
* The number of times the job may be attempted.
*
* #var int
*/
public $tries = 3;
/**
* The number of seconds the job can run before timing out.
*
* #var int
*/
public $timeout = 240;
private League $league;
public function __construct(League $league)
{
$this->league = $league;
}
public function handle()
{
if (!$this->league || $this->league->league_type !== 'eclectic') {
throw new Exception("Error Processing the job - league not found or invalid league", 1);
}
$PDFGenerator = new PDFGenerator(new EclecticLeagueTable($this->league));
$PDFGenerator->createLeaguePDF();
$this->league->player()->get()->each(function ($player) {
if ($player->contactEmail) {
Mail::to($player->contactEmail)
->queue(new MailEclecticUpdate(
$this->league
));
}
});
Log::info('Eclectic League update issued');
}
}
and here's the basics of the test:
<?php
namespace Tests\Jobs;
use App\Jobs\ProcessSendEclecticUpdateEmail;
use App\Mail\MailEclecticUpdate;
use App\Models\League;
use App\Models\User;
use App\Wedleague\Utility\PDFGenerator;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Foundation\Testing\WithFaker;
use Illuminate\Support\Facades\Bus;
use Illuminate\Support\Facades\Mail;
use Tests\TestCase;
/**
* Class ProcessSendEclecticUpdateTest
* #package Tests\Jobs
*/
class ProcessSendEclecticUpdateTest extends TestCase
{
use RefreshDatabase;
use WithFaker;
private $user;
private $adminUser;
protected function setUp(): void
{
parent::setUp();
$this->seed();
$this->adminUser = User::factory()->create(['admin' => 1]);
}
/**
* #test
* #covers ProcessSendEclecticUpdateEmail::handle
* #description:
*/
public function testHandle()
{
$mock = $this->mock(\App\Wedleague\Utility\PDFGenerator::class);
$mock->shouldReceive('createLeaguePDF')->once();
$this->app->instance(PDFGenerator::class, $mock);
Mail::fake();
$this->withoutExceptionHandling();
$league = League::find(27);
$players = $league->player()
->get()
->filter(function ($player){
return $player->contactEmail;
});
ProcessSendEclecticUpdateEmail::dispatch($league);
Mail::assertQueued(MailEclecticUpdate::class, $players->count());
}
}
The response I get from the test is:
Mockery\Exception\InvalidCountException : Method createLeaguePDF(<Any Arguments>) from Mockery_2_App_Wedleague_Utility_PDFGenerator should be called
exactly 1 times but called 0 times.
I know this is not using the mock due to the length of time the test takes as it produces a PDF
Any ideas how I can get this to work - it's mocking me!!! :)

Laravel: Passing a class name from a Controller to a Job

I'm trying to pass a certain class name from the controller to a job file through
Job::dispatch($className);
I've tried passing it as a string and passing it as a ClassName::class but none of these methods work
The code in the Job file looks something along the lines of this:
<?php
namespace App\Jobs;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
class BreakUpArraysJob implements ShouldQueue
{
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
protected $products;
protected $jobName;
public function __construct($products, $jobName)
{
$this->products = $products;
$this->jobName = $jobName;
}
/**
* Execute the jobName.
*
* #return void
*/
public function handle()
{
$productArrays = array_chunk($this->products, 5000);
foreach($productArrays as $productArray){
$this->jobName::dispatch($productArray);
}
}
}
the $jobname variable is the className I'm trying to pass.
I also need to note that I'm passing the classname of a different Job from the Controller, by which I mean the job handler is supposed to call another Job through the variable.
I do not think your syntax is working, an alternative approach would be to do it like this.
dispatch(new $this->jobName($productArray));
in your job construct add class Name that your var is instance of before $product like this:
public function __construct(Product $products, $jobName)
{
$this->products = $products;
$this->jobName = $jobName;
}

Laravel class not found for mail

I'm trying to generate a notification email to the webmaster when there's a new registration on the site I'm building.
I have a mail class called SignedUp at App\Mail\SignedUp.php.
In the Illuminate/Foundation/Auth/RegistersUser file, I have the following:
namespace Illuminate\Foundation\Auth;
use App\Organization;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Auth\Events\Registered;
use App\Mail\SignedUp;
trait RegistersUsers
{
use RedirectsUsers;
/**
* Show the application registration form.
*
* #return \Illuminate\Http\Response
*/
public function showRegistrationForm()
{
$organizations = Organization::all();
return view('auth.register', compact('organizations'));
}
/**
* Handle a registration request for the application.
*
* #param \Illuminate\Http\Request $request
* #return \Illuminate\Http\Response
*/
public function register(Request $request)
{
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
$this->guard()->login($user);
Mail::to('example#example.com')->send(new SignedUp($user));
return $this->registered($request, $user)
?: redirect($this->redirectPath());
}
When I register a new user, I get this message:
Class 'Illuminate\Foundation\Auth\Mail' not found
if I change use App\Mail\SignedUp; to use App\Mail;, the error is still: Class 'Illuminate\Foundation\Auth\Mail' not found
use Mail; gives me Illuminate\Foundation\Auth\SignedUp' not found
use App\Mail\SignedUp; gives me Class 'Illuminate\Foundation\Auth\Mail' not found
Not attempting to import the class at all, and instead changing my statement to: \Mail::to('example#example.com')->send(new SignedUp($user));, I get Class 'Illuminate\Foundation\Auth\SignedUp' not found
A cannot figure out how to get it to find my mail class.
I'm just guessing and shooting in the dark - can anyone see what I'm doing wrong?
Here is my mail class file:
<?php
namespace App\Mail;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class SignedUp extends Mailable
{
use Queueable, SerializesModels;
public $url = 'https://av-cuauhtemoc.org/users';
public $user;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct()
{
//
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->from('example#example.com')->view('emails.new-registration');
}
}
I finally stumbled upon the answer.
I also needed to import the use Illuminate\Support\Facades\Mail; class.
So this works:
use App\Mail\Signedup;
use Illuminate\Support\Facades\Mail;
first of all you shouldn't change Illuminate/Foundation/Auth/RegistersUser file, instead you have to overwrite this method in your RegisterController:
use App\Mail\SignedUp;
use Illuminate\Auth\Events\Registered;
use Illuminate\Http\Request;
use Mail;
public function register(Request $request)
{
$this->validator($request->all())->validate();
event(new Registered($user = $this->create($request->all())));
$this->guard()->login($user);
Mail::to('example#example.com')->send(new SignedUp($user));
return $this->registered($request, $user)
?: redirect($this->redirectPath());
}

Laravel Queue is Processed but email not found in inbox

I Am sending emails in Laravel Queue. While using the send method, as shown here
Mail::to($userSocial->getEmail())->send(new WelcomeEmail('1234567', "haha", "Makamu"));
my email is delivered to my inbox. However when i switch to queue like below
Mail::to($userSocial->getEmail())->queue(new WelcomeEmail('1234567', "haha", "Makamu"));
I also used this method
SendEmailSocialReg::dispatch('12345678', "haha", "Makamu");
and monitor via queue:listen i am get the processing. then processed message. No error however.
What could be wrong?
my WelcomeEmail
<?php
namespace App\Mail;
use App\User;
use Illuminate\Bus\Queueable;
use Illuminate\Mail\Mailable;
use Illuminate\Queue\SerializesModels;
use Illuminate\Contracts\Queue\ShouldQueue;
class WelcomeEmail extends Mailable
{
use Queueable, SerializesModels;
public $password;
public $client_name;
public $client_email;
/**
* Create a new message instance.
*
* #return void
*/
public function __construct($password, $email, $name)
{
$this->password = $password;
$this->client_name = $name;
$this->client_email = $email;
}
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->view('email.auto');
}
}
Your WelcomeEmail class must return markdown() at build() function like this:
/**
* Build the message.
*
* #return $this
*/
public function build()
{
return $this->markdown('email.auto');
}
Then the queue worker must be executed with this command:
php artisan queue:work --queue=default

Resources