4

Summary

I have a package that needs to be wrapped by a real laravel app to be tested properly. Unfortunately I cant use the orchestra testbench. However I have setup a Github action for this. But, when running the final step vendor/phpunit/phpunit/phpunit path/to/package im experiencing the following issues:

  • PHPUnit can't resolve certain classes
  • It thinks PHPUnit setUp() is public in my parent class though it is protected.
  • I suspect some issue with caching or delay after push (but I have tested wiping all cache, autoload and config)

Example error

Run vendor/phpunit/phpunit/phpunit packages/Ajthinking/PHPFileManipulator/tests
PHP Fatal error:  Uncaught Error: Class 'PHPFileManipulator\Tests\FileTestCase' not found in /home/runner/work/php-file-manipulator/php-file-manipulator/host/packages/Ajthinking/PHPFileManipulator/tests/Unit/APIDelegationTest.php:10
Stack trace:
#0 /home/runner/work/php-file-manipulator/php-file-manipulator/host/vendor/phpunit/phpunit/src/Util/FileLoader.php(59): include_once()
#1 /home/runner/work/php-file-manipulator/php-file-manipulator/host/vendor/phpunit/phpunit/src/Util/FileLoader.php(47): 

Reviewing my master branch, the requested file FileTestCase is there.

The full action script:

name: Laravel-wrapped-package-test

on: [push]

jobs:
  laravel-tests:
    runs-on: ubuntu-latest
    steps:
    - name: Install host app
      run: composer create-project --prefer-dist laravel/laravel host

    - name: Install package (this repo)
      uses: actions/checkout@v2
      with:
        path: host/packages/Ajthinking/PHPFileManipulator

    - name: Add this package to composer.json repositories
      uses: ajthinking/[email protected]
      with:
        repo_relative_path: 'host/packages/Ajthinking/PHPFileManipulator'

    - name: Require this package
      working-directory: ./host
      run: composer require ajthinking/php-file-manipulator @dev

    - name: Publish things
      working-directory: ./host
      run: php artisan vendor:publish --provider="PHPFileManipulator\ServiceProvider"       

    - name: Run tests
      working-directory: ./host      
      run: vendor/phpunit/phpunit/phpunit packages/Ajthinking/PHPFileManipulator/tests

TestCase

Below is my base test case. But the setUp method is never called. (Previously it was called TestCase as the default laravel provides - it was renamed while debugging)

<?php

namespace PHPFileManipulator\Tests; 

use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class FileTestCase extends BaseTestCase  
{
    protected function setUp() : void
    {
        parent::setUp();
        dd("it will never reach this dd call on github actions!");
    }
}

Usage:

<?php

namespace PHPFileManipulator\Tests\Unit;

use PHPFileManipulator\Tests\FileTestCase;

class StupidTest extends FileTestCase
{
    /** @test */
    public function it_can_run_tests()
    {
        $this->assertTrue(true);
    }    
}

versions

Im using PHP 7.4.2 and PHPUnit 8.5.2 (same as github actions ubuntu-latest) Mimicing the action script on my own machine works fine. I have reviewed CASE on github/local found no differences.

Any ideas, what I am missing?

10
  • 2
    Do you have this file in your app? If not, put it and see if it helps. github.com/laravel/laravel/blob/master/tests/TestCase.php Also, see if you have something like this in your composer.json - "autoload-dev": { "classmap": [ "tests/TestCase.php" ] }, Commented Feb 7, 2020 at 6:00
  • 1
    Can you show us example test class? Commented Feb 7, 2020 at 8:28
  • @QumberRizvi, I have added my base test case to the question. (TestCase was renamed when debugging). Im autoloading "tests/" into namespace "Tests\\" in composer.json Commented Feb 7, 2020 at 12:47
  • 1
    If Laravel is a dependency for your package it should be stated in its composer.json file. Wouldn't be easier to just run composer install from your CI script to install all the necessary dependencies and run vendor/bin/phpunit from there (package root) instead of creating a fresh laravel project, installing your package and running the tests from a subfolder? Commented Feb 10, 2020 at 15:10
  • 1
    @Anders why can't you use orchestra testbench? Commented Feb 10, 2020 at 16:08

1 Answer 1

1

I failed updating the host app composer.json repositories section. That meant requiring the package with @dev flag (taking latest commit) had no effect - instead an outdated package version was pulled from packagagist.

Fixed github workflow:

name: tests

on: [push, pull_request]

jobs:
  laravel-tests:
    runs-on: ubuntu-latest
    steps:
    - name: Install host app
      run: composer create-project --prefer-dist laravel/laravel host

    - name: Checkout the package
      uses: actions/checkout@v2
      with:
        path: host/packages/Ajthinking/PHPFileManipulator

    - name: Add composer.json repository
      working-directory: ./host      
      run: composer config repositories.php-file-manipulator path "/home/runner/work/php-file-manipulator/php-file-manipulator/host/packages/Ajthinking/PHPFileManipulator"

    - name: Require the package
      working-directory: ./host
      run: composer require ajthinking/php-file-manipulator @dev

    - name: Publish things
      working-directory: ./host
      run: php artisan vendor:publish --provider="PHPFileManipulator\ServiceProvider"

    - name: Run tests
      working-directory: ./host      
      run: vendor/phpunit/phpunit/phpunit packages/Ajthinking/PHPFileManipulator/tests
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.