66use Cake \TestSuite \TestCase ;
77use Josegonzalez \Upload \File \Writer \DefaultWriter ;
88use Laminas \Diactoros \UploadedFile ;
9- use League \Flysystem \Adapter \NullAdapter ;
10- use League \Flysystem \Vfs \VfsAdapter ;
9+ use League \Flysystem \InMemory \InMemoryFilesystemAdapter ;
10+ use League \Flysystem \UnableToDeleteFile ;
11+ use League \Flysystem \UnableToMoveFile ;
12+ use League \Flysystem \UnableToWriteFile ;
1113use VirtualFileSystem \FileSystem as Vfs ;
1214
1315class DefaultWriterTest extends TestCase
@@ -29,7 +31,7 @@ public function setUp(): void
2931 $ this ->settings = [
3032 'filesystem ' => [
3133 'adapter ' => function () {
32- return new VfsAdapter ( new Vfs () );
34+ return new InMemoryFilesystemAdapter ( );
3335 },
3436 ],
3537 ];
@@ -65,9 +67,9 @@ public function testInvoke()
6567
6668 public function testDelete ()
6769 {
68- $ filesystem = $ this ->getMockBuilder ('League\Flysystem\FilesystemInterface ' )->getMock ();
69- $ filesystem ->expects ($ this ->at (0 ))->method ('delete ' )-> will ( $ this -> returnValue ( true )) ;
70- $ filesystem ->expects ($ this ->at (1 ))->method ('delete ' )->will ($ this ->returnValue ( false ));
70+ $ filesystem = $ this ->getMockBuilder ('League\Flysystem\FilesystemOperator ' )->getMock ();
71+ $ filesystem ->expects ($ this ->at (0 ))->method ('delete ' );
72+ $ filesystem ->expects ($ this ->at (1 ))->method ('delete ' )->will ($ this ->throwException ( new UnableToDeleteFile () ));
7173 $ writer = $ this ->getMockBuilder ('Josegonzalez\Upload\File\Writer\DefaultWriter ' )
7274 ->setMethods (['getFilesystem ' ])
7375 ->setConstructorArgs ([$ this ->table , $ this ->entity , $ this ->data , $ this ->field , $ this ->settings ])
@@ -86,51 +88,51 @@ public function testDelete()
8688
8789 public function testWriteFile ()
8890 {
89- $ filesystem = $ this ->getMockBuilder ('League\Flysystem\FilesystemInterface ' )->getMock ();
90- $ filesystem ->expects ($ this ->once ())->method ('writeStream ' )-> will ( $ this -> returnValue ( true )) ;
91- $ filesystem ->expects ($ this ->exactly (3 ))->method ('delete ' )-> will ( $ this -> returnValue ( true )) ;
92- $ filesystem ->expects ($ this ->once ())->method ('rename ' )-> will ( $ this -> returnValue ( true ) );
91+ $ filesystem = $ this ->getMockBuilder ('League\Flysystem\FilesystemOperator ' )->getMock ();
92+ $ filesystem ->expects ($ this ->once ())->method ('writeStream ' );
93+ $ filesystem ->expects ($ this ->exactly (3 ))->method ('delete ' );
94+ $ filesystem ->expects ($ this ->once ())->method ('move ' );
9395 $ this ->assertTrue ($ this ->writer ->writeFile ($ filesystem , $ this ->vfs ->path ('/tmp/tempfile ' ), 'path ' ));
9496
95- $ filesystem = $ this ->getMockBuilder ('League\Flysystem\FilesystemInterface ' )->getMock ();
96- $ filesystem ->expects ($ this ->once ())->method ('writeStream ' )->will ($ this ->returnValue ( false ));
97- $ filesystem ->expects ($ this ->exactly (2 ))->method ('delete ' )-> will ( $ this -> returnValue ( true )) ;
98- $ filesystem ->expects ($ this ->never ())->method ('rename ' );
97+ $ filesystem = $ this ->getMockBuilder ('League\Flysystem\FilesystemOperator ' )->getMock ();
98+ $ filesystem ->expects ($ this ->once ())->method ('writeStream ' )->will ($ this ->throwException ( new UnableToWriteFile () ));
99+ $ filesystem ->expects ($ this ->exactly (2 ))->method ('delete ' );
100+ $ filesystem ->expects ($ this ->never ())->method ('move ' );
99101 $ this ->assertFalse ($ this ->writer ->writeFile ($ filesystem , $ this ->vfs ->path ('/tmp/tempfile ' ), 'path ' ));
100102
101- $ filesystem = $ this ->getMockBuilder ('League\Flysystem\FilesystemInterface ' )->getMock ();
102- $ filesystem ->expects ($ this ->once ())->method ('writeStream ' )-> will ( $ this -> returnValue ( true )) ;
103- $ filesystem ->expects ($ this ->exactly (3 ))->method ('delete ' )-> will ( $ this -> returnValue ( true )) ;
104- $ filesystem ->expects ($ this ->once ())->method ('rename ' )->will ($ this ->returnValue ( false ));
103+ $ filesystem = $ this ->getMockBuilder ('League\Flysystem\FilesystemOperator ' )->getMock ();
104+ $ filesystem ->expects ($ this ->once ())->method ('writeStream ' );
105+ $ filesystem ->expects ($ this ->exactly (3 ))->method ('delete ' );
106+ $ filesystem ->expects ($ this ->once ())->method ('move ' )->will ($ this ->throwException ( new UnableToMoveFile () ));
105107 $ this ->assertFalse ($ this ->writer ->writeFile ($ filesystem , $ this ->vfs ->path ('/tmp/tempfile ' ), 'path ' ));
106108 }
107109
108110 public function testDeletePath ()
109111 {
110- $ filesystem = $ this ->getMockBuilder ('League\Flysystem\FilesystemInterface ' )->getMock ();
111- $ filesystem ->expects ($ this ->any ())->method ('delete ' )-> will ( $ this -> returnValue ( true )) ;
112+ $ filesystem = $ this ->getMockBuilder ('League\Flysystem\FilesystemOperator ' )->getMock ();
113+ $ filesystem ->expects ($ this ->any ())->method ('delete ' );
112114 $ this ->assertTrue ($ this ->writer ->deletePath ($ filesystem , 'path ' ));
113115
114- $ filesystem = $ this ->getMockBuilder ('League\Flysystem\FilesystemInterface ' )->getMock ();
115- $ filesystem ->expects ($ this ->any ())->method ('delete ' )->will ($ this ->returnValue ( false ));
116+ $ filesystem = $ this ->getMockBuilder ('League\Flysystem\FilesystemOperator ' )->getMock ();
117+ $ filesystem ->expects ($ this ->any ())->method ('delete ' )->will ($ this ->throwException ( new UnableToDeleteFile () ));
116118 $ this ->assertFalse ($ this ->writer ->deletePath ($ filesystem , 'path ' ));
117119 }
118120
119121 public function testGetFilesystem ()
120122 {
121- $ this ->assertInstanceOf ('League\Flysystem\FilesystemInterface ' , $ this ->writer ->getFilesystem ('field ' , []));
122- $ this ->assertInstanceOf ('League\Flysystem\FilesystemInterface ' , $ this ->writer ->getFilesystem ('field ' , [
123+ $ this ->assertInstanceOf ('League\Flysystem\FilesystemOperator ' , $ this ->writer ->getFilesystem ('field ' , []));
124+ $ this ->assertInstanceOf ('League\Flysystem\FilesystemOperator ' , $ this ->writer ->getFilesystem ('field ' , [
123125 'key ' => 'value ' ,
124126 ]));
125- $ this ->assertInstanceOf ('League\Flysystem\FilesystemInterface ' , $ this ->writer ->getFilesystem ('field ' , [
127+ $ this ->assertInstanceOf ('League\Flysystem\FilesystemOperator ' , $ this ->writer ->getFilesystem ('field ' , [
126128 'filesystem ' => [
127- 'adapter ' => new NullAdapter (),
129+ 'adapter ' => new InMemoryFilesystemAdapter (),
128130 ],
129131 ]));
130- $ this ->assertInstanceOf ('League\Flysystem\FilesystemInterface ' , $ this ->writer ->getFilesystem ('field ' , [
132+ $ this ->assertInstanceOf ('League\Flysystem\FilesystemOperator ' , $ this ->writer ->getFilesystem ('field ' , [
131133 'filesystem ' => [
132134 'adapter ' => function () {
133- return new NullAdapter ();
135+ return new InMemoryFilesystemAdapter ();
134136 },
135137 ],
136138 ]));
0 commit comments