samedi 4 août 2018

Fatal Error Cannot declare class because the name is already in use

I am trying to setup phpunit and use it for testing. This is the first time that I have used this so I may be missing something. It looks like through searching that others have come across this issue however the answers have not worked for me. I've tried adjusting namespaces and checking for requires/includes in the class that my test is calling, however none of this has made a difference.

I have a class similar to the below:

<?php

namespace TM470_project\Test\php;

use \vendor\Autoload;

class myClassTest extends \PHPUnit\Framework\TestCase
{
     protected function setUP()
     {
         $temp = $this -> getMockBuilder('\My_project\php\myClassScript')
                       -> setConstructorArgs(
                          array(
                              "key" => "value",
                              "key" => "value",
                              "key" => "value"
                          ))
                       -> getMock();
 }

protected function tearDown()
{
    $temp = null;
}

public function remove()
{

}

When I try to run phpunit the response is as follows:

Fatal error: Cannot declare class myClass, because the name is already in use     in /Library/WebServer/Documents/mysites/My_project/php/myClass.php on line 3

Call Stack:
    0.0003     356720   1. {main}() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit/phpunit:0
    0.0032     727752   2. PHPUnit\TextUI\Command::main() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit/phpunit:53
    0.0032     727864   3. PHPUnit\TextUI\Command->run() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit/src/TextUI/Command.php:148
    0.0171    2257584   4. PHPUnit\TextUI\TestRunner->doRun() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit/src/TextUI/Command.php:195
    0.0230    2410320   5. PHPUnit\Framework\TestSuite->run() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit/src/TextUI/TestRunner.php:546
    0.0276    2417088   6. PHPUnit\Framework\TestCase->run() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit/src/Framework/TestSuite.php:755
    0.0276    2417088   7. PHPUnit\Framework\TestResult->run() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit/src/Framework/TestCase.php:894
    0.0281    2439208   8. PHPUnit\Framework\TestCase->runBare() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit/src/Framework/TestResult.php:698
    0.0283    2455896   9. vendor\AutoLoad\mortgageTest->setUP() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit/src/Framework/TestCase.php:935
    0.0300    2633496  10. PHPUnit\Framework\MockObject\MockBuilder->getMock() /Library/WebServer/Documents/mysites/TM470_Project/Test/php/myTest.php:61
    0.0300    2633496  11. PHPUnit\Framework\MockObject\Generator->getMock() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit-mock-objects/src/MockBuilder.php:118
    0.0301    2633496  12. PHPUnit\Framework\MockObject\Generator->generate() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit-mock-objects/src/Generator.php:189
    0.0301    2633560  13. PHPUnit\Framework\MockObject\Generator->generateMock() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit-mock-objects/src/Generator.php:488
    0.0313    2780728  14. interface_exists() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit-mock-objects/src/Generator.php:657
    0.0313    2780784  15. spl_autoload_call() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit-mock-objects/src/Generator.php:657
    0.0313    2780920  16. Composer\Autoload\ClassLoader->loadClass() /Library/WebServer/Documents/mysites/vendor/phpunit/phpunit-mock-objects/src/Generator.php:657
    0.0314    2781048  17. Composer\Autoload\includeFile() /Library/WebServer/Documents/mysites/vendor/composer/ClassLoader.php:322
    0.0319    2841600  18. include('/Library/WebServer/Documents/mysites/TM470_project/php/mortgage.php') /Library/WebServer/Documents/mysites/vendor/composer/ClassLoader.php:444

Line 3 is the class declaration in myClass.php, so I cannot see why this message is coming up.

Some other posts have mentioned the Autoloader being the issue, however I do not know what to look for. here is my autoloader.php file:

<?php
// autoload.php @generated by Composer
require_once __DIR__ . '/composer/autoload_real.php';
return ComposerAutoloaderInit5d05186b2ab974ea0fca942edb705943::getLoader();

If I remove the test function, then the message disappears I presume because there are then no tests in the class.

My question is this: Please advise, based on the code provided, why this fatal error message is appearing. Also, how can I remove this error i.e. what should I look for in my code?

Aucun commentaire:

Enregistrer un commentaire