Skip to content
This repository was archived by the owner on Dec 1, 2021. It is now read-only.

Commit 0a63d01

Browse files
committed
Unit test for DownloadAction added
1 parent 77868f9 commit 0a63d01

File tree

2 files changed

+150
-2
lines changed

2 files changed

+150
-2
lines changed

tests/DownloadActionTest.php

Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
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+
}

tests/TestCase.php

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,14 +47,15 @@ protected function tearDown()
4747
* @param array $config The application configuration, if needed
4848
* @param string $appClass name of the application class to create
4949
*/
50-
protected function mockApplication($config = [], $appClass = '\yii\console\Application')
50+
protected function mockApplication($config = [], $appClass = '\yii\web\Application')
5151
{
5252
new $appClass(ArrayHelper::merge([
5353
'id' => 'testapp',
5454
'basePath' => __DIR__,
5555
'vendorPath' => $this->getVendorPath(),
5656
'components' => [
57-
'urlManager' => [
57+
'request' => [
58+
'hostInfo' => 'http://domain.com',
5859
'scriptUrl' => '/index.php'
5960
],
6061
],
@@ -125,4 +126,21 @@ public function getTestTmpPath()
125126
{
126127
return Yii::getAlias('@yii2tech/tests/unit/filestorage/runtime/test_file_storage_tmp');
127128
}
129+
130+
/**
131+
* Invokes object method, even if it is private or protected.
132+
* @param object $object object.
133+
* @param string $method method name.
134+
* @param array $args method arguments
135+
* @return mixed method result
136+
*/
137+
protected function invoke($object, $method, array $args = [])
138+
{
139+
$classReflection = new \ReflectionClass(get_class($object));
140+
$methodReflection = $classReflection->getMethod($method);
141+
$methodReflection->setAccessible(true);
142+
$result = $methodReflection->invokeArgs($object, $args);
143+
$methodReflection->setAccessible(false);
144+
return $result;
145+
}
128146
}

0 commit comments

Comments
 (0)