Codeception/AspectMock Parent class not found by locator

后端 未结 2 899
我寻月下人不归
我寻月下人不归 2021-02-19 16:41

I have a problem with Codeception/AspectMock. When using custom autoloader and try to create an instance of a class which has parent form the same custom namespace I have this

相关标签:
2条回答
  • 2021-02-19 17:11

    The topic starter has already got an answer on GitHub.

    In order to use custom autoloader you should re-init ReflectionEngine with composite class locator that will be able to locate your classes or you can use CallableLocator with closure for resolving paths.

    Or, even better you could switch your code base to the PSR0/PSR-4

    For example:

    $kernel->loadFile(__DIR__ . '/autoload.php'); // custom autoloader
    
    \Go\ParserReflection\ReflectionEngine::init(
        new class implements \Go\ParserReflection\LocatorInterface {
            public function locateClass($className) {
                return (new ReflectionClass($className))->getFileName();
            }
         }
    );
    
    $b = new \lib\B(); // here you go
    
    0 讨论(0)
  • 2021-02-19 17:18

    If you can easily do a find and replace on your codebase, maybe you could refactor your code to PSR-4 autoloading standards and do away with the need for a custom autoloader altogether.

    This is the spec https://www.php-fig.org/psr/psr-4/. I'll try and explain it as simply as possible.

    Imagine changing your lowercase namespace lib to Lib, and setting that namespace to the src/ directory in your composer.json:

    "autoload": {
        "psr-4": {
          "Lib\\": "src/"
        }
    }
    

    After setting that, run composer dumpautoload. Then all you need to do is search and replace namespace lib;, replacing with namespace Lib;.

    An example class located in src/Form.php would have namespace Lib; at the top, followed by class Form.

    <?php 
    
    namepace Lib;
    
    class Form
    {
      // code
    }
    

    Namespaces use the folder naming convention. All classes directly in src/ have namespace Lib;. If there are subdirectories, the directory name becomes part of the namespace. For example a file in src/Form/Field/Text.php would have namespace Lib\Form\Field; class Text {}.

    <?php 
    
    namepace Lib\Form\Field;
    
    class Text
    {
      // code
    }
    

    You can see the full convention in the link above, but the general rule is make any folders begin with a capital letter, as with your classname, and the autoloader should be able to find all of your classes.

    This is probably the best practice solution for you, and again as I said, only requires a little bit of file renaming and namespace tweaking. Good luck!

    0 讨论(0)
提交回复
热议问题