1
+ <?php
2
+
3
+ namespace yii2tech \tests \unit \filestorage ;
4
+
5
+ use Yii ;
6
+ use yii \web \Controller ;
7
+ use yii2tech \filestorage \DownloadAction ;
8
+ use yii2tech \filestorage \local \Storage ;
9
+
10
+ class DownloadActionTest extends TestCase
11
+ {
12
+ /**
13
+ * @param array $config action configuration.
14
+ * @return DownloadAction action instance.
15
+ */
16
+ protected function createAction ($ config = [])
17
+ {
18
+ $ controller = new Controller ('test ' , Yii::$ app );
19
+ return new DownloadAction ('download ' , $ controller , $ config );
20
+ }
21
+
22
+ /**
23
+ * @return Storage file storage instance.
24
+ */
25
+ protected function createFileStorage ()
26
+ {
27
+ return new Storage ([
28
+ 'basePath ' => $ this ->getTestTmpPath (),
29
+ 'buckets ' => [
30
+ 'temp ' => [
31
+ 'baseSubPath ' => 'temp '
32
+ ],
33
+ 'item ' => [
34
+ 'baseSubPath ' => 'item '
35
+ ],
36
+ ]
37
+ ]);
38
+ }
39
+
40
+ // Tests :
41
+
42
+ public function testSuccess ()
43
+ {
44
+ $ fileStorage = $ this ->createFileStorage ();
45
+ $ action = $ this ->createAction (['fileStorage ' => $ fileStorage ]);
46
+ $ bucket = $ fileStorage ->getBucket ('temp ' );
47
+
48
+ $ fileName = 'success.txt ' ;
49
+ $ fileContent = 'Success content ' ;
50
+ $ bucket ->saveFileContent ($ fileName , $ fileContent );
51
+
52
+ $ response = $ action ->run ($ bucket ->getName (), $ fileName );
53
+ $ this ->assertEquals ($ fileContent , $ response ->content );
54
+ $ this ->assertEquals ('text/plain ' , $ response ->getHeaders ()->get ('content-type ' ));
55
+ }
56
+
57
+ public function testInvalidBucket ()
58
+ {
59
+ $ fileStorage = $ this ->createFileStorage ();
60
+ $ action = $ this ->createAction (['fileStorage ' => $ fileStorage ]);
61
+
62
+ $ this ->setExpectedException ('yii\web\NotFoundHttpException ' );
63
+
64
+ $ response = $ action ->run ('unexisting ' , 'some.txt ' );
65
+ }
66
+
67
+ public function testInvalidFileName ()
68
+ {
69
+ $ fileStorage = $ this ->createFileStorage ();
70
+ $ action = $ this ->createAction (['fileStorage ' => $ fileStorage ]);
71
+ $ bucket = $ fileStorage ->getBucket ('temp ' );
72
+
73
+ $ this ->setExpectedException ('yii\web\NotFoundHttpException ' );
74
+
75
+ $ response = $ action ->run ($ bucket ->getName (), 'some.txt ' );
76
+ }
77
+
78
+ /**
79
+ * Data provider for [[testIsBucketAllowed()]]
80
+ * @return array test data
81
+ */
82
+ public function dataProviderIsBucketAllowed ()
83
+ {
84
+ return [
85
+ [
86
+ [
87
+ 'onlyBuckets ' => ['temp ' ]
88
+ ],
89
+ 'temp ' ,
90
+ true
91
+ ],
92
+ [
93
+ [
94
+ 'onlyBuckets ' => ['temp ' ]
95
+ ],
96
+ 'item ' ,
97
+ false
98
+ ],
99
+ [
100
+ [
101
+ 'exceptBuckets ' => ['temp ' ]
102
+ ],
103
+ 'temp ' ,
104
+ false
105
+ ],
106
+ [
107
+ [
108
+ 'exceptBuckets ' => ['temp ' ]
109
+ ],
110
+ 'item ' ,
111
+ true
112
+ ],
113
+ ];
114
+ }
115
+
116
+ /**
117
+ * @dataProvider dataProviderIsBucketAllowed
118
+ *
119
+ * @param array $actionConfig
120
+ * @param string $bucketName
121
+ * @param boolean $expectedResult
122
+ */
123
+ public function testIsBucketAllowed ($ actionConfig , $ bucketName , $ expectedResult )
124
+ {
125
+ $ actionConfig ['fileStorage ' ] = $ this ->createFileStorage ();
126
+ $ action = $ this ->createAction ($ actionConfig );
127
+
128
+ $ this ->assertEquals ($ expectedResult , $ this ->invoke ($ action , 'isBucketAllowed ' , [$ bucketName ]));
129
+ }
130
+ }
0 commit comments