0

我正在开发一个没有任何框架的网站,我想使用 panther 集成一些测试。

信息

我的文件夹结构

myproject/
├── src/
│   ├── myprojectrootfile-1.php
│   ├── ...
├── Test/
│   ├── E2eTest.php
├── vendor/
├── composer.json
├── composer.lock
├── phpunit.xml.dist

我的 composer.json

{
    "require-dev": {
        "phpunit/phpunit": "9",
        "symfony/panther": "^0.7.1"
    }
}

我的 phpunit.xml.dist

<phpunit>
<extensions>
    <extension class="Symfony\Component\Panther\ServerExtension" />
</extensions>
<php>
    <server name="KERNEL_DIR" value="./Tests/App" />
    <server name="PANTHER_WEB_SERVER_DIR" value="./src/" />
</php>
</phpunit>

我的 E2eTest.php

<?php

namespace App\Tests;

use Symfony\Component\Panther\PantherTestCase;

class E2eTest extends PantherTestCase
{
    public function testMyApp(): void
    {
        $client = static::createPantherClient(); // Your app is automatically started using the built-in web server
        $client->request('GET', '/src/index.php');

        // Use any PHPUnit assertion, including the ones provided by Symfony
        $this->assertPageTitleContains('My Title');
        $this->assertSelectorTextContains('#main', 'My body');
    }
}

问题

当我运行时.\vendor\bin\phpunit .\Tests\E2eTest.php出现错误:

1) App\Tests\E2eTest::testMyApp 错误:调用未定义的方法 App\Tests\E2eTest::assertPageTitleContains()

我不明白为什么。

谢谢你的帮助

4

2 回答 2

0

我知道这有点晚了,但是在最近发布 1.0 之后尝试 Panther 时遇到了同样的问题。我在使用 Slim + JS SPA 前端的应用程序上使用它。

  1. symfony/framework-bundle作为开发依赖安装,需要 4.4+
composer require --dev symfony/framework-bundle
  1. 将以下方法添加到您的测试用例中或将它们放入基本测试用例类中
<?php

namespace Tests;

use Symfony\Component\Panther\PantherTestCase;

abstract class BasePantherTestCase extends PantherTestCase {

  public static function createKernel(array $options = [])
  {
    return null;
  }

  public static function bootKernel(array $options = [])
  {
  }

}
  1. 创建 Panther 客户端时设置基本 uri
$client = static::createPantherClient([
  'external_base_uri' => 'http://url-to-your-test-env:port'
]);

希望能帮助到你 ;)

于 2021-02-13T04:38:48.997 回答
0

这就是我使用独立的 Panther 测试套件(没有 symfony)获得标题的原因。

套餐:

  • phpunit(在我的情况下为 9.5)
  • symfony/panther(在我的例子中是 1.1)

安装软件包。

composer require phpunit/phpunit symfony/panther

然后创建您的测试。

<?php declare(strict_types=1);

use Symfony\Component\Panther\PantherTestCase;

final class MyTests extends PantherTestCase
{
    // @warning baseUri is already in use by Symfony\Component\Panther\PantherTestCas as a static variable
    private $myBaseUri = 'http://localhost:8000/';

    public function testWebsiteHomePage(): void
    {
        // external_base_uri prevents from exception Symfony\Component\Process\Exception\RuntimeException: The provided cwd "vendor/symfony/panther/src/../../../../public" does not exist.
        $client = static::createPantherClient(['external_base_uri' => $this->myBaseUri]);

        // load home page
        $client->request('GET', $this->myBaseUri);
        $crawler = $client->waitFor('html');

        // test home page title
        $title = $client->getTitle();
        $this->assertStringContainsString('MyAppName', $title);

        // you can also retrieve current url
        $url = $client->getCurrentURL();
        $this->assertEquals($this->myBaseUri, $url);

        // or retrieve current source code
        // but it's better practice to use the crawler to inspect the web page content
        $source = $client->getPageSource();
        $this->assertStringContainsString('html', $source);
    }
}

启动一个 Web 服务器,php -S localhost:8000然后用vendor/bin/phpunit tests/MyTests.php.

如果您想了解有关如何使用$client和 的更多信息$crawler,请阅读以下内容:

于 2021-10-14T14:32:24.663 回答