1

我正在尝试使用 PHPUnit 为 10 月 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...
}
4

3 回答 3

0

我们最终使用 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('/'));
}

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

于 2017-08-18T18:24:59.323 回答
0

我不知道为什么,但如果您--process-isolationphpunit通话中添加标志,我认为可能是缓存问题

于 2017-08-17T22:17:47.167 回答
0

不幸的是没有解决方案,只是角度不同。我认为这与 10 月份的“测试”配置(config/testing/cms.php)有关。这将在目录“tests/fixtures/themes/pages/”中查找文件。它在那里找到的 index.htm 文件的 url 参数设置为“/”。这就是为什么 phpunit 会找到它,而不是其他 url。我已经看到了一些针对该问题的建议解决方案,但没有一个对我有用:

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

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

于 2017-09-19T09:56:28.633 回答