十月 CMS PHPUnit 路由测试

October CMS PHPUnit Route Testing

我正在尝试使用 PHPUnit 为 October CMS 插件的自定义路由编写一些测试,但是 运行 遇到了一些错误,导致测试 运行 正确。

当单独 运行 时,每个测试都会通过,但当 运行 作为一个组时,第一个测试将通过,其余测试将失败并出现 500 个错误。失败测试的错误消息是:

in Resolver.php line 44
at HandleExceptions->handleError('8', 'Undefined index: myThemeName',
'/Users/me/src/myProject/vendor/october/rain/src/Halcyon/Datasource/
Resolver.php', '44', array('name' => 'myThemeName')) in Resolver.php line 
44

测试用例如下所示:

class RoutesTest extends PluginTestCase
{
  protected $baseUrl = "/";

  public function setUp() {
    parent::setUp();
    DB::beginTransaction();
 }

  public function tearDown()
  {
    DB::rollBack();
    parent::tearDown();
  }

  public function testRootPath()
  {
    $response = $this->call('GET', '/');
    $this->assertEquals(200, $response->status());
  }

  public function testIntroPath()
  {
    $response = $this->call('GET', '/intro');
    $this->assertEquals(200, $response->status());
  }

  etc...
}

我不知道为什么,但是如果你在 phpunit 调用中添加标志 --process-isolation 就可以工作,我认为可能是缓存问题

我们最终使用 curl 发出实际请求并从中获取响应代码。

protected function getResponseCode($url) {
    $ch = curl_init($this->baseUrl.$url);
    curl_setopt($ch, CURLOPT_HEADER, true);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_TIMEOUT,10);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_exec($ch);
    return curl_getinfo($ch, CURLINFO_HTTP_CODE);
}

public function testRootPath()
{
    $this->assertEquals(200, $this->getResponseCode('/'));
}

现在所有测试都通过了,无需使用隔离进程。

可惜没有办法,换个角度而已。我认为这与十月的 "testing" 配置 (config/testing/cms.php) 有关。 这将在目录 "tests/fixtures/themes/pages/" 中查找文件。它在那里找到的 index.htm 文件将 url 参数设置为“/”。 这就是 phpunit 会找到它,但其他 urls 找不到的原因。 我已经看到针对该问题的几个建议解决方案,但其中 none 对我有用:

  1. 在运行测试之前使用"Config::set('cms.activeTheme', 'myTheme');"。
  2. 将 "pluginsPathLocal" 从 "base_path('tests/fixtures/plugins')" 更改为 config/testing/cms 中的 "base_path('plugins')"。php
  3. 将 "themesPathLocal" 从 "base_path('tests/fixtures/themes')" 更改为 config/testing/cms 中的 "base_path('themes')"。php

如果我能弄清楚如何以正确的方式访问我的主题页面之一,生活就会轻松得多......