Schedule a console command in Laravel 5.0 - php

I am super confused about Laravel's commands vs. console commands and which one I'm supposed to use with the task scheduler.
I am attempting to use a console since that's the one that seems to work with the task scheduler per Laravel's docs but for some reason it doesn't allow me to have any other methods in the command file other than the construct, fire, getArguments, and getOptions. I can't put everything in the fire method as it would be one big mess.
Basically I am trying to have a crawler run every 5 minutes. So other methods deal with various parts of the crawling process.
And then in the fire method, I'm trying to call my main method via $this->run(); but it returns error:
Declaration of App\Console\Commands\Crawler::run() should be compatible with Illuminate\Console\Command::run(Symfony\Component\Console\Input\InputInterface $input, Symfony\Component\Console\Output\OutputInterface $output)
Any ideas how to get around this?

Related

Multi threading in Laravel 9

I've a code base written in Laravel 9 which targets to execute multiple requests to one API with 1000 different API keys + API secrets. Is there a way to actually do this simmultaniously and not one after another ?
I've come up to this thread Laravel Commands, Pthreads and Closure but the answer there doesn't look like it is wokring, in my opinion it shows that everything is kinda executing at the same time because there is sleep(rand(1,3)) inside the OperatorThreaded class.
Any idea ?
A Laravelly way is to do it like so:
Create a RequestYourApi Job (php artisan make:job RequestYourApi or make a class yourself inside App\Jobs).
Dispatch any amount of jobs using RequestYourApi::dispatch([/* args */]) (the arguments will be passed to the constructor of this class, and the job will be put into the queue, see https://laravel.com/docs/9.x/queues)
Run multiple workers using for instance Supervisor (or Laravel Horizon): php artisan queue:work (use --help to see additional options)
Scale up/down your workers depending on how fast you need to process things.

Laravel Dusk - Run tests & get results via code

I need to make a tool to Browser test my production system. I have read up all about Laravel Dusk and it seems like a perfect tool. However, I need to run tests automatically via schedule and have a dashboard with the results.
I can easily run the command php artisan dusk from the code using the Scheduler, however, how can I get the results? Is there a better option than simply parsing the Console Output from that command? Ideally I would have a way of getting the status of each test (whether it passed or failed) to be able to log, process and display all that information.
The Dusk documentation hasn't got any more information on running the tests programatically, it only has instructions to run via php artisan dusk.
Has anyone encountered this?
Thank you!
The way I have achieved what I needed is to use the command options for dusk/phpunit.
I used --filter=MyTestClass to single out which test I wanted to run, and --log-junit log.log to log the results for that test, which I then parsed via code as well to fetch the results. This allowed me to build a fully custom dashboard that was able to run each test individually, report the results, send notifications etc.
Not the prettiest solution, but it worked well for what I needed. If anyone encounters better way to achieve that (or just use Dusk in general as a browser/scraper outside phpunit) please do post a comment/answer!

Automatic database manipulations using Symfony every month

I have a use case where i need to do certain database manipulations automatically every month on a specific date.Currently using Symfony 2.7 framework is it possible to call a controller every month on a specific date??.Any feedback's/ideas would be helpful
Instead of calling a controller, you could probably create a Symfony Console Command. Have a look here at how to do it: https://symfony.com/doc/2.7/console.html
With this option, the only way to execute it would be via the cli.
Then you can call the command from a cron job that runs at the time you want. Some examples of how to schedule jobs using crontab: https://tecadmin.net/crontab-in-linux-with-20-examples-of-cron-schedule/
You can make a Console Command in Symfony and call this command every month with a cron task.
Console command

Running PHP Selenium Webdriver tests programmatically, without phpunit command

My question is quite simple. I'm coming from Python world, where it's very simple to execute Selenium testing code within a program, just writing something like:
from selenium import webdriver
driver = webdriver.Firefox()
driver.get("http://www.python.org")
driver.close()
When using PHP, things are getting harder: I wrote something like that
require 'vendor/autoload.php';
class MyTest extends PHPUnit_Extensions_Selenium2TestCase {
public function setUp() {
$this->setBrowser('Firefox');
$this->setBrowserUrl('http://www.python.org');
}
public function testToto() {
$this->url('/');
}
}
...which kinda works when I execute phpunit MyTest.php.
But what I would like to do is to instanciate my test class in PHP code, and execute my Selenium commands "programmatically", like:
$myTest = new MyTest();
$myTest->testToto();
And here it sucks :(
PHP Fatal error: Uncaught exception 'PHPUnit_Extensions_Selenium2TestCase_Exception' with message 'There is currently no active session to execute the 'url' command.
So is there a way to execute Selenium code directly from PHP script without executing command line things with phpunit?
Edit: What am I trying to achieve? My project is to build a testing application which must be able to launch tests within a UI built by a end user thanks to a user friendly drag and drop builder (the user chooses which test he wants to execute first, then another, and so on). So I would like to avid ececuting phpunit commands with a ugly PHP exec: to me, the best option is to launch test case methods programmatically!
I think the pain comes from trying to use the PHPUnit Webdriver integration, without really using PHPUnit.
You can write code like your Python example by using a standalone Webdriver implementation (that does not need PHPUnit). I recommend the one written by Facebook:
https://github.com/facebook/php-webdriver
but there are some more:
http://docs.seleniumhq.org/docs/03_webdriver.jsp#php
You can also use these implementations inside PHPUnit tests. I do that as I don't like the PHPUnit Webdriver implementation.
With these it's trivial to write your example in PHP.
Well, a very nice question first of all. The short answer is yes you can, but it's too much pain. PHPUnit is just a modestly complicated, huge, scary and amazing library with a gadzillion of extensions. In the nutshell it reads the configuration, finds the tests, and runs them.
You can put a break point inside your test and trace to the top what it does, what parameters it accepts and literally simulate the whole thing. That would be the "proper" and crazy way, and the most complex too.
The simpler way would be by finding out what the test case class needs in order to run (break point & trace are always your best friends), in this particular case it turned out to be just this:
$myTest = new MyTest();
$myTest->setUp(); // Your setup will always be called prior the test.
$myTest->prepareSession(); // Specific to Selenium test case, called from `runTest` method.
$myTest->testToto();
But, even in PHPUnit_Extensions_Selenium2TestCase there is a lot of stuff that are not publicly accessible and it feels just a strike of luck. But you get the idea. Besides, simply calling a method of a test case class will result in two things: nothing happens, or you get an exception. All the fancy result tracing happens higher in the hierarchy.
I can only guess what you are trying to achieve, but probably if you ask the question about the actual problem we'd be able to help more.
Edit
exec might seem ugly indeed, but it's there for a very good reason: process isolation. There are situations when one piece of the code that is being tested changes the environment and it becomes conflicting with another piece of code, e.g., session-related, sent headers, etc. When you come across one of them, you will be praying on exec.
In your case, the easiest would be to launch the PHPUnit from the command line, but you might need to write a custom formatter to get the data in the necessary format out of it, unless you are happy with the existing ones.
Another option would be to use the the existing client for the WebDriver / Selenium and simply send commands directly to the Selenium server, I assume that's what you really need? You can find out the piece of code responsible for that in the PHPUnit extension or there's another cool project called Behat (and Mink). I believe their client is in the Behat/MinkSelenium2Driver repository. And if you don't like those, I'm sure there are other php wrappers you can find on the github, or can create your own using the existing ones as an example.
PS: Share a link to the project when it's up and running if its public.

How do I run a php function in a subprocess in Symfony2?

I want to run some functions parallel in the background.
Is there a nice way to do this in Symfony2 or even with php?
Or is there only the Symfony\Component\Process\Process?
Can I use this Process with a function? I need the actual Context (logged in user and some session data), so it is not possible to source the function out to an external php-file...
Symfony2 Process component allows you to run some shell command or execute php-script in a different process.
To run exact function in a thread try look at PHP Thread class

Categories