Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
71.43% covered (warning)
71.43%
25 / 35
44.44% covered (danger)
44.44%
4 / 9
CRAP
0.00% covered (danger)
0.00%
0 / 1
ExportFileWebdavCall
71.43% covered (warning)
71.43%
25 / 35
44.44% covered (danger)
44.44%
4 / 9
16.94
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 getTestName
0.00% covered (danger)
0.00%
0 / 3
0.00% covered (danger)
0.00%
0 / 1
2
 bootstrap
60.00% covered (warning)
60.00%
3 / 5
0.00% covered (danger)
0.00%
0 / 1
2.26
 isFakeDatalayer
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 shouldDoRequestValidation
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 shouldDoResponseValidation
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 getRequest
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 getExpectedStatusCode
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 verifyValidResponse
83.33% covered (warning)
83.33%
15 / 18
0.00% covered (danger)
0.00%
0 / 1
4.07
1<?php
2
3namespace Apie\IntegrationTests\Requests;
4
5use Apie\Common\IntegrationTestLogger;
6use Apie\Core\BoundedContext\BoundedContextId;
7use Apie\Core\Entities\EntityInterface;
8use Apie\Core\Identifiers\SnakeCaseSlug;
9use Apie\Faker\Datalayers\FakerDatalayer;
10use Apie\IntegrationTests\Interfaces\TestApplicationInterface;
11use Nyholm\Psr7\ServerRequest;
12use PHPUnit\Framework\TestCase;
13use Psr\Http\Message\ResponseInterface;
14use Psr\Http\Message\ServerRequestInterface;
15use ZipArchive;
16
17class ExportFileWebdavCall implements WebdavTestRequestInterface, BootstrapRequestInterface
18{
19    private bool $faked = false;
20
21    /**
22     * @param array<int, EntityInterface> $entities
23     */
24    public function __construct(
25        private readonly BoundedContextId $boundedContextId,
26        private readonly string $path,
27        private readonly array $entities = [],
28    ) {
29    }
30
31    public function getTestName(): SnakeCaseSlug
32    {
33        return new SnakeCaseSlug(
34            'download_file_in_' . $this->boundedContextId . '_' . count($this->entities) . '_' . md5($this->path)
35        );
36    }
37
38    public function bootstrap(TestApplicationInterface $testApplication): void
39    {
40        $apieFacade = $testApplication->getServiceContainer()->get('apie');
41        foreach ($this->entities as $entity) {
42            $apieFacade->persistNew($entity, $this->boundedContextId);
43            usleep(1); // Ensure different timestamps
44        }
45        $this->faked = $testApplication->getApplicationConfig()->getDatalayerImplementation()->name === FakerDatalayer::class;
46    }
47
48    public function isFakeDatalayer(): bool
49    {
50        return $this->faked;
51    }
52
53    public function shouldDoRequestValidation(): bool
54    {
55        return false;
56    }
57
58    public function shouldDoResponseValidation(): bool
59    {
60        return true;
61    }
62
63    public function getRequest(): ServerRequestInterface
64    {
65        return new ServerRequest(
66            'GET',
67            'http://localhost/webdav/' . $this->boundedContextId . $this->path
68        );
69    }
70
71    public function getExpectedStatusCode(): int
72    {
73        return 200;
74    }
75
76    public function verifyValidResponse(ResponseInterface $response): void
77    {
78        $body = (string) $response->getBody();
79        $statusCode = $response->getStatusCode();
80        if ($statusCode === 500) {
81            IntegrationTestLogger::failTestShowError();
82        }
83        TestCase::assertEquals($this->getExpectedStatusCode(), $statusCode, 'Expect status code 200, got: ' . $body);
84        TestCase::assertEquals(
85            'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
86            $response->getHeaderLine('content-type')
87        );
88        if (!class_exists(ZipArchive::class)) {
89            return;
90        }
91        $tempfile = tempnam(sys_get_temp_dir(), 'webdav-export');
92        try {
93            file_put_contents($tempfile, $body);
94            $archive = new ZipArchive();
95            if (!$archive->open($tempfile)) {
96                throw new \LogicException('Could not open zip file');
97            }
98            TestCase::assertEquals(6, $archive->count());
99        } finally {
100            @unlink($tempfile);
101        }
102    }
103}