How does one run a test for a Symfony Console command? - php

I find the Symfony Console documentation to be quite good. In clear language, it explains how to use Symfony Console to write console commands in PHP, including how to take user input, how to render output, and even how to write tests for these console commands.
However, the documentation doesn't seem to explain how to run these tests. Assuming one has written a test like CreateUserCommandTest.php, which they provide as an example, how would they run it?

Related

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!

How can I run codeception via browser without using php exec

I am currently creating an automated test website where all codeception test logs can be shown. My server runs codeception through cron but the user should be able to run the test manually. My question is can I use codeception class in a php webpage without executing the commandline version? If possible anyone have an idea how?
Yes you can, it is actually also quite simple. Codeception uses the symfony console component for their command line tool. Take a look at the \Codeception\Command\Run::execute() method on how they do it. It can be a bit overwhelming at first glance, but in the end it boils down to this piece of code:
$this->codecept = new Codecept($userOptions);
if ($suite and $test) {
$this->codecept->run($suite, $test);
}

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.

Laravel: artisan explination

I'm new to laravel and I'm trying to understand some of the stuff that the artisan command line tool is doing but I can't seem to find a clear explanation.
When I type in:
php artisan help view:publish
It gives me the syntax and different flags I can throw, but no explanation of what the command actually does.
When I look on laravel's website to read the documentation on the artisan tool I get an incredibly brief explanation of how to use the tool but nothing on any of it's commands and the development section of the docs show's how to build command but not what they do.
I've searched around online and I've not found anything that helps. I'm sure I'm wearing my frustration glasses and I'm missing some obvious sources, but it's hard to take those glasses off sometimes.
Does anyone know where I can find manual so I can rtf out of it??
Try the aritsan command: $ php artisan list
It will give you a synopsis of all of the built-in (and custom-made) commands. In the case of view:publish, this is what it reports:
view
view:publish Publish a package's views to the application

Using Cakephp tests in production

I have a test suite for my cakephp web app using the cakephp phpunit wrapper provided in 2.x. I would love to merge my test suite into production and be able to test periodically in production via a test database. My issue is as followed:
CakePHP uses a file called test.php to run the test suite and index.php to run the site. I want to lock access to my test suite behind our administrative login, because I don't want anyone to be able to run my test suite on our site just by navigating to www.myapp.com/test.php
I cannot find any information about how to do this. Its possible that no one is doing this because this is a bad strategy that I have propose. If that is the case, could someone please direct me to a better cakephp testing strategy. Sorry, I'm new to building tests.
Thank you to anyone that can help.
Don't use the web test suite but use the command line instead. See "Running tests from the command line".
cake test
Also running tests on a production system might not be a good idea either as it can put some load on the server and the site might respond very slow while the tests run.
If you run the tests via command line you can use the command "nice" and set how much CPU load the command you're going to execute is allowed to cause.
Here we set up a server that we run right next to production called development. These two servers are exact matches (I hope...) and we run our test suite from there. I still run them through the browser, but I agree that the command line works as describe by #burzum. Testing on live is a bad idea. It could mess things up and is not worth the risk for us.

Categories