lstrojny / phpunit-clever-and-smart

Smarter test runner for PHPUnit
170 stars 13 forks source link

Sqlite db file not found when running on Travis CI #35

Open mihaeu opened 9 years ago

mihaeu commented 9 years ago

Hey there and thanks for CaS, it's a little tool I love (and believe it should be part of PHPUnit's core).

I just had some problems getting the listener to work with Travis CI. Usually paths like src/ or build/... resolve in PHPUnit's XML config.

When I configure CaS with a relative path I get the following problem:

Stack trace:
#0 /home/travis/build/mihaeu/movie-manager/vendor/lstrojny/phpunit-clever-and-smart/src/PHPUnit/Runner/CleverAndSmart/Storage/Sqlite3Storage.php(24): SQLite3->__construct('build/.phpunit-...')
#1 [internal function]: PHPUnit\Runner\CleverAndSmart\Storage\Sqlite3Storage->__construct('build/.phpunit-...')
#2 phar:///home/travis/.phpenv/versions/5.4.32/bin/phpunit/phpunit/Util/XML.php(276): ReflectionClass->newInstanceArgs(Array)
#3 phar:///home/travis/.phpenv/versions/5.4.32/bin/phpunit/phpunit/Util/Configuration.php(374): PHPUnit_Util_XML::xmlToVariable(Object(DOMElement))
#4 /home/travis/build/mihaeu/movie-manager/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(722): PHPUnit_Util_Configuration->getListenerConfiguration()
#5 /home/travis/build/mihaeu/mo in /home/travis/build/mihaeu/movie-manager/vendor/lstrojny/phpunit-clever-and-smart/src/PHPUnit/Runner/CleverAndSmart/Storage/Sqlite3Storage.php on line 24

https://travis-ci.org/mihaeu/movie-manager/jobs/37203163#L249

I played around a bit and solved it by extending Sqlite3Storage

<?php

namespace Mihaeu\MovieManager\Tests;

use PHPUnit\Runner\CleverAndSmart\Storage\Sqlite3Storage;

class Sqlite3TestStorage extends Sqlite3Storage
{
    public function __construct($fileName = '.phpunit-cas.db')
    {
        $fileName = realpath(__DIR__.'/../../..').'/'.$fileName;
        if (!file_exists(dirname($fileName))) {
            mkdir(dirname($fileName), 0777, true);
        }
        parent::__construct($fileName);
    }
}

Of course it's not a nice solution, but it works in my case (the path has to be adjusted of course if loaded from a vendor dir). In the case of Travis getcwd() could be used to find the base path as well. Of course using CaD on Travis doesn't make much sense, because there are no repeated test runs, but it would be nice to be able to use the same phpunit.xml

What do you think?

lstrojny commented 9 years ago

There are two options I see: a) we try to be smart about figuring out the path but I expect that to be a long and annoying journey or b) we just try and create the directory where the file will be put. The ladder seems reasonable although not very polite. Opinions?

mihaeu commented 9 years ago

I had a look at the code and saw that the listener has no access to PHPUnit or it's configuration (which never having done anything but testing with PHPUnit, I didn't know; makes sense though ...) itself so figuring out the path would be trial and error and long and annoying journey indeed.

b) really sounds like the best bet. Most build tools will create the build structure you specify without asking for permission or asking you to create it manually so I guess it would be okay to do the same in this case.

The problem came up because I had a build folder on my dev machine (with coverage and other build artifacts), but on a new checkout or on Travis CI that will be created only after the tests and all the listeners ran.

Greetings to Munich (only 55km from where I am)

lstrojny commented 9 years ago

Yeah, let’s go with option b). I might not have time in the next few days, but a pull request is always welcome :smile: