如何使用Codeception而不使用框架来配置AspectMock?

问题描述 投票:0回答:1

我正在尝试配置AspectMock,并仔细阅读了文档和一些教程/示例代码块(thisthisthis),这些都为难题增加了一部分。与大多数使用AspectMock的人一样,我确信这是一个配置错误。我正在尝试模拟自己的自定义类以及vendor文件夹中的类。这是一个自定义类的示例:

<?php
// bootstrap.php
include __DIR__ . '/../vendor/autoload.php'; // composer autoload

$kernel = \AspectMock\Kernel::getInstance();
$kernel->init([
    'appDir' => __DIR__ . '/..',
    'debug' => true,
    'includePaths' => [__DIR__ . '../vendor/mailjet'],
    'excludePaths' => [__DIR__, '../vendor/codeception', '../vendor/phpunit'],
    'cacheDir' => '/tmp/somewhere'
]);

// library/Dumb.php
class Dumb
{
    protected $some_var;

    public function someMethod()
    {
        codecept_debug('original Dumb->someMethod() called');

        return 'called original someMethod';
    }
}

// LibraryFunctionTest.php
public function testDumbMock(): void
{
    $double = test::double('Dumb', [
        'someMethod' => function () {
            codecept_debug('stubbed  method called');
            return 'stubbed!';
        }
    ]);

    codecept_debug($double);

    $new_dumb = new Dumb();
    codecept_debug($new_dumb);
    $new_dumb->someMethod();
    $double->verifyInvoked('someMethod');
}

和输出:

- LibraryFunctionTest: Dumb mock  AspectMock\Proxy\ClassProxy Object
  (
      [reflected:protected] => ReflectionClass Object
          (
              [name] => Dumb
          )

      [className] => Dumb
      [invokedFail:protected] => Expected %s to be invoked but it never occurred. Got: %s
      [notInvokedMultipleTimesFail:protected] => Expected %s to be invoked %s times but it never occurred.
      [invokedMultipleTimesFail:protected] => Expected %s to be invoked but called %s times but called %s.
      [neverInvoked:protected] => Expected %s not to be invoked but it was.
  )

  Dumb Object
  (
      [some_var:protected] => 
  )

  original Dumb->someMethod() called
✖ LibraryFunctionTest: Dumb mock (0.01s)
-------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------


Time: 125 ms, Memory: 16.00 MB

There was 1 failure:

---------
1) LibraryFunctionTest: Dumb mock
 Test  tests/unit/library/FunctionsTest.php:testDumbMock
Expected Dumb::someMethod to be invoked but it never occurred. Got: 

您可以看到,Aspect模拟似乎在模拟Dumb类,但是当我尝试创建Dumb的实例并调用someMethod()时,它使用的是原始对象定义。

[当我对通过作曲家加载的MailJet库中的类进行类似的测试时,我得到了同样的东西:我似乎无法创建模拟对象的实例。

php unit-testing phpunit autoload codeception
1个回答
0
投票

怀疑,这是bootstrap.php中的配置错误>

1)我自己的课

解决方案是使用自动加载-我承认我并不真正理解并且没有使用它,因为我的应用程序完全是过程性的,根本没有自定义类。我只需要在bootstrap.php

的末尾添加以下行
$kernel->loadFile(__DIR__ . '/../library/Dumb.php');

(我相信)指示AspectMock进行此类的魔术操作并弄乱了该类的内部。在文档中自动加载的东西

,但我不理解,也没有抛出错误。

2)MailJet类

问题出在includePaths指令。我在路径中缺少一个额外的斜线。解决方案是:

'includePaths' => [__DIR__ . '/../vendor/mailjet'],
'excludePaths' => [__DIR__, '/../vendor/codeception', '/../vendor/phpunit'],

文档已有

前导斜杠,同样,这是我的错。但是,如果引发了错误提示我该错误,调试起来会容易得多。我觉得在跌倒之前,我尝试了这些路径的无数(错误)组合。
© www.soinside.com 2019 - 2024. All rights reserved.