laravel 4 unit testing on windows keeps giving error - windows

I have a Laravel 4 installation right out of the box. I am running Windows 8 64 bit with PHP 5.5, Apache 2.2 through XAMPP.
When I run phpunit I get the standard laravel error page Whoops, looks like something went wrong. and right above it I see PHPUnit 3.8-dev by Sebastian Bergmann. Configuration read from C:\project\phpunit.xml The Xdebug extension is not loaded. No code coverage will be generated.
If I delete file app/tests/ExampleTest.php then phpunit seems to work and I get
PHPUnit 3.8-dev by Sebastian Bergmann.
Configuration read from C:\project\phpunit.xml
The Xdebug extension is not loaded. No code coverage will be generated.
Time: 232 ms, Memory: 2.75Mb
←[30;43mNo tests executed!←[0m
app/tests/ExampleTest.php
<?php
class ExampleTest extends TestCase {
/**
* A basic functional test example.
*
* #return void
*/
public function testBasicExample()
{
$crawler = $this->client->request('GET', '/');
$this->assertTrue($this->client->getResponse()->isOk());
}
}
app/tests/TestCase.php
<?php
class TestCase extends Illuminate\Foundation\Testing\TestCase {
/**
* Creates the application.
*
* #return Symfony\Component\HttpKernel\HttpKernelInterface
*/
public function createApplication()
{
$unitTesting = true;
$testEnvironment = 'testing';
return require __DIR__.'/../../bootstrap/start.php';
}
}

Related

Laravel $this->withoutExceptionHandling(); Not Working

I'm running Laravel 5.8 and this function isn't working for my tests. I've put it in the individual tests as the laracast video shows and i've also put it in the setup method. I'm still getting Failed asserting that 500 matches expected 201 type errors without a stacktrace. Anybody know why this could be?
PHPUnit 7.5.20 by Sebastian Bergmann and contributors
/** #test */
public function test_name(){
$this->withoutExceptionHandling();
$response = $this->post("path/url/");
$this->assertEquals(201, $response->getStatusCode());
}

Load fixtures one time before all phpunit test on symfony 3

I ve got a SF3 application and lot of functionnals tests.
Before each tests we load and purge all fixtures.
Time of all tests are so long.
I would like to load fixtures just one time et truncate after last test.
Is it the good method to improve functionnal tests speed ?
Is there a php method in phpunit which is launched just one time before all tests ? (Because setUpBeforeClass is executed before each test)
An exemple of the setUpBeforeClass method in my test's classes.
class SearchRegisterControllerTest extends WebTestCase
{
/** #var Client $client */
private $client;
protected static $application;
public static function setUpBeforeClass()
{
$kernel = static::createKernel();
$kernel->boot();
$em = $kernel->getContainer()->get('doctrine.orm.entity_manager');
$schemaTool = new SchemaTool($em);
$metadata = $em->getMetadataFactory()->getAllMetadata();
$schemaTool->dropSchema($metadata);
$schemaTool->createSchema($metadata);
/** #var Client $client */
$client = static::createClient();
$em = $client->getContainer()->get('doctrine.orm.entity_manager');
$loader = new Loader();
$loader->loadFromDirectory('src/MyNameSpace/AppBundle/DataFixtures/ORM');
$purger = new ORMPurger();
$executor = new ORMExecutor($em, $purger);
$executor->execute($loader->getFixtures(), true);
}
Thanks in advance.
You could implement a test listener.
tests/StartTestSuiteListener.php
namespace App\Tests;
class StartTestSuite extends \PHPUnit_Framework_BaseTestListener
{
public function startTestSuite(\PHPUnit_Framework_TestSuite $suite)
{
// do initial stuff
}
}
Then enable enable the test listener in your phpunit.xml config:
phpunit.xml
<phpunit
...
>
<listeners>
<listener class="App\Tests\StartTestSuiteListener">
</listener>
</listeners>
[...]
</phpunit>
In the same manner you could implement a endTestSuite (Check for all event listed in the doc)
Hope this help
You can use bash script like this to load fixtures once before all tests.
php bin/console doctrine:database:create --env=test --if-not-exists
php bin/console doctrine:schema:update --force --env=test --complete
php bin/console doctrine:fixtures:load --fixtures=tests/fixtures/api --env=test --no-interaction
php vendor/bin/phpunit tests/Functional
Keep in mind that your test will not be executed within isolated environments with fresh data and thus will interfere with each other.

Check if input is from console

I want to share a variable of my views with:
class AppServiceProvider extends ServiceProvider
{
/**
* Bootstrap any application services.
*
* #return void
*/
public function boot()
{
\Schema::defaultStringLength(191);
$customers = Customer::get();
\View::share('customers', $customers);
}
}
it works as expected, but when I want to migrate my tables via artisan it throws an error, that the table for customers was not found because it is checked BEFORE the migration starts. So I need something like
if(!artisan_request) {
//request to laravel is via web and not artisan
}
But I haven't found anything in the documentation.
You can check if you are running in the console by using
app()->runningInConsole()
Underneath that, all it does is check the interface type
return php_sapi_name() == 'cli' || php_sapi_name() == 'phpdbg'
You can find more on the PHP Docs site
To detect whether the app is running in console, you can do something like this:
use Illuminate\Support\Facades\App;
if(App::runningInConsole())
{
// app is running in console
}
See, illuminate/Foundation/Application.php:520

How to Run Laravel Database Seeder from PHPUnit Test setUp?

I am trying to recreate the database before each test in some PHPUnit test cases. I am using Laravel 5.3. Here is TestCase:
class CourseTypesTest extends TestCase
{
public function setUp()
{
parent::setUp();
Artisan::call('migrate');
Artisan::call('db:seed', ['--class' => 'TestDatabaseSeeder ', '--database' => 'testing']);
}
/**
* A basic functional test example.
*
* #return void
*/
public function test_list_course_types()
{
$httpRequest = $this->json('GET', '/api/course-types');
$httpRequest->assertResponseOk();
$httpRequest->seeJson();
}
public function tearDown()
{
Artisan::call('migrate:reset');
parent::tearDown();
}
}
Running phpunit fails with error:
$ phpunit PHPUnit 5.7.5 by Sebastian Bergmann and contributors.
E 1 /
1 (100%)
Time: 2.19 seconds, Memory: 12.00MB
There was 1 error:
1) CourseTypesTest::test_list_course_types ReflectionException: Class
TestDatabaseSeeder does not exist
D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Container\Container.php:749
D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Container\Container.php:644
D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Foundation\Application.php:709
D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Database\Console\Seeds\SeedCommand.php:74
D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Database\Console\Seeds\SeedCommand.php:63
D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Database\Eloquent\Model.php:2292
D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Database\Console\Seeds\SeedCommand.php:64
D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Container\Container.php:508
D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Console\Command.php:169
D:\www\learn-laravel\my-folder-api\vendor\symfony\console\Command\Command.php:254
D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Console\Command.php:155
D:\www\learn-laravel\my-folder-api\vendor\symfony\console\Application.php:821
D:\www\learn-laravel\my-folder-api\vendor\symfony\console\Application.php:187
D:\www\learn-laravel\my-folder-api\vendor\symfony\console\Application.php:118
D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Console\Application.php:107
D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Foundation\Console\Kernel.php:218
D:\www\learn-laravel\my-folder-api\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php:237
D:\www\learn-laravel\my-folder-api\tests\rest\CourseTypesTest.php:17
ERRORS! Tests: 1, Assertions: 0, Errors: 1.
but this class exists:
Since version 5.8 you can do:
// Run the DatabaseSeeder...
$this->seed();
// Run a single seeder...
$this->seed(OrderStatusesTableSeeder::class);
Take a look at the documentation
The DatabaseSeeder can be instantiated on its own, and its call method is public.
All you need to do in your CourseTypesTest class would be
(new DatabaseSeeder())->call(TestDatabaseSeeder::class);
Or you can make use of Laravel's app helper as follow
app(DatabaseSeeder::class)->call(TestDatabaseSeeder::class);
The problem is empty space in your --class argument. If you take close look at array '--class' => 'TestDatabaseSeeder ' there is space in the end ... this is the problem. Change it to '--class' => 'TestDatabaseSeeder' and it should work fine.
You can try this way. You can execute this command when you run your test.

Why would the example test in Laravel fail for redirect?

Please explain why Laravel 4's example test fails for me.
My "/" route redirects to "login" in my routes.php file.
(Oh, I've RTFMed and scoured several sites, blogs, and tuts.)
-sh-3.2$ phpunit
PHPUnit 4.0.7 by Sebastian Bergmann.
Configuration read from /home/dev/phpunit.xml
The Xdebug extension is not loaded. No code coverage will be generated.
F
Time: 83 ms, Memory: 13.50Mb
There was 1 failure:
1) ExampleTest::testBasicExample
Failed asserting that false is true.
/home/dev/app/tests/ExampleTest.php:14
FAILURES!
Tests: 1, Assertions: 1, Failures: 1.
-sh-3.2$
Here is the code from ExampleTest.php
<?php
class ExampleTest extends TestCase {
/**
* A basic functional test example.
*
* #return void
*/
public function testBasicExample()
{
$crawler = $this->client->request('GET', '/');
$this->assertTrue($this->client->getResponse()->isOk());
}
}
The test is failing because you're getting 302 status code instead of 200 (since there is a redirection that wasn't in the default routes.php file).
If you want to test redirection, then Laravel provides several methods just for that.
If you're using named routes:
$this->assertRedirectedToRoute('login');
If not:
$this->assertRedirectedTo('login');
For testing response status codes:
$this->assertResponseStatus(302);

Resources