nothingworksinc / ticketbeast

Back to the lesson videos:
https://course.testdrivenlaravel.com/lessons
552 stars 11 forks source link

DatabaseMigration not working for Laravel 5.4 (1.3 - Getting to Green) #26

Closed munhing closed 7 years ago

munhing commented 7 years ago

Hi Adam,

I was following your video, 1.3 Getting to Green, but i first encounter the below:

1) Tests\Feature\ViewConcertListingTest::user_can_view_a_concert_listing
Error: Call to undefined method Tests\Feature\ViewConcertListingTest::visit()

but i resolved it by installing the package from laravel\browser-kit-testing and modified the BaseTestCase file as follows:

<?php

namespace Tests;

use Laravel\BrowserKitTesting\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
    // public $baseUrl = 'http://localhost';
}

However, i receive a new error:

1) Tests\Feature\ViewConcertListingTest::user_can_view_a_concert_listing
Illuminate\Database\QueryException: SQLSTATE[HY000]: General error: 1 no such table: concerts (SQL: insert into "concerts" ("title", "date", "ticket_price", "additional_information", "updated_at", "created_at") values (The Red Chord, 2016-12-13 20:00:00, 3250, For tickets, call (555) 555-5555., 2017-01-30 10:11:53, 2017-01-30 10:11:53))

What should i do?

munhing commented 7 years ago

I should be clear that earlier before installer laravel/browser-kit-testing, the DatabaseMigration is working fine. Once i did the modification, the SQLSTATE[HY000] error came back again.

Below is the ViewConcertListingTest.php code for your information:

<?php

namespace Tests\Feature;

use Tests\TestCase;
use App\Concert;
use Carbon\Carbon;
use Illuminate\Foundation\Testing\WithoutMiddleware;
use Illuminate\Foundation\Testing\DatabaseMigrations;
use Illuminate\Foundation\Testing\DatabaseTransactions;

class ViewConcertListingTest extends TestCase
{
    use DatabaseMigrations;

    /** @test */
    function user_can_view_a_concert_listing()
    {
        // Arrange
        $concert = Concert::create([
            'title' => 'The Red Chord',
            'date' => Carbon::parse('December 13, 2016 8:00pm'),
            'ticket_price' => 3250,
            'additional_information' => 'For tickets, call (555) 555-5555.'
        ]);

        // Act
        $this->visit('/concerts/' . $concert->id);

        // Assert
        $this->see('The Red Chord');
        $this->see('December 13, 2016');
        $this->see('8:00pm');
        $this->see('32.50');
        $this->see('For tickets, call (555) 555-5555.');

    }
}
flyingearl commented 7 years ago

Hi @munhing,

Have you read the upgrade notes on Laravel's website? The link above will take you to the upgrade notes and near the bottom it describes how to use Laravel 5.3 testing within Laravel 5.4. I haven't tested it myself yet but fingers crossed this solves your issue.

ThibaudDauce commented 7 years ago

Hi @munhing,

The new BrowserKit testing sets up traits only if there is in the namespace Laravel\BrowserKitTesting. You need to import Laravel\BrowserKitTesting\DatabaseMigrations instead of the old Illuminate\Foundation\Testing\DatabaseMigrations.

JuanRangel commented 7 years ago

Hey @munhing,

Yesterday I upgraded an app from 5.3 to 5.4 and ran into a similar issue.

You should have the following files:

TestCase.php

<?php

namespace Tests;

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
}

CreatesApplication.php

<?php
namespace Tests;

use Illuminate\Contracts\Console\Kernel;

trait CreatesApplication
{
    /**
     * Creates the application.
     *
     * @return \Illuminate\Foundation\Application
     */
    public function createApplication()
    {
        $app = require __DIR__ . '/../bootstrap/app.php';
        $app->make(Kernel::class)->bootstrap();

        return $app;
    }
}

BrowserKitTesting.php

<?php

use Illuminate\Contracts\Console\Kernel;
use Laravel\BrowserKitTesting\TestCase as BaseTestCase;

abstract class BrowserKitTest extends BaseTestCase
{
    /**
     * The base URL of the application.
     *
     * @var string
     */
    public $baseUrl = 'http://localhost';

    /**
     * Creates the application.
     *
     * @return \Illuminate\Foundation\Application
     */
    public function createApplication()
    {
        $app = require __DIR__.'/../bootstrap/app.php';

        $app->make(Kernel::class)->bootstrap();

        return $app;
    }
}

Finally, any tests that you want to use the 5.3 way of testing should extend BrowserKitTest

<?php

use Illuminate\Foundation\Testing\DatabaseMigrations;

class OldTest extends BrowserKitTest
{
    use DatabaseMigrations;

    /**
     * @test
     */
    public function admin_can_view_a_list_of_clients()
    {
        $this->visit('/');

        $this->see('Laravel');
    }
}

Lastly, if you are getting a Class not found error, be sure and properly setup the psr-4 autoload in composer.json file.

munhing commented 7 years ago

Hi all,

Thanks for pointing me to the right direction. I managed to get it working already. @flyingearl, thanks for pointing me to the right resources. I followed the instructions in the Testing section.

@ThibaudDauce, that did the trick by importing Laravel\BrowserKitTesting\DatabaseMigrations in the ViewConcertListingTest.php

@JuanRangel , thanks for the detailed file listing.

adamwathan commented 7 years ago

From the sounds of it, a couple changes were pushed to 5.4 recently to try and fix these issues.

Run composer update, and with any luck you should be able to still use the normal Illuminate\Foundation\Testing\DatabaseMigrations trait in your test.

munhing commented 7 years ago

Hi Adam,

Unfortunately composer update did not resolve the issue. I still had to swap Illuminate\Foundation\Testing\DatabaseMigrations with Laravel\BrowserKitTesting\DatabaseMigrations to make it work.

Since it is working now, it is fine with me. Thanks.