Code coverage for laravel dusk - laravel

Is there any way to get code coverage when running Laravel Dusk?
I know it runs browser tests so it's not scrutinizing code, but is there a way to add a listener to check what code is covered? I did not see anything on this subject now.

Conceptually, you need to bootstrap all your requests w/ PHP Unit's code coverage tools.
You can do this with phpunit libraries directly, or via xdebug's coverage tools (which use phpunit).
From this sample gist that I found, you can start coverage tools based on a couple of _GET parameters passed via the Dusk test.
public function testBasicExample()
{
$this->browse(function (Browser $browser) {
$browser->visit(route('test', [
'test_name' => 'testBasicExample',
'coverage_dir' => '/app/Http'
]))->assertSee('test');
});
}
The code that does the work is two parts
1. Start collecting based on parameters:
$test_name = $_GET['test_name'];
require __DIR__ . '/../vendor/autoload.php';
$current_dir = __DIR__;
$coverage = new SebastianBergmann\CodeCoverage\CodeCoverage;
$filter = $coverage->filter();
$filter->addDirectoryToWhitelist(
$current_dir . '/..' . ((isset($_GET['coverage_dir']) && $_GET['coverage_dir'])
? $_GET['coverage_dir']
: '/app')
);
$coverage->start($test_name);
And 2 end collecting and output:
function end_coverage()
{
global $test_name;
global $coverage;
global $filter;
global $current_dir;
$coverageName = $current_dir . '/coverages/coverage-' . $test_name . '-' . microtime(true);
try {
$coverage->stop();
$writer = new \SebastianBergmann\CodeCoverage\Report\Html\Facade;
$writer->process($coverage, $current_dir . '/../public/report/' . $test_name);
$writer = new SebastianBergmann\CodeCoverage\Report\PHP();
} catch (Exception $ex) {
file_put_contents($coverageName . '.ex', $ex);
}
}
The end collection is called using a clever little trick where the class coverage_dumper has just a destructor, which gets called automatically when php ends the process.
The code itself can use a bit of tidy up as far as output paths, and variables go, but from a concept, it should work.

Dusk is using Browsers to run tests and the Browser can't see the PHP code that's being executed.
The only way that I see to achieve code coverage with Dusk is to create a option in php artisan serve that would be possible to count and create the coverage file.

Related

Laravel: Show output of a console command inside a migration?

I created a command to do some data manipulation on a very large database table and as it takes fair enough time to complete, i took the benefits of progress bar plus echoing some information on the console.
to automate stuff and reduce human errors, i want to call my command inside a laravel migration using programmatically-executing-commands style and it works but the problem is it wont print any output from corresponding command inside the console
i think i should pass the current Output-buffer that artisan:migrate is using to the Artisan::call function to make it work but had no luck to access it inside the migration
any suggestions?
Expanding on #ettdro's answer, the Artisan::call method has the following signature:
Artisan::call(string $command, array $parameters = [], $outputBuffer = null);
As you can see, the method accepts an output buffer as its 3rd argument. You can pass that output buffer to the method and the command logs will show up on the console.
Here's an example:
<?php
use App\Console\Commands\YourConsoleCommand;
use Illuminate\Database\Migrations\Migration;
use Symfony\Component\Console\Output\ConsoleOutput;
class SomeDbMigration extends Migration
{
public function up()
{
$output = new ConsoleOutput();
Artisan::call(YourConsoleCommand::class, ['--some-option' => true], $output);
}
public function down()
{
$output = new ConsoleOutput();
Artisan::call(YourConsoleCommand::class, ['--some-option' => false], $output);
}
}
You could use ConsoleOutput provided by Symfony to print out in the console after calling Artisan command. Make sure to use it in your desired .php file like so use Symfony\Component\Console\Output\ConsoleOutput;.
You could have something like this:
$output = new ConsoleOutput();
$exitCode = Artisan::call('your call');
if ($exitCode == -1)
$output->writeln("<bg=red;options=bold>Error occured while migration rollback " . "Exit code: " . $exitCode ."</>");
else {
$output->writeln("<bg=blue;options=bold>Rollbacked successfully! Exit code: " . $exitCode ."</>");
}
See in my example you can also add colors to your text, that could be useful to have better visuals on errors and success, see more at this link: https://symfony.com/search?q=ConsoleOutput

How to test a route in Laravel that uses both `Storage::put()` and `Storage::temporaryUrl()`?

I have a route in Laravel 7 that saves a file to a S3 disk and returns a temporary URL to it. Simplified the code looks like this:
Storage::disk('s3')->put('image.jpg', $file);
return Storage::disk('s3')->temporaryUrl('image.jpg');
I want to write a test for that route. This is normally straightforward with Laravel. I mock the storage with Storage::fake('s3') and assert the file creation with Storage::disk('s3')->assertExists('image.jpg').
The fake storage does not support Storage::temporaryUrl(). If trying to use that method it throws the following error:
This driver does not support creating temporary URLs.
A common work-a-round is to use Laravel's low level mocking API like this:
Storage::shouldReceive('temporaryUrl')
->once()
->andReturn('http://examples.com/a-temporary-url');
This solution is recommended in a LaraCasts thread and a GitHub issue about that limitation of Storage::fake().
Is there any way I can combine that two approaches to test a route that does both?
I would like to avoid reimplementing Storage::fake(). Also, I would like to avoid adding a check into the production code to not call Storage::temporaryUrl() if the environment is testing. The latter one is another work-a-round proposed in the LaraCasts thread already mentioned above.
I had the same problem and came up with the following solution:
$fakeFilesystem = Storage::fake('somediskname');
$proxyMockedFakeFilesystem = Mockery::mock($fakeFilesystem);
$proxyMockedFakeFilesystem->shouldReceive('temporaryUrl')
->andReturn('http://some-signed-url.test');
Storage::set('somediskname', $proxyMockedFakeFilesystem);
Now Storage::disk('somediskname')->temporaryUrl('somefile.png', now()->addMinutes(20)) returns http://some-signed-url.test and I can actually store files in the temporary filesystem that Storage::fake() provides without any further changes.
Re #abenevaut answer above, and the problems experienced in the comments - the call to Storage::disk() also needs mocking - something like:
Storage::fake('s3');
Storage::shouldReceive('disk')
->andReturn(
new class()
{
public function temporaryUrl($path)
{
return 'https://mock-aws.com/' . $path;
}
}
);
$expectedUrl = Storage::disk('s3')->temporaryUrl(
'some-path',
now()->addMinutes(5)
);
$this->assertEquals('https://mock-aws.com/some-path', $expectedUrl);
You can follow this article https://laravel-news.com/testing-file-uploads-with-laravel, and mix it with your needs like follow; Mocks seem cumulative:
<?php
namespace Tests\Feature;
use Tests\TestCase;
use Illuminate\Http\UploadedFile;
use Illuminate\Support\Facades\Storage;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;
class ExampleTest extends TestCase
{
public function testAvatarUpload()
{
$temporaryUrl = 'http://examples.com/a-temporary-url';
Storage::fake('avatars');
/*
* >>> Your Specific Asserts HERE
*/
Storage::shouldReceive('temporaryUrl')
->once()
->andReturn($temporaryUrl);
$response = $this->json('POST', '/avatar', [
'avatar' => UploadedFile::fake()->image('avatar.jpg')
]);
$this->assertContains($response, $temporaryUrl);
// Assert the file was stored...
Storage::disk('avatars')->assertExists('avatar.jpg');
// Assert a file does not exist...
Storage::disk('avatars')->assertMissing('missing.jpg');
}
}
Another exemple for console feature tests:
command : https://github.com/abenevaut/pokemon-friends.com/blob/1.1.3/app/Console/Commands/PushFileToAwsCommand.php
test : https://github.com/abenevaut/pokemon-friends.com/blob/1.1.6/tests/Feature/Console/Files/PushFileToCloudCommandTest.php

JApplicationCli Cronjob

I have the following script:
// Initialize Joomla framework
const _JEXEC = 1;
// Load system defines
if (file_exists(dirname(__DIR__) . '/defines.php'))
{
require_once dirname(__DIR__) . '/defines.php';
}
if (!defined('_JDEFINES'))
{
define('JPATH_BASE', dirname(__DIR__));
require_once JPATH_BASE . '/includes/defines.php';
}
// Get the framework.
require_once JPATH_LIBRARIES . '/import.legacy.php';
// Bootstrap the CMS libraries.
require_once JPATH_LIBRARIES . '/cms.php';
/**
* Cron job to trash expired cache data.
*
* #since 2.5
*/
class DoCron extends JApplicationCli
{
public function doExecute()
{
echo 'ok3';
$this->out('Fetching updates...');
}
}
JApplicationCli::getInstance('DoCron')->execute();
I have added this in CPanel with a Cronjob and get the results of excecution by e-mail.
Now I hoped for an e-mail with 'ok3' or 'Fetching updates...' but none of that all. I do get an e-mail but it is an reference to php excecution.
When I add an 'echo ok' tag right before:
JApplicationCli::getInstance('DoCron')->execute();
I get that 'ok' as a result in the e-mail.
Any thoughts on what goes wrong here? The script is based on general scripts coming with joomla 3.6.5. Those scripts also give no result.
I had the same problem. It turns out that the default php binary for most hosts is the PHP CGI. Running under PHP CGI results in no stdout, stderr and stdin which is why you are not seeing the correct output.
Instead, check your CPanel documentation and look for the CLI version of PHP. It is most likely called php-cli. For example, I had to run a cron job for a Joomla CLI app and found the following to work:
/usr/bin/php-cli /path/to/joomla/cli/my-cli -a b -c d --verbose

Laravel 4 SQL log / console

Is there something similar in Laravel that allows you to see the actual SQL being executed?
In Rails, for example, you can see the SQL in console. In Django you have a toolbar.
Is there something like that in Laravel 4?
To clarify: My question is how to do it without code. Is there something that is built-in in Laravel that does not require me to write code in app?
UPDATE: Preferably I'd like to see CLI queries as well (for example php artisan migrate)
If you are using Laravel 4, use this:
$queries = DB::getQueryLog();
$last_query = end($queries);
I do this in Laravel 4.
Just set it once in app/start/global.php or anywhere but make sure it is loaded and then it will start logging all your SQL queries.
Event::listen("illuminate.query", function($query, $bindings, $time, $name){
\Log::sql($query."\n");
\Log::sql(json_encode($bindings)."\n");
});
Here is a quick Javascript snippet you can throw onto your master page template.
As long as it's included, all queries will be output to your browser's Javascript Console.
It prints them in an easily readable list, making it simple to browse around your site and see what queries are executing on each page.
When you're done debugging, just remove it from your template.
<script type="text/javascript">
var queries = {{ json_encode(DB::getQueryLog()) }};
console.log('/****************************** Database Queries ******************************/');
console.log(' ');
$.each(queries, function(id, query) {
console.log(' ' + query.time + ' | ' + query.query + ' | ' + query.bindings[0]);
});
console.log(' ');
console.log('/****************************** End Queries ***********************************/');
</script>
There is a Composer package for that: https://packagist.org/packages/loic-sharma/profiler
It will give you a toolbar at the bottom with SQL queries, log messages, etc. Make sure you set debug to true in your configuration.
Here's another nice debugging option for Laravel 4:
https://github.com/barryvdh/laravel-debugbar
I came up with a really simple way (if you are using php artisan serve and PHP 5.4) - add this to app/start/local.php:
DB::listen(function($sql, $bindings, $time)
{
file_put_contents('php://stderr', "[SQL] {$sql} in {$time} s\n" .
" bindinds: ".json_encode($bindings)."\n");
});
but hoping to find a more official solution.
This will print SQL statements like this:
[SQL] select 1 in 0.06s
This code is directly taken form other source but i wanted to make it easy for you as follow it worked for me on PHPStorm using my terminal window i was able to see a complete log but ,after login there was some Sentry thing.
1.add
'log'=>true
inside your config/database.php and below the place ur database name ex.mysql
then add below code toroutes.php above all no under any route configuration , since u can make that under a give route configuration but , u only see when that route is called.
to see this output /goto / app/storage/log/somelogfile.log
if (Config::get('database.log', false))
{
Event::listen('illuminate.query', function($query, $bindings, $time, $name)
{
$data = compact('bindings', 'time', 'name');
// Format binding data for sql insertion
foreach ($bindings as $i => $binding)
{
if ($binding instanceof \DateTime)
{
$bindings[$i] = $binding->format('\'Y-m-d H:i:s\'');
}
else if (is_string($binding))
{
$bindings[$i] = "'$binding'";
}
}
// Insert bindings into query
$query = str_replace(array('%', '?'), array('%%', '%s'), $query);
$query = vsprintf($query, $bindings);
Log::info($query, $data);
});
}
Dont forget to make break point .... or ping me :)
In QueryBuilder instance there is a method toSql().
echo DB::table('employees')->toSql()
would return:
select * from `employees`
This is the easiest method to shows the queries.

How to use Laravel 4 profiler correctly

I'm trying to improve the performance of some code in my site and found this profiler: https://github.com/loic-sharma/profiler
I've followed the guidance on the site and included the following in one of the sites controllers:
public function getTest() {
$logger = new Profiler\Logger\Logger;
$profiler = new Profiler\Profiler($logger);
$profiler->startTimer('testLogging');
$data = Article::select(array(
'articles.id',
'articles.article_date',
'articles.image_link',
'articles.headline',
'articles.category'
)) ->get()
->toArray();
var_dump($data);
$profiler->endTimer('testLogging');
Log::info('Hello World!');
echo $profiler;
In the browser I get the expected results and can see the profiler bar at the bottom.
I have one problem: in this basic test the profiler bar, when clicked doesn't stay open so I'm unable to view the logs etc. I'm not sure why or how to go about fixing. THe pane opens then closes again immediately.
If I remove the final echo it works correctly.
I can't though seem to see the timer 'testLogging' in the toolbar.
Have I misunderstood a concept here?
How can I time specific functions in my code and display the results?
Thanks
To use the profiler(loic-sharma/profiler) correctly in Laravel 4 it does not require you the create an instance of the objects for example.
$logger = new Profiler\Logger\Logger;
$profiler = new Profiler\Profiler($logger);
Laravel has these beautiful things called Facades (http://laravel.com/docs/facades), the profiler implements them so you can call the Profiler and Log like this:
public function getTest() {
Profiler::startTimer('testLogging');
$data = Article::select(array(
'articles.id',
'articles.article_date',
'articles.image_link',
'articles.headline',
'articles.category'
)) ->get()
->toArray();
var_dump($data);
Profiler::endTimer('testLogging');
Log::info('Hello World!');
}
This does not require you to echo the $profiler, all output will be displayed in the profiler bar in the browser automatically.
Notice the :: now after Profiler this usually means you are using the facade, it is important you understand that the facade and the $profiler are completely different entities.
If you have not yet installed the facades and or service provider do the following:
First you have to install the package with composer, make sure you
have run composer update after adding it in your composer.json in
"require".- you have already done this.
Next add 'Profiler\ProfilerServiceProvider', to the list of service
providers in app/config/app.php
Next add 'Profiler' => 'Profiler\Facades\Profiler', to the list of
class aliases in app/config/app.php
Then in the console run php artisan config:publish
loic-sharma/profiler
After you have complete that the amended code above should work perfectly.
Just to clarify what you did wrong, you created a new Instance of the profiler with new Profiler\Logger\Logger; if you already had the facades set up the profiler bar would be displayed (echoed) to the browser already so when you echo $profiler; you now have two profilers in your browser causing the open close issue, and when you don't echo $profiler the bar is still displayed because it not the one you created thus not showing your output correctly.
If you still want to use your own instance of the profiler:
remove 'Profiler\ProfilerServiceProvider', from the list of service
providers in app/config/app.php
remove 'Profiler' => 'Profiler\Facades\Profiler', from the list of
class aliases in app/config/app.php
Then in the console run php artisan dump-autoload
Then this will work:
public function getTest() {
$logger = new Profiler\Logger\Logger;
$profiler = new Profiler\Profiler($logger);
$profiler->startTimer('testLogging');
$data = Article::select(array(
'articles.id',
'articles.article_date',
'articles.image_link',
'articles.headline',
'articles.category'
)) ->get()
->toArray();
$logger->debug(var_dump($data));
$profiler->endTimer('testLogging');
$logger->info('Hello World!');
echo $profiler;
}

Resources