Timtendo12's avatar

Error when running laravel's phpunit test

I get an error when running the "test laravel project" action through github actions

image

Running on Laravel 10.10, PHP 8.1.21.

my workflow file:

name: Laravel

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  laravel-tests:

    runs-on: ubuntu-latest

    steps:
    - uses: shivammathur/setup-php@v2
      with:
        php-version: '8.1'
    - uses: actions/checkout@v3
    - name: Copy .env
      run: php -r "file_exists('.env') || copy('.env.example', '.env');"
    - name: Install Dependencies
      run: composer install -q --no-ansi --no-interaction --no-scripts --no-progress --prefer-dist
    - name: Generate key
      run: php artisan key:generate
    - name: Directory Permissions
      run: chmod -R 777 storage bootstrap/cache
    - name: Create Database
      run: |
        mkdir -p database
        touch database/database.sqlite
    - name: Execute tests (Unit and Feature tests) via PHPUnit
      env:
        DB_CONNECTION: sqlite
        DB_DATABASE: database/database.sqlite
      run: vendor/bin/phpunit
0 likes
6 replies
vincent15000's avatar
Expected response status code [200] but received 302.

This doesn't seem to be a bug, but it's just that the test is failing.

Timtendo12's avatar

@vincent15000 I know that. ITs just that this is the default test that comes with Laravel. I expect it to work since its delivered with Laravel

1 like
vincent15000's avatar

@Timtendo12 Is it this one ?

$this->assertTrue(true);

Or this one ?

$response = $this->get('/');

$response->assertStatus(200);
Timtendo12's avatar

@vincent15000 I think its the phpunit line (123) on vendor/laravel/framework/src/Illuminate/Testing/TestResponse.php

    /**
     * Assert that the response has the given status code.
     *
     * @param  int  $status
     * @return $this
     */
    public function assertStatus($status)
    {
        $message = $this->statusMessageWithDetails($status, $actual = $this->getStatusCode());

        PHPUnit::assertSame($actual, $status, $message);

        return $this;
    }
1 like
Timtendo12's avatar

@vincent15000 Sorry for the late response, But your question made me realize what's wrong. My route.php redirects guests to /login, And these redirects have a status code of 302, So it makes sense why it isn't working. I fixed it by changing the route to check from / to /ping. and made it return a simple 200 response. Thanks for the help!

Please or to participate in this conversation.