库:“aws/aws-sdk-php”:“2. *”
PHP版本:PHP 5.4.24(cli)

composer.json

{
    "require": {
        "php": ">=5.3.1",
        "aws/aws-sdk-php": "2.*",
        ...
    },

    "require-dev": {
        "phpunit/phpunit": "4.1",
        "davedevelopment/phpmig": "*",
        "anahkiasen/rocketeer": "*"
    },
    ...
}

我们制作了一个AwsWrapper来获取功能性 Action :uploadFile,deleteFile ...
您可以阅读该类,并进行依赖注入(inject)以进行单元测试。
专注于构造函数,并在uploadFile函数上进行内部$ this-> s3Client-> putObject(...)调用。
<?php

namespace app\lib\modules\files;

use Aws\Common\Aws;
use Aws\S3\Exception\S3Exception;
use Aws\S3\S3Client;
use core\lib\exceptions\WSException;
use core\lib\Injector;
use core\lib\utils\System;

class AwsWrapper
{

  /**
   * @var \core\lib\Injector
   */
  private $injector;

  /**
   * @var S3Client
   */
  private $s3Client;

  /**
   * @var string
   */
  private $bucket;

  function __construct(Injector $injector = null, S3Client $s3 = null)
  {
    if( $s3 == null )
    {
      $aws = Aws::factory(dirname(__FILE__) . '/../../../../config/aws-config.php');
      $s3 = $aws->get('s3');
    }
    if($injector == null)
    {
      $injector = new Injector();
    }
    $this->s3Client = $s3;
    $this->bucket = \core\providers\Aws::getInstance()->getBucket();
    $this->injector = $injector;
  }

  /**
   * @param $key
   * @param $filePath
   *
   * @return \Guzzle\Service\Resource\Model
   * @throws \core\lib\exceptions\WSException
   */
  public function uploadFile($key, $filePath)
  {
    /** @var System $system */
    $system = $this->injector->get('core\lib\utils\System');
    $body   = $system->fOpen($filePath, 'r');
    try {
      $result = $this->s3Client->putObject(array(
        'Bucket' => $this->bucket,
        'Key'    => $key,
        'Body'   => $body,
        'ACL'    => 'public-read',
      ));
    }
    catch (S3Exception $e)
    {
      throw new WSException($e->getMessage(), 201, $e);
    }

    return $result;
  }

}

测试文件将我们的Injector和S3Client实例作为PhpUnit MockObject。要模拟S3Client,我们必须使用Mock Builder禁用原始构造函数。

模拟S3Client:
$this->s3Client = $this->getMockBuilder('Aws\S3\S3Client')->disableOriginalConstructor()->getMock();

配置内部putObject调用(用putObject进行测试的情况下,抛出S3Exception,但是$ this-> returnValue($ expected)存在相同的问题。

要初始化测试类并配置sut,请执行以下操作:
  public function setUp()
  {
    $this->s3Client = $this->getMockBuilder('Aws\S3\S3Client')->disableOriginalConstructor()->getMock();
    $this->injector = $this->getMock('core\lib\Injector');
  }

  public function configureSut()
  {
    return new AwsWrapper($this->injector, $this->s3Client);
  }

无效的代码:
$expectedArray = array(
  'Bucket' => Aws::getInstance()->getBucket(),
  'Key'    => $key,
  'Body'   => $body,
  'ACL'    => 'public-read',
);
$this->s3Client->expects($timesPutObject)
  ->method('putObject')
  ->with($expectedArray)
  ->will($this->throwException(new S3Exception($exceptionMessage, $exceptionCode)));
$this->configureSut()->uploadFile($key, $filePath);

当我们执行测试功能时,注入(inject)的S3Client不会引发异常或返回期望值,而始终返回NULL。

使用xdebug,我们已经看到S3Client MockObject的配置正确,但不能如will()所配置的那样工作。

一个“解决方案”(或一个不好的解决方案)可能正在做S​​3ClientWrapper,这只会将问题转移到无法使用模拟进行单元测试的其他类。

任何想法?

更新
使用xdebug配置MockObject的屏幕截图:

最佳答案

以下代码可以正常工作并通过,因此我认为您不会遇到由PHPUnit或AWS开发工具包引起的任何限制。

<?php

namespace Aws\Tests;

use Aws\S3\Exception\S3Exception;
use Aws\S3\S3Client;
use Guzzle\Service\Resource\Model;

class MyTest extends \PHPUnit_Framework_TestCase
{
    public function testMockCanReturnResult()
    {
        $model = new Model([
            'Contents' => [
                ['Key' => 'Obj1'],
                ['Key' => 'Obj2'],
                ['Key' => 'Obj3'],
            ],
        ]);

        $client = $this->getMockBuilder('Aws\S3\S3Client')
            ->disableOriginalConstructor()
            ->setMethods(['listObjects'])
            ->getMock();
        $client->expects($this->once())
            ->method('listObjects')
            ->with(['Bucket' => 'foobar'])
            ->will($this->returnValue($model));

        /** @var S3Client $client */
        $result = $client->listObjects(['Bucket' => 'foobar']);

        $this->assertEquals(
            ['Obj1', 'Obj2', 'Obj3'],
            $result->getPath('Contents/*/Key')
        );
    }

    public function testMockCanThrowException()
    {
        $client = $this->getMockBuilder('Aws\S3\S3Client')
            ->disableOriginalConstructor()
            ->setMethods(['getObject'])
            ->getMock();
        $client->expects($this->once())
            ->method('getObject')
            ->with(['Bucket' => 'foobar'])
            ->will($this->throwException(new S3Exception('VALIDATION ERROR')));

        /** @var S3Client $client */
        $this->setExpectedException('Aws\S3\Exception\S3Exception');
        $client->getObject(['Bucket' => 'foobar']);
    }
}

如果您只想模拟响应,而不关心模拟/ stub 对象,也可以使用Guzzle MockPlugin

10-08 00:58