Skip to content

Commit

Permalink
Merge 10dc4e4 into 69878ea
Browse files Browse the repository at this point in the history
  • Loading branch information
adriansuter committed Apr 30, 2019
2 parents 69878ea + 10dc4e4 commit 30e659f
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
27 changes: 27 additions & 0 deletions tests/Assets/PhpFactoryFunctionOverrides.php
@@ -0,0 +1,27 @@
<?php
/**
* Slim Framework (https://slimframework.com)
*
* @license https://github.com/slimphp/Slim-Psr7/blob/master/LICENSE.md (MIT License)
*/

declare(strict_types=1);

namespace Slim\Psr7\Factory;

/**
* Return `false` if the parameter is "non-readable", otherwise the function would return the value from
* the php built-in function.
*
* @param string $filename
*
* @return bool
*/
function is_readable(string $filename): bool
{
if ('non-readable' === $filename) {
return false;
}

return \is_readable($filename);
}
45 changes: 45 additions & 0 deletions tests/Factory/UploadedFileFactoryTest.php
Expand Up @@ -10,6 +10,9 @@
namespace Slim\Tests\Psr7\Factory;

use Interop\Http\Factory\UploadedFileFactoryTestCase;
use InvalidArgumentException;
use Prophecy\Argument\Token\ExactValueToken;
use Prophecy\Prophecy\MethodProphecy;
use Psr\Http\Message\StreamInterface;
use Slim\Psr7\Factory\StreamFactory;
use Slim\Psr7\Factory\UploadedFileFactory;
Expand All @@ -36,4 +39,46 @@ protected function createStream($content)

return (new StreamFactory())->createStreamFromResource($resource);
}

/**
* Prophesize a `\Psr\Http\Message\StreamInterface` with a `getMetadata` method prophecy.
*
* @param string $argKey Argument for the method prophecy.
* @param mixed $returnValue Return value of the `getMetadata` method.
*
* @return StreamInterface
*/
protected function prophesizeStreamInterfaceWithGetMetadataMethod(string $argKey, $returnValue): StreamInterface
{
$prophecy = $this->prophesize(StreamInterface::class);
$mp = new MethodProphecy($prophecy, 'getMetadata', [new ExactValueToken($argKey)]);
$mp->shouldBeCalled();
$mp->willReturn($returnValue);

/** @var StreamInterface $upload */
$upload = $prophecy->reveal();
return $upload;
}

/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage File is not readable.
*/
public function testCreateUploadedFileWithInvalidUri()
{
$this->factory->createUploadedFile(
$this->prophesizeStreamInterfaceWithGetMetadataMethod('uri', null)
);
}

/**
* @expectedException InvalidArgumentException
* @expectedExceptionMessage File is not readable.
*/
public function testCreateUploadedFileWithNonReadableFile()
{
$this->factory->createUploadedFile(
$this->prophesizeStreamInterfaceWithGetMetadataMethod('uri', 'non-readable')
);
}
}
1 change: 1 addition & 0 deletions tests/bootstrap.php
Expand Up @@ -11,3 +11,4 @@

require __DIR__ . '/Assets/PhpGetAllHeaders.php';
require __DIR__ . '/Assets/PhpFunctionOverrides.php';
require __DIR__ . '/Assets/PhpFactoryFunctionOverrides.php';

0 comments on commit 30e659f

Please sign in to comment.