File: UploadedFileFactoryTestCase.php

package info (click to toggle)
php-http-interop-http-factory-tests 2.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 152 kB
  • sloc: php: 549; makefile: 7
file content (76 lines) | stat: -rw-r--r-- 2,380 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
<?php

namespace Interop\Http\Factory;

use PHPUnit\Framework\TestCase;
use Psr\Http\Message\StreamInterface;
use Psr\Http\Message\UploadedFileFactoryInterface;
use Psr\Http\Message\UploadedFileInterface;
use function strlen;
use const UPLOAD_ERR_NO_FILE;
use const UPLOAD_ERR_OK;

abstract class UploadedFileFactoryTestCase extends TestCase
{
    /**
     * @var UploadedFileFactoryInterface
     */
    protected $factory;

    /**
     * @return UploadedFileFactoryInterface
     */
    abstract protected function createUploadedFileFactory();

    /**
     * @return StreamInterface
     */
    abstract protected function createStream($content);

    public function setUp(): void
    {
        $this->factory = $this->createUploadedFileFactory();
    }

    protected function assertUploadedFile(
        $file,
        $content,
        $size = null,
        $error = null,
        $clientFilename = null,
        $clientMediaType = null
    ) {
        static::assertInstanceOf(UploadedFileInterface::class, $file);
        static::assertSame($content, (string) $file->getStream());
        static::assertSame(($size ?? strlen($content)), $file->getSize());
        static::assertSame(($error ?? UPLOAD_ERR_OK), $file->getError());
        static::assertSame($clientFilename, $file->getClientFilename());
        static::assertSame($clientMediaType, $file->getClientMediaType());
    }

    public function testCreateUploadedFileWithClientFilenameAndMediaType()
    {
        $content = 'this is your capitan speaking';
        $upload = $this->createStream($content);
        $error = UPLOAD_ERR_OK;
        $clientFilename = 'test.txt';
        $clientMediaType = 'text/plain';

        $file = $this->factory->createUploadedFile($upload, null, $error, $clientFilename, $clientMediaType);

        $this->assertUploadedFile($file, $content, null, $error, $clientFilename, $clientMediaType);
    }

    public function testCreateUploadedFileWithError()
    {
        $upload = $this->createStream('foobar');
        $error = UPLOAD_ERR_NO_FILE;

        $file = $this->factory->createUploadedFile($upload, null, $error);

        // Cannot use assertUploadedFile() here because the error prevents
        // fetching the content stream.
        static::assertInstanceOf(UploadedFileInterface::class, $file);
        static::assertSame($error, $file->getError());
    }
}