Laravel 4 testing "Command"s? - php

I've tried testing my commands in Laravel 4, as they are significant part of my system, but it seems like the documentation coverage is so poor, that they only explain basic testing of controllers and some models.
In Commands, you can pass arguments via command line to the class and it's received via $this->input property, something I don't know how to emulate.
Whenever I try to run the test for my command, when it expects an argument in "fire" method, I get this error:
Fatal error: Call to a member function getArgument() on a non-object in /var/www/html/project/vendor/laravel/framework/src/Illuminate/Console/Command.php on line 153
Which is logical, there's no argument passed. Is there a way to test this functionality?...
Thanks

Most of it can be done using Symfony Command Tester (since Command is based on Symfony Console), example: http://alexandre-salome.fr/blog/Test-your-commands-in-Symfony2. However this would start to fail if you have to call another artisan command such as $this->call('db:seed'); or etc because this is actually Illuminate\Console\Application specific syntax.
I'm all open if there anyone that have a solution for above scenario.

I have recently made a post about Testing Laravel commands.
If you have a specific part of code that you want to use in multiple commands then you have to move that part of the code somewhere where both commands can use it (Event handler, trait, another class ...) and then in your command reference that code instead of using $this->call('db:seed');

Related

Laravel Artisan & imap_open()

Currently experiencing a weird bug or something I might be overseeing. (Laravel framework)
I have a plain HomeController where I am doing a succesful imap_open() connection. ( I can read out the complete mailbox using the browser )
But once I transport this function to an artisan command, it returns me:
[Symfony\Component\Debug\Exception\FatalThrowableError]
Call to undefined function App\Console\Commands\imap_open()
What am I missing or is this just plain not possible?
You try to be able to call it though Artisan, but you need to add an Artisan function since Artisan can't know what you want to do, please check the documentation section Writing Commands here.
For now you're calling an unknown Artisan function that's why you get this
Your code is currently in the App\Console\Command namespace.
Because you are calling the imap_open function without specifying the namespace, it is looking for a App\Console\Command\imap_open function and can't find it.
Because it's a default php function, the key here is to call it in the global namespace using a \
So make sure you call it like that:
$res = \imap_open(...params);
Also make sure the imap is compiled/enabled for the php cli (as the php.ini for cli is different from the web one)

Calling Laravel Controller-function from within custom artisan-command -> unknown database

I've created a custom artisan-command in order to call a controller-function via commandline (without curl).
Since there seems to be no way in Laravel 4 to directly call the function I'm dispatching a request like this:
public function fire()
{
$request = Request::create('insert', 'GET', array());
Route::dispatch($request)->getContent();
}
When I call the command via command-line it starts showing the HTML-content the controller-function would output:
php artisan myApp:customCommand
<h1>Insert into Database</h1>
<pre>
but then returns this error:
[PDOException]
SQLSTATE[HY000] [1049] Unknown database 'appdatabase'
What can I do about it?
The reason there is no way to directly call the function is because you should never be calling a controller function from your artisan command.
You should refactor the controller's functions into its own class+function, then call that class+function from either your controller or your artisan command.
The problem here was a wrong database-configuration. For some reasons php artisan needs the port-number of the MySQL-server in the hostname of the database-configuration. The web-application itself only works without specifying the port number.
Since that is a more general problem, I created another question regarding that.
Artisan-commands need different hostname in db-config than Laravel itself
Thanks for the feedback regarding the web-application's structure.

Laravel 4 Commands stopped working suddenly

Back from vacation, and when I'm trying to run one of my previously working command:
php artisan list
Throws me:
[root#api2]# php artisan list
{"error":{"type":"Symfony\\Component\\Debug\\Exception\\FatalErrorException","message":"Call to a member function getArgument() on a non-object","file":"\/var\/www\/html\/api2.alariscms.com\/vendor\/laravel\/framework\/src\/Illuminate\/Console\/Command.php","line":153}}
And I seriously cannot understand why this happens. I tried debugging the file that throws an error. It awaits argument:
InputInterface $input but it gets NULL
I don't know what can go wrong on a framework level to stop receiving the required object.
Any ideas where to start debugging from? Or I can reload configurations / update laravel via cmd?
Thanks
This is what I would do:
php artisan dump
composer dump-autoload
check for current Laravel version
and then go to that specific command and check for bug there.
Also, what kind of argument are you trying to pass to command ? Is that object - if so check where is implementation of that class ?

Codeigniter command line not working with database class / driver?

Has anyone else out there had this problem and found a possible solution. Im not getting any error msg's, simply that no data is been returned from the command line when the database class is either loaded automatically or from the controller.
(This is for CodeIgniter Reactor version.)
Update:
Reported this as a bug:
https://bitbucket.org/ellislab/codeigniter-reactor/issue/85/
Im using cURL to run the script for now and not direct php,
so this:
curl http://localhost/~derrick/mywebsite/index.php/task/run
instead of this:
php Users/derrick/sites/mywebsite/index.php task run
for now until the problem has been resolved.

Creating a testing environment with php cli

I want to use php in console mode and create an environment to test my functions.
I do not want to be forced to use a web browser and create a new file each time I want to test a function.
I want to access the function in the console and then it return the result.
How do I do this?
Update:
Perhaps I have explained this badly. I only want to see what result the function's return.
Maybe I have to learn unit testing but for the moment I only want an interactive console which allows me to test all functions one by one.
In my case I have to load the wordpress functions (I know how do it with a regular .php file and then a browser to parse the file) but i don't if it is possible to do it with php from the command line.
I have used phpsh in the past and found it very useful. Once you start it you will need to chdir() to where your files are and then obviously require() any files containing functions you need to test. You can then just test your function calls by typing them into the shell e.g. var_dump(some_function(1, 2));
I guess you've to be more specific what kind of functions exactly. Wordpress does not provide something like that out of the box, most PHP apps won't.
I also think you're calling for trouble here when such apps aren't developed in mind for such environments.
Here's an example trying to call "current_time()" from functions.php and the attempts I had to do just to realize it won't work that way:
php -r 'require "functions.php"; var_dump(current_time("mysql"));'
gives
Fatal error: Call to undefined function apply_filters() in functions.php on line 346
Trying
php -r 'require "functions.php"; require "plugin.php"; var_dump(current_time("mysql"));'
gives
Fatal error: Call to undefined function wp_cache_get() in functions.php on line 351
Trying
php -r 'require "functions.php"; require "plugin.php"; require "cache.php"; var_dump(current_time("mysql"));'
gives
Fatal error: Call to a member function get() on a non-object in cache.php on line 93
Looking at the last error in the source I see
function wp_cache_get($id, $flag = '') {
global $wp_object_cache;
return $wp_object_cache->get($id, $flag);
}
Using global variables makes testing in other environments a PITA if not impossible.
If this is not what you're trying to do, you've to be more specific/detailed in your question.
You are going to want to read up on "Unit Testing" in a generic sense and then try and apply them to PHP.
The framework you are using (if any), the style of code, and the tests you want to run are going to determine the exact methods you need to use. Only by first understanding the concept of Unit Tests and implementing them into your coding best-practices will you be able to make progress in this regard.
How about:
php -a
And if you compile php with readline support it'll be more fancy.

Categories