简体   繁体   中英

PHPUnit can't find my PHP file, namespaces issue?

Maybe someone can help me out here. I can't get the following PHPUnit test to work.

In C:\\wamp\\www\\stats I have a file Baseball.php

<?php
namespace stats;
class Baseball
{
    /**
    *
    * Add two given values together and divide by 2
    *
    */
    public function calc_avg($ab,$hits)
    {
        if ($ab == 0)
        {
            $avg = "0.000";
        }
        else
        {
            $avg = $hits/$ab;
        }
        return $avg;
    }

    /**
...
...
...

In C:\\wamp\\www\\stats\\Test there is the test file BaseballTest.php

<?php
namespace stats\Test;

use stats\Baseball;

class BaseballTest extends \PHPUnit_Framework_TestCase
{

    public function testCalcAvgEquals() {

        $atbats = 389;
        $hits = 129;
        $baseball = new Baseball();
        $result = $baseball->calc_avg($atbats, $hits);
        $expectedresult = $hits/$atbats;
        $this->assertEquals($expectedresult, $result);

    }


}

It's from a Lynda.com Tutorial and it does work for that guy, but not for me. When I run

>cd C:\wamp\www\stats\
>phpunit

I get the following error:

Fatal error: Class 'stats\Baseball' not found in C:\wamp\www\stats\Test\Baseball
Test.php on line 14

Is this some namespaces issue I ran into here..? I can't figure it out...

Ok, obviously even if you use "use" you still have to include the file...

I added this line to my BaseballTest.php:

include '/Baseball.php';

Now it works.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM