doutang1873 2014-10-17 11:15
浏览 123
已采纳

PHPUnit的Composer自动加载失败

I ran into issues using composers psr-4 autoloading feature and PHPUnit. The problems occur on my local machine as well as on TravisCI builds. Unit tests aren't able to find my classes, while normal scripts do.

There is certainly something wrong with any of the configurations, but I cannot figure out what I did wrong. Thanks in advance for your help.

Here's my projects file structure:

  • public/
  • src/
    • http/
      • urlhelper.php
    • lib.php
  • tests/
    • blacktie/
      • http/
        • urlhelper.test.php
      • lib.test.php
    • bootstrap.php
    • testcase.php
  • .travis.yml
  • composer.json
  • init.php
  • phpunit.xml

Here's what my composer.json looks like:

{
    "require": {
        "php": ">=5.4.0",
        "nesbot/carbon": "1.*",
        "ext-pdo": "*",
        "predis/predis": "1.1.*@dev"
    },
    "require-dev": {
        "satooshi/php-coveralls": "dev-master",
        "phpunit/phpunit": ">=3.7"
    },
    "autoload": {
        "psr-4": {
            "BlackTie\\": "src/blacktie"
        }
    }
}

The PHPUnit configuration defines a bootstrap, which includes per require_once a script shared by the main project and the tests to initialise the psr-4 autoloader and common functions: init.php

define('ROOT', __DIR__);

// Composer-Autoloader
require_once "vendor/autoload.php";

// Framework library with common functions
require_once "src/blacktie/lib.php";

Travis' command line says:

PHP Fatal error: Class 'BlackTie\Http\UrlHelper' not found in /home/travis/build/jazzschmidt/black-tie/tests/blacktie/http/urlhelper.test.php on line 38

  • 写回答

2条回答 默认 最新

  • dourui7186 2014-10-18 15:42
    关注

    As you've found out, to make your code work properly on Linux systems, you need to have the correct case for class names.

    The reason it works locally on your mac is that OSX doesn't have a fully case sensitive file system. Although it preserves the case of the file and directories, it resolves differently cased file names to the same storage path e.g.

    $ touch foo.txt
    $ touch Foo.txt
    $ ls -l
    total 0
    -rw-r--r--  1 danack  admin  0 18 Oct 16:32 foo.txt
    $ touch Bar.txt
    $ touch bar.txt
    $ ls -l
    total 0
    -rw-r--r--  1 danack  admin  0 18 Oct 16:32 Bar.txt
    -rw-r--r--  1 danack  admin  0 18 Oct 16:32 foo.txt
    

    It similar considers pre-composed and de-composed versions of characters to be the same. i.e. é (U+00E9) is to e (U+0065) and combining acute accent (U+0301)

    An example of this can be shown more easily through PHP:

    <?php
    
    touch("Am\xC3\xA9lie.txt");
    touch("Am\x65\xCC\x81lie.txt");
    

    Results in a single file called 'Amélie.txt' being generated.

    Although this may seem nuts at first, it means that applications don't need to worry about the decomposition of characters, and you won't suddenly find that one application saves a file with one representation, and then another application saves it as a different representation.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥120 计算机网络的新校区组网设计
  • ¥20 完全没有学习过GAN,看了CSDN的一篇文章,里面有代码但是完全不知道如何操作
  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 海浪数据 南海地区海况数据,波浪数据
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?