定义自定义 PHP 类 以在 CodeCeption 测试中使用

Define custom PHP classes to use in CodeCeption tests

我是 CodeCeption 的新手,我已经开始使用一些简单的 API 测试用例。我想创建自己的 classes 以便在多个文件中管理我的方法。我在目录 _support 中写了一个名为 MyClass.php 的新 PHP 文件,其中有一个名为 MyClass 的 class。

<?php
class MyClass extends \Codeception\Actor
{
    use _generated\ApiTesterActions;
    public  function  SayHi($name) {
        $respBody = [
            'hi' => 'hi' + $name
        ];
        return json_decode($respBody, true) ;
    }
}

我在文件 codeception.yml:

的依赖项中添加了这个 class
modules:
    enabled:
        - MyClass

我还在 api.suite.yml 中添加了 MyClass 作为 actor:

actor: MyClass

我试图构建项目以便在我的测试中访问提到的 class,但找不到模块:

zeinab@Zeinab:~/PhpstormProjects/api-testing$ php vendor/bin/codecept build
Building Actor classes for suites: acceptance, api, functional, unit

In ModuleContainer.php line 80:

  Module MyClass could not be found and loaded  


build

有什么我遗漏的吗?

我之前在文件 ApiTester.php 的 class ApiTester 中添加了这个模块并且工作正常;我可以构建项目并在测试中使用该方法。

编辑 1:

我已经根据@Alexey 和@Naktibalda 帮助说明更改了我的代码和配置。

现在这是我的文件结构:

test
|_ src
   |_ MyClass.php
|_ tests
   |_ ...
   |_ mytests
      |_ MyCest.php
   |_ ...
   |_ mytests.suite.yml
   |_ ...
|_ vendor
|_ codeception.yml
|_ composer.json
|_ composer.lock

这是我的 composer.json:

{
    "require": {
        "codeception/base": "^2.5",
        "ext-json": "*"
    },
    "autoload": {
        "psr-4": {
            "Codeception\Module\": "src/"
        }
    }
}

这段代码在MyClass.php:

里面
<?php
class MyClass extends \Codeception\Actor
{
    public function SayHi($name) {
        $respBody = 'hi' . $name;
        return json_decode($respBody, true) ;
    }
}

这段代码在MyCest.php:

里面
<?php
class MyCest
{
    public function MyTestCase(MyClass $I)
    {
        $I->SayHi('ZiZi');
    }
}

这是里面 mytests.suite.yml:

actor: ApiTester
modules:
  enabled:
    - REST:
        depends: PhpBrowser
        part: Json

    - Asserts:

现在,构建正常,其日志暗示 mytests 已作为套件成功加载:

zeinab@Zeinab:~/PhpstormProjects/test$ codecept build
==== Redirecting to Composer-installed version in vendor/codeception ====
Building Actor classes for suites: acceptance, api, functional, mytests, unit
 -> AcceptanceTesterActions.php generated successfully. 0 methods added
\AcceptanceTester includes modules: PhpBrowser, \Helper\Acceptance
 -> ApiTesterActions.php generated successfully. 0 methods added
\ApiTester includes modules: \Helper\Api
 -> FunctionalTesterActions.php generated successfully. 0 methods added
\FunctionalTester includes modules: \Helper\Functional
 -> ApiTesterActions.php generated successfully. 0 methods added
\ApiTester includes modules: REST, PhpBrowser, Asserts
 -> UnitTesterActions.php generated successfully. 0 methods added
\UnitTester includes modules: Asserts, \Helper\Unit

但是运行测试遇到错误:

zeinab@Zeinab:~/PhpstormProjects/test$ codecept run tests/mytests/MyCest.php 

==== Redirecting to Composer-installed version in vendor/codeception ====
Codeception PHP Testing Framework v2.5.1
Powered by PHPUnit 7.5.0 by Sebastian Bergmann and contributors.
Running with seed: 


Mytests Tests (1) -------------------------------------------------------------------------------------------------------------
E MyCest: My test case (0.00s)
-------------------------------------------------------------------------------------------------------------------------------
DEPRECATION: Calling the "Symfony\Component\BrowserKit\Client::getInternalResponse()" method before the "request()" one is deprecated since Symfony 4.1 and will throw an exception in 5.0. /home/zeinab/PhpstormProjects/test/vendor/symfony/browser-kit/Client.php:217


Time: 76 ms, Memory: 12.00MB

There was 1 error:

---------
1) MyCest: My test case
 Test  tests/mytests/MyCest.php:MyTestCase

  [InjectionException] Failed to inject dependencies in instance of 'MyCest'. Class MyClass does not exist  

#1  phar:///usr/local/bin/codecept/autoload.php:12
#2  /usr/local/bin/codecept:5

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

我想我在配置我的 composer.json 时仍然有问题,但我想不通。

编辑 2:

我将 composer.json 中的 class 定义更改为 ``;结果是:

zeinab@Zeinab:~/PhpstormProjects/test$ codecept run tests/mytests/MyCest.php 

==== Redirecting to Composer-installed version in vendor/codeception ====
Codeception PHP Testing Framework v2.5.1
Powered by PHPUnit 7.5.1 by Sebastian Bergmann and contributors.
Running with seed: 


Mytests Tests (1) ------------------------------------------------------------------------------------------------------------------------------------------------------------------------
E MyCest: My test case (0.00s)
------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
DEPRECATION: Calling the "Symfony\Component\BrowserKit\Client::getInternalResponse()" method before the "request()" one is deprecated since Symfony 4.1 and will throw an exception in 5.0. /home/zeinab/PhpstormProjects/test/vendor/symfony/browser-kit/Client.php:217


Time: 76 ms, Memory: 12.00MB

There was 1 error:

---------
1) MyCest: My test case
 Test  tests/mytests/MyCest.php:MyTestCase

  [InjectionException] Failed to inject dependencies in instance of 'MyCest'. Class MyClass does not exist  

#1  phar:///usr/local/bin/codecept/autoload.php:12
#2  /usr/local/bin/codecept:5

ERRORS!
Tests: 1, Assertions: 0, Errors: 1.

先写个Helper不是更简单吗?在这种情况下,将 class 从 \Codeception\Module 扩展,而不是 \Codeception\Actor?然后保持你的配置不变并删除将其添加为 Actor

我终于解决了这个问题;感谢@Alexey 和@Naktibalda。

我将包含我的 class、MyClass.php 的 PHP 文件放在目录 src/ 中,并在 composer.json 中将我的 class 定义为:

{
    "require": {
        "codeception/base": "^2.5",
        "ext-json": "*",
        "codeception/codeception": "^2.5"
    },
    "autoload": {
        "classmap": ["src/"]
    }
}

现在这是我的文件结构:

test
|_ src
   |_ MyClass.php
|_ tests
   |_ ...
   |_ mytests
      |_ MyCest.php
   |_ ...
   |_ mytests.suite.yml
   |_ ...
|_ vendor
|_ codeception.yml
|_ composer.json
|_ composer.lock

这段代码在MyClass.php:

里面
<?php
class MyClass extends \Codeception\Actor
{
    public function SayHi($name) {
        $respBody = 'hi' . $name;
        return $respBody;
    }
}

这段代码在MyCest.php:

里面
<?php
class MyCest
{
    public function MyTestCase(MyClass $I)
    {
        $resp = $I->SayHi('ZiZi');
        echo "Response: " . $resp;
    }
}

这是里面 mytests.suite.yml:

actor: ApiTester
modules:
  enabled:
    - REST:
        depends: PhpBrowser
        part: Json

    - Asserts: