File: DateFromResultTest.php

package info (click to toggle)
php-async-aws-core 1.27.0-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 988 kB
  • sloc: php: 6,837; makefile: 32
file content (67 lines) | stat: -rw-r--r-- 1,911 bytes parent folder | download | duplicates (3)
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
<?php

namespace AsyncAws\Core\Tests\Unit\Credentials;

use AsyncAws\Core\Credentials\DateFromResult;
use AsyncAws\Core\Response;
use AsyncAws\Core\Result;
use PHPUnit\Framework\TestCase;
use Psr\Log\NullLogger;
use Symfony\Component\HttpClient\MockHttpClient;
use Symfony\Component\HttpClient\Response\MockResponse;

class DateFromResultTest extends TestCase
{
    public function testWithValidDate()
    {
        $httpResponse = $this->getMockBuilder(MockResponse::class)
            ->disableOriginalConstructor()
            ->onlyMethods(['getHeaders'])
            ->getMock();
        $time = '2020-10-29 17:42:00';
        $httpResponse->expects(self::once())
            ->method('getHeaders')
            ->with(false)
            ->willReturn(['date' => [$time]]);

        $response = new Response($httpResponse, new MockHttpClient(), new NullLogger());
        $result = new DummyResult($response);

        $output = (new DummyCredentials())->expose($result);
        self::assertInstanceOf(\DateTimeImmutable::class, $output);
        self::assertEquals($time, $output->format('Y-m-d H:i:s'));
    }

    public function testWithNoDate()
    {
        $httpResponse = $this->getMockBuilder(MockResponse::class)
            ->disableOriginalConstructor()
            ->onlyMethods(['getHeaders'])
            ->getMock();

        $httpResponse->expects(self::once())
            ->method('getHeaders')
            ->with(false)
            ->willReturn([]);

        $response = new Response($httpResponse, new MockHttpClient(), new NullLogger());
        $result = new DummyResult($response);

        $output = (new DummyCredentials())->expose($result);
        self::assertNull($output);
    }
}

class DummyResult extends Result
{
}

class DummyCredentials
{
    use DateFromResult;

    public function expose(Result $result)
    {
        return $this->getDateFromResult($result);
    }
}