File: UploadedFileFactoryTest.php

package info (click to toggle)
php-slim-psr7 1.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 656 kB
  • sloc: php: 5,274; makefile: 17; sh: 11; xml: 10
file content (96 lines) | stat: -rw-r--r-- 2,888 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
<?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\Tests\Psr7\Factory;

use PHPUnit\Framework\TestCase as UploadedFileFactoryTestCase;
//use Interop\Http\Factory\UploadedFileFactoryTestCase;
use InvalidArgumentException;
use Psr\Http\Message\StreamInterface;
use Slim\Psr7\Factory\StreamFactory;
use Slim\Psr7\Factory\UploadedFileFactory;

use function fopen;
use function fwrite;
use function rewind;
use function sys_get_temp_dir;
use function tempnam;

/**
 * @group excluded
 */
#[\PHPUnit\Framework\Attributes\Group('excluded')]
class UploadedFileFactoryTest extends UploadedFileFactoryTestCase
{
    protected function createUploadedFileFactory(): UploadedFileFactory
    {
        return new UploadedFileFactory();
    }

    protected function createStream($content): StreamInterface
    {
        $file = tempnam(sys_get_temp_dir(), 'Slim_Http_UploadedFileTest_');
        $resource = fopen($file, 'r+');
        fwrite($resource, $content);
        rewind($resource);

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

    /**
     * Create a `\Psr\Http\Message\StreamInterface` mock with a `getMetadata` method expectation.
     *
     * @param string $argKey Argument for the method expectation.
     * @param mixed $returnValue Return value of the `getMetadata` method.
     *
     * @return StreamInterface
     */
    protected function prophesizeStreamInterfaceWithGetMetadataMethod(string $argKey, $returnValue): StreamInterface
    {
        $stream = $this->createMock(StreamInterface::class);
        $stream->expects($this->once())
            ->method('getMetadata')
            ->with($argKey)
            ->willReturn($returnValue);

        return $stream;
    }

    public function testCreateUploadedFileWithInvalidUri()
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('File is not readable.');

        $stream = $this->createMock(StreamInterface::class);
        $stream->expects($this->once())
            ->method('getMetadata')
            ->with('uri')
            ->willReturn(null);

        $this->factory->createUploadedFile($stream);
    }

    public function testCreateUploadedFileWithNonReadableFile()
    {
        $this->expectException(InvalidArgumentException::class);
        $this->expectExceptionMessage('File is not readable.');

        $stream = $this->createMock(StreamInterface::class);
        $stream->expects($this->once())
            ->method('getMetadata')
            ->with('uri')
            ->willReturn('non-readable');
        $stream->expects($this->once())
            ->method('isReadable')
            ->willReturn(false);

        $this->factory->createUploadedFile($stream);
    }
}