jeudi 16 mars 2017

phpunit getMockBuilder getMock cannot redeclare class

I have these files:

- phpunit.xml
- module.php
-- helper
--- abstract.php
- tests
-- moduleTest.php
-- helper
--- abstract.php

This is my phpunit.xml config:

<?xml version="1.0" encoding="UTF-8"?>
<phpunit
        strict="true"
        bootstrap="./tests/bootstrap.php"
>
    <testsuites>
        <testsuite name="unit">
            <directory>tests</directory>
        </testsuite>
    </testsuites>
    <logging>
        <log type="testdox-html" target="tests/results.html"/>
    </logging>
    <filter>
        <blacklist>
            <directory suffix=".php">vendor</directory>
        </blacklist>
        <whitelist processUncoveredFilesFromWhitelist="true">
            <directory>tests/</directory>
        </whitelist>
    </filter>
</phpunit>

When I run phpunit from the root, I get this message:

Fatal error: Cannot declare class ProcessWire\KeyAgencyGeneral_Helper_Abstract, because the name is already in use in /Users/chris/Sites/processwire/site/modules/KeyAgencyGeneral/Helper/Abstract.php on line 161

Call Stack:
    0.0013     467248   1. {main}() /usr/local/bin/phpunit:0
    0.0797    7512968   2. PHPUnit\TextUI\Command::main() /usr/local/bin/phpunit:465
    0.0798    7513080   3. PHPUnit\TextUI\Command->run() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:136
    0.4198   20795224   4. PHPUnit\TextUI\TestRunner->doRun() phar:///usr/local/bin/phpunit/phpunit/TextUI/Command.php:207
    0.4242   20800768   5. PHPUnit\Framework\TestSuite->run() phar:///usr/local/bin/phpunit/phpunit/TextUI/TestRunner.php:514
    0.4344   20838784   6. PHPUnit\Framework\TestSuite->run() phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php:739
    0.4747   21239232   7. PHPUnit\Framework\TestCase->run() phar:///usr/local/bin/phpunit/phpunit/Framework/TestSuite.php:739
    0.4747   21239232   8. PHPUnit\Framework\TestResult->run() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:867
    0.4749   21239688   9. PHPUnit\Framework\TestCase->runBare() phar:///usr/local/bin/phpunit/phpunit/Framework/TestResult.php:688
    0.4750   21256248  10. PHPUnit\Framework\TestCase->runTest() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:912
    0.4750   21256544  11. ReflectionMethod->invokeArgs() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:1053
    0.4750   21256552  12. KeyAgencyGeneralTest->testGetModuleConfigInputfieldsOnInstance() phar:///usr/local/bin/phpunit/phpunit/Framework/TestCase.php:1053
    0.4750   21256872  13. ReflectionMethod->invoke() /Users/chris/Sites/processwire/site/modules/KeyAgencyGeneral/tests/KeyAgencyGeneralModuleTest.php:314
    0.4750   21256872  14. ProcessWire\KeyAgencyGeneral->getModuleConfigInputfields() /Users/chris/Sites/processwire/site/modules/KeyAgencyGeneral/tests/KeyAgencyGeneralModuleTest.php:314
    0.5113   21842032  15. ProcessWire\Wire->getHelper() /Users/chris/Sites/processwire/site/modules/KeyAgencyGeneral/KeyAgencyGeneral.module.php:545
    0.5113   21842408  16. ProcessWire\Wire->__call() /Users/chris/Sites/processwire/site/modules/KeyAgencyGeneral/KeyAgencyGeneral.module.php:545
    0.5114   21842408  17. ProcessWire\WireHooks->runHooks() /Users/chris/Sites/processwire/wire/core/Wire.php:399
    0.5122   21844144  18. ProcessWire\KeyAgencyGeneral_Model_Observer->onGetHelper() /Users/chris/Sites/processwire/wire/core/WireHooks.php:629

My tests/helper/abstract.php looks like this:

<?php
use PHPUnit\Framework\TestCase;

/**
 * Class KeyAgencyGeneral_Helper_AbstractTest
 */
class KeyAgencyGeneral_Helper_AbstractTest
    extends TestCase
{
    public function testGetModule()
    {
        $mock = $this->getMockBuilder(\ProcessWire\KeyAgencyGeneral_Helper_Abstract::class)
            ->setMethods(null)
            ->getMock()
        ;
//        $reflectionClass = new ReflectionClass($mock);
//        $reflectionMethod = $reflectionClass->getMethod('_getModule');
//
//        $reflectionMethod->setAccessible(true);
//
//        $this->assertInstanceOf(
//            \ProcessWire\KeyAgencyGeneral::class,
//            $reflectionMethod->invoke(
//                $mock
//            )
//        );
    }
}

My helper/abstract.php looks like this:

class KeyAgencyGeneral_Helper_Abstract
    extends KeyAgencyGeneral
{
    [...]
}

I don't know what's going wrong here. The module.php and moduleTest.php look the same (other class names of course) and work fine. When I comment out the mock part there is no problem, but of course, I don't have a mock as well then. So I'm trying to understand what is going wrong when creating the mock.

Aucun commentaire:

Enregistrer un commentaire