11<?php
22
3+ declare (strict_types=1 );
4+
35/**
46 * SPDX-FileCopyrightText: 2016 Nextcloud GmbH and Nextcloud contributors
57 * SPDX-License-Identifier: AGPL-3.0-or-later
911
1012use OCP \AppFramework \Http ;
1113use OCP \AppFramework \Http \FileDisplayResponse ;
14+ use OCP \AppFramework \Http \IOutput ;
1215use OCP \Files \File ;
16+ use PHPUnit \Framework \MockObject \MockObject ;
1317
1418class FileDisplayResponseTest extends \Test \TestCase {
15- /** @var File|\PHPUnit\Framework\MockObject\MockObject */
16- private $ file ;
17-
18- /** @var FileDisplayResponse */
19- private $ response ;
19+ private File &MockObject $ file ;
20+ private FileDisplayResponse $ response ;
2021
2122 protected function setUp (): void {
22- $ this ->file = $ this ->getMockBuilder ('OCP\Files\File ' )
23- ->getMock ();
24-
23+ $ this ->file = $ this ->createMock (File::class);
2524 $ this ->file ->expects ($ this ->once ())
2625 ->method ('getETag ' )
2726 ->willReturn ('myETag ' );
@@ -52,7 +51,7 @@ public function testLastModified(): void {
5251 }
5352
5453 public function test304 (): void {
55- $ output = $ this ->getMockBuilder (' OCP\AppFramework\Http\ IOutput' )
54+ $ output = $ this ->getMockBuilder (IOutput::class )
5655 ->disableOriginalConstructor ()
5756 ->getMock ();
5857
@@ -69,25 +68,50 @@ public function test304(): void {
6968
7069
7170 public function testNon304 (): void {
72- $ output = $ this ->getMockBuilder ('OCP\AppFramework\Http\IOutput ' )
71+ $ resource = fopen ('php://memory ' , "w+b " );
72+ fwrite ($ resource , 'my data ' );
73+ rewind ($ resource );
74+
75+ $ this ->file ->expects ($ this ->once ())
76+ ->method ('read ' )
77+ ->willReturn ($ resource );
78+ $ this ->file ->expects ($ this ->any ())
79+ ->method ('getSize ' )
80+ ->willReturn (7 );
81+
82+ $ output = $ this ->getMockBuilder (IOutput::class)
7383 ->disableOriginalConstructor ()
7484 ->getMock ();
75-
76- $ output ->expects ($ this ->any ())
85+ $ output ->expects ($ this ->once ())
7786 ->method ('getHttpResponseCode ' )
7887 ->willReturn (Http::STATUS_OK );
7988 $ output ->expects ($ this ->once ())
80- ->method ('setOutput ' )
81- ->with ($ this ->equalTo (' my data ' ));
89+ ->method ('setReadFile ' )
90+ ->with ($ this ->equalTo ($ resource ));
8291 $ output ->expects ($ this ->once ())
8392 ->method ('setHeader ' )
84- ->with ($ this ->equalTo ('Content-Length: 42 ' ));
93+ ->with ($ this ->equalTo ('Content-Length: 7 ' ));
94+
95+ $ this ->response ->callback ($ output );
96+ }
97+
98+ public function testFileNotFound (): void {
8599 $ this ->file ->expects ($ this ->once ())
86- ->method ('getContent ' )
87- ->willReturn ('my data ' );
88- $ this ->file ->expects ($ this ->any ())
89- ->method ('getSize ' )
90- ->willReturn (42 );
100+ ->method ('read ' )
101+ ->willReturn (false );
102+
103+ $ output = $ this ->getMockBuilder (IOutput::class)
104+ ->disableOriginalConstructor ()
105+ ->getMock ();
106+ $ output ->expects ($ this ->once ())
107+ ->method ('getHttpResponseCode ' )
108+ ->willReturn (Http::STATUS_OK );
109+ $ output ->expects ($ this ->once ())
110+ ->method ('setHttpResponseCode ' )
111+ ->with ($ this ->equalTo (Http::STATUS_NOT_FOUND ));
112+ $ output ->expects ($ this ->once ())
113+ ->method ('setOutput ' )
114+ ->with ($ this ->equalTo ('' ));
91115
92116 $ this ->response ->callback ($ output );
93117 }
0 commit comments