Skip to content

Instantly share code, notes, and snippets.

@williamespindola
Created July 31, 2017 00:56
Show Gist options
  • Save williamespindola/f0678023e5c8411e61740f827154aa49 to your computer and use it in GitHub Desktop.
Save williamespindola/f0678023e5c8411e61740f827154aa49 to your computer and use it in GitHub Desktop.
Exemplo de teste com stub, dummy, mock builtin function, mock de final class
//------------------ DownloadAssetsProcess.php
<?php
/**
* This file is part of Whirlpool Middleware project.
*
* PHP version 7
*
* @category PHP
* @package ArizonaTecnologia\WhirlppolMiddleware\Services\Consumers\Process
* @author William Espindola <oi@williamespindola.com.br>
* @copyright 2016 Arizona Tecnologia - All Rights Reserved
* @license http://gitlab.arizona.com.br/pim/whirlpool-middleware/licence.txt Proprietary
* @link http://gitlab.arizona.com.br/pim/whirlpool-middleware
*/
declare(strict_types=1);
namespace ArizonaTecnologia\WhirlppolMiddleware\Services\Consumers\Process;
use Pimple\Container;
use SRIO\ChainOfResponsibility\ChainContext;
use SRIO\ChainOfResponsibility\ChainProcessInterface;
use ArizonaTecnologia\WhirlppolMiddleware\Services\FoldersCreator;
use ArizonaTecnologia\VistoIntegrator\Asset\Visto\GetAssets as RequestVistoAssets;
use ArizonaTecnologia\VistoIntegrator\Asset\Nuxeo\Download as RequestNuxelDownloadAsset;
/**
* Walk on assets and download
*
* @category PHP
* @package ArizonaTecnologia\WhirlppolMiddleware\Services\Consumers\Process
* @author William Espindola <oi@williamespindola.com.br>
* @copyright 2016 Arizona Tecnologia - All Rights Reserved
* @license http://gitlab.arizona.com.br/pim/whirlpool-middleware/licence.txt Proprietary
* @version Release: 1.0.0
* @link http://gitlab.arizona.com.br/pim/whirlpool-middleware
*/
class DownloadAssetsProcess implements ChainProcessInterface
{
/**
* @var FoldersCreator $vistoFolders
*/
private $vistoFolders;
/**
* @var RequestVistoAssets $requestVistoAssets
*/
private $requestVistoAssets;
/**
* @var RequestNuxelDownloadAsset requestNuxelDownloadAsset
*/
private $requestNuxelDownloadAsset;
/**
* Initializes DownloadAssets
*
* @param Container $container
*/
public function __construct(
FoldersCreator $vistoFolders,
RequestVistoAssets $requestVistoAssets,
RequestNuxelDownloadAsset $requestNuxelDownloadAsset
)
{
$this->vistoFolders = $vistoFolders;
$this->requestVistoAssets = $requestVistoAssets;
$this->requestNuxelDownloadAsset = $requestNuxelDownloadAsset;
}
/**
* Execute donwload for each asset
*
* @param ChainContext $context
*/
public function execute(ChainContext $context)
{
foreach ($context['sanitizedAssets'] as $asset) {
$directory = $this->vistoFolders
->createFolderInPath(
$this->vistoFolders->getNodePath('/print'),
$asset['work-asset-data']['asset-type']
);
$assetOnVisto = $this->requestVistoAssets
->getAssets([$asset['product-version-assets']['asset_id']]);
if ($assetOnVisto->assets->fileRepositoryObjectId != NULL) {
$fildPath = $this->requestNuxelDownloadAsset
->download(
$assetOnVisto->assets->fileRepositoryObjectId,
$asset['work-asset-data']['striped-name']
);
$destiniFile = $directory . '/' . $asset['work-asset-data']['striped-name'];
rename($fildPath, $destiniFile);
}
}
}
}
//-------------------------------- DownloadAssetsProcessTest.php
<?php
/**
* This file is part of PIM API project.
*
* PHP version 7
*
* @category PHP
* @package ArizonaTecnologia\WhirlppolMiddleware\Test\Services\Consumers\Process
* @author William Espindola <oi@williamespindola.com.br>
* @copyright 2016 Arizona Tecnologia - All Rights Reserved
* @license Proprietary
* @link http://gitlab.arizona.com.br/pim/pim-api
*/
namespace ArizonaTecnologia\WhirlppolMiddleware\Services\Consumers\Process;
use PHPUnit\Framework\TestCase;
use Psr\Http\Message\ResponseInterface;
use SRIO\ChainOfResponsibility\ArrayChainContext;
use ArizonaTecnologia\WhirlppolMiddleware\Services\FoldersCreator;
use ArizonaTecnologia\VistoIntegrator\Asset\Visto\GetAssets;
use ArizonaTecnologia\WhirlppolMiddleware\Services\Consumers\Process\DownloadAssetsProcess;
use ArizonaTecnologia\VistoIntegrator\Asset\Nuxeo\Download;
$renamed = null;
class MockFolders implements FoldersCreator
{
public function createFolders($domainPath)
{
}
public function getFolder()
{
}
public function getFullPath()
{
}
public function getNodePath($name)
{
return 'root-folder/' . $name;
}
public function createFolderInPath($destinyPath, $newFolder)
{
return $destinyPath . '/' . $newFolder;
}
}
function rename($first, $secound)
{
global $renamed;
$renamed = $first . $secound;
}
class DownloadAssetsProcessTest extends TestCase
{
public function setUp()
{
global $renamed;
$renamed = null;
$this->mockGetAsset = $this->getMockBuilder(GetAssets::class)
->disableOriginalConstructor()
->setMethods(['getAssets'])
->getMock();
$this->mockDownloadAsset = $this->getMockBuilder(Download::class)
->disableOriginalConstructor()
->setMethods(['download'])
->getMock();
}
public function testDownloadAssetsProcessWhenObjectNofFoundShouldKeepRenamedNULL()
{
global $renamed;
$dummyObject = new \stdClass;
$dummyObject->fileRepositoryObjectId = null;
$dummyAsset = new \stdClass;
$dummyAsset->assets = $dummyObject;
$this->mockGetAsset
->expects($this->any())
->method('getAssets')
->willReturn($dummyAsset);
$instance = new DownloadAssetsProcess(
new MockFolders(),
$this->mockGetAsset,
$this->mockDownloadAsset
);
$context = new ArrayChainContext([
'sanitizedAssets' => [
[
'work-asset-data' => [
'asset-type' => 'foo',
'striped-name' => 'bar'
],
'product-version-assets' => [
'asset_id' => 'foo'
]
]
]
]);
$instance->execute($context);
$this->assertEquals(null, $renamed);
}
public function testDownloadAssetsProcessWhenObjectFoundShouldRenamedConcatPaths()
{
global $renamed;
$dummyObject = new \stdClass;
$dummyObject->fileRepositoryObjectId = 'foo-bar';
$dummyAsset = new \stdClass;
$dummyAsset->assets = $dummyObject;
$this->mockGetAsset
->expects($this->any())
->method('getAssets')
->willReturn($dummyAsset);
$this->mockDownloadAsset
->expects($this->any())
->method('download')
->willReturn('foo');
$instance = new DownloadAssetsProcess(
new MockFolders(),
$this->mockGetAsset,
$this->mockDownloadAsset
);
$context = new ArrayChainContext([
'sanitizedAssets' => [
[
'work-asset-data' => [
'asset-type' => 'foo',
'striped-name' => 'bar'
],
'product-version-assets' => [
'asset_id' => 'foo'
]
]
]
]);
$instance->execute($context);
$this->assertEquals('fooroot-folder//print/foo/bar', $renamed);
}
}
//--------------------------
➜ whirlpool-middleware git:(BP-699) ✗ ./vendor/bin/phpunit tests/unit/Services/Consumers/Process/DownloadAssetsProcessTest.php
PHPUnit 6.1.1 by Sebastian Bergmann and contributors.
Runtime: PHP 7.0.10 with Xdebug 2.4.1
Configuration: /Users/william.espindola/Desktop/whirlpool-middleware/phpunit.xml.dist
.. 2 / 2 (100%)
Time: 595 ms, Memory: 6.00MB
OK (2 tests, 2 assertions)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment