mardi 14 juillet 2020

Route Testing a Laravel Package

I created a package for laravel. I want to get into testing, so I figured this would be a good time. Although, testing a package vs your laravel app seems to be a bit different. I can run a test on a route that just returns a view. But a route that uses a controller never gives me a 200.

Here are my routes...

Route::get('styleguide', '\Vendor\Styleguide\Http\Controllers\StyleguideController@index')->name('styleguide');

Route::get('tester', function () {
    return view('styleguide::layout');
});

Here is my TestCase...

<?php
    
    namespace Vendor\Styleguide\Tests;
    
    use Vendor\Styleguide\Providers\StyleguideServiceProvider;
    
    class TestCase extends \Orchestra\Testbench\TestCase
    {
        protected function getPackageProviders($app)
        {
            return [
                StyleguideServiceProvider::class,
            ];
        }
    
        protected function getPackageAliases($app)
        {
            return [
                'Vendor' => 'Vendor\Styleguide'
            ];
        }
    
        /**
         * Setup the test environment.
         */
        protected function setUp(): void
        {
            parent::setUp();
            // Your code here
        }
    
        // Use annotation @test so that PHPUnit knows about the test
        /** @test */
        public function visit_test_route()
        {
            // Visit /test and see "Test Laravel package isolated" on it
            $response = $this->get('tester');
            $response->assertStatus(200);
            $response->assertSee('Preview');
        }
    
    }

Here is my composer.json...

{
    "name": "vendor/styleguide",
    "autoload": {
        "psr-4": {
            "Vendor\\Styleguide\\": "app"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Vendor\\Styleguide\\Test\\": "tests"
        }
    },
    "require": {},
    "require-dev": {
        "phpunit/phpunit": "^9.2",
        "orchestra/testbench": "^5.3"
    }
}

And I have this in my ServiceProvider for this package...

public function boot()
{
    // Package routes.
    include __DIR__.'/../../routes/routes.php';
}

Again, I can get my /tester link to test. But i can't run it on /styleguide. There's clearly something I don't know about getting my routes to show up. I hope this is enough detail for anyone willing to help. I've searched and searched through examples online. I just can't quite get it.

Directory structure...

packages
  vendor
    styleguide
      app
        Http
          Controllers
            StyleguideController
        Providers
          StyleguideServiceProvider
        View
      tests
        TestCase.php
      composer.json
      phpunit.xml

Aucun commentaire:

Enregistrer un commentaire