3

I've upgraded from SilverStripe 3 to 4 and now my phpUnit tests own't run because they can't find any of my Custom Classes.

There must be something missing from an autoloader or something.

I have a simple test like this

  use SilverStripe\Dev\SapphireTest;

class EntityTest extends SapphireTest
{
    var $Entity;
    function setUp()/* The :void return type declaration that should be here would cause a BC issue */
    {
        parent::setUp(); // TODO: Change the autogenerated stub
        $this->Entity = new \My\API\Client\Model\Entity();
    }

    function testMethods(){

        $this->assertMethodExist($this->Entity,'setName');
    }


    function assertMethodExist($class, $method) {
        $oReflectionClass = new ReflectionClass($class);
        assertThat("method exist", true, $oReflectionClass->hasMethod($method));
    }
}

and when running I get: $ php vendor/phpunit/phpunit/phpunit mysite/tests/EntityTest.php

Fatal error: Class 'SilverStripe\Dev\SapphireTest' not found

wmk
  • 4,598
  • 1
  • 20
  • 37
wild
  • 311
  • 4
  • 11

2 Answers2

3

I ran into a similar issue with SilverStripe 4.1, here is what I found (and resolved).

1) As of 4.1, you need to use --prefer-source instead of --prefer-dist to get the test code. Test code is now omitted from the distributed packages, see https://github.com/silverstripe/silverstripe-framework/issues/7845

2) phpunit must be in require-dev at version ^ 5.7 - I had a different value and this was the cause of the autoload issue.

I've created a test module for reference, see https://github.com/gordonbanderson/travistestmodule

Cheers

Gordon

gordonbanderson
  • 221
  • 2
  • 5
0

You're probably missing the test bootstrapping. SS4 still relies on the SilverStripe class manifest to register available classes (not just PSR-4 autoloaders), so you need to include it. Try either of these:

$ vendor/bin/phpunit --bootstrap vendor/silverstripe/framework/tests/bootstrap.php mysite/tests

or create a phpunit.xml file in your root project:

<phpunit bootstrap="vendor/silverstripe/framework/tests/bootstrap.php" colors="true">
</phpunit>

You may also use the equivalent file from the CMS module instead, but you probably won't see any differences until you start to integrate your testsuite into a CI provider.

scrowler
  • 24,273
  • 9
  • 60
  • 92