1

Actually Iam new to PHP. I am running this from a nearly empty folder (actually following along a Lara-casts tutorial: Design a Fluent API with TDD).

My directory structure looks like

  • src
    • Expression.php
  • tests
    • ExpressionTest.php
  • vendor
  • composer.json
  • composer.lock

Inside composer.json:

{    
    "require-dev": {
        "phpunit/phpunit": "^5.2"
    },
    "autoload": {
        "psr-4": {
            "": "src/"
        }
    }
}

Inside ExpressionTest.php:

class ExpressionTest extends PHPUnit_Framework_TestCase
{
    /** @test */
    public function it_finds_a_string()
    {
        $regex = Expression::make()->find('www');

        $this->assertRegExp($regex, 'www');
    }
}

Inside Expression.php

<?php

class Expression
{

}

I then run composer dump-autoload and run phpunit but I still get:

 "Fatal error: Class 'Expression' not found in 
C:\Users\nobis\code\testing2\tests\ExpressionTest.php on line 8"

Is there something wrong with my syntax? My understanding of Composer is very basic. Thanks in advance.

2 Answers 2

1

PHPUnit does not know about Composer natively, therefore without configuring PHPUnit, it will not know about your autoloader setup.

try running PHPunit with --bootstrap vendor/autoload.php to tell it to load with your autoload file.

If that does not work, check your namespace value in your Composer configuration (i.e. "": "src/" might need to change.)

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks. I also had to specify that PHPUnit was in the folder's vendor folder: ./vendor/bin/phpunit tests --bootstrap vendor/autoload.php But thanks for pointing me in the right direction.
Sorry, I only thought to include the flag portion, not the entire required command.
1

You need to include the autoloader at the top of your test. It is typically at

require __DIR__ . '/vendor/autoload.php';

You can also add a phpunit.xml file to your tell it where the autoloader is then it will run it before every test:

<phpunit
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:noNamespaceSchemaLocation="http://schema.phpunit.de/5.2/phpunit.xsd"
    bootstrap="/path/to/bootstrap.php"

</phpunit>

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.