Skip to content

Commit 9824b35

Browse files
committed
Add first simple unit tests
1 parent 54974f7 commit 9824b35

File tree

4 files changed

+70
-6
lines changed

4 files changed

+70
-6
lines changed

phpunit.xml.dist

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@
77
convertWarningsToExceptions="true"
88
stopOnFailure="false">
99
<testsuites>
10-
<testsuite name="Integration">
11-
<directory>./tests/integration</directory>
10+
<testsuite name="Functional">
11+
<directory>./tests/functional</directory>
1212
</testsuite>
1313
<testsuite name="Unit">
1414
<directory>./tests/unit</directory>

src/UploadFileBehavior.php

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,9 @@
1010
use yii\helpers\FileHelper;
1111
use backend\models\blog\BlogArticles;
1212
use yii\base\InvalidCallException;
13-
14-
13+
use yii\base\InvalidArgumentException;
14+
use yii\base\InvalidConfigException;
15+
use yii\base\NotSupportedException;
1516

1617
/**
1718
* UploadFileBehavior class
@@ -150,6 +151,25 @@ class UploadFileBehavior extends Behavior
150151
*/
151152
private $time = false;
152153

154+
/**
155+
* @inheritdoc
156+
*/
157+
public function init()
158+
{
159+
parent::init();
160+
161+
if (!class_exists(Image::class)) {
162+
throw new NotSupportedException("Yii2-imagine extension is required to use the UploadImageBehavior");
163+
}
164+
165+
if ($this->directories === null) {
166+
throw new InvalidConfigException('The "directories" property must be set.');
167+
}
168+
if (!is_array($this->directories)) {
169+
throw new InvalidConfigException('The "directories" property must be an array.');
170+
}
171+
}
172+
153173
/**
154174
* {@inheritdoc}
155175
*/
@@ -289,7 +309,6 @@ protected function getNewFileName()
289309
protected function isFile()
290310
{
291311
return $this->getFileInstance() && $this->getFileInstance()->tempName;
292-
293312
}
294313

295314
}

tests/data/test-image.jpg

464 KB
Loading

tests/unit/UploadFileBehaviorTest.php

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,57 @@
22
//UploadFileBehavior
33
namespace tests\unit;
44

5-
use yii\base\InvalidParamException;
65
use Yii;
6+
use yii\web\UploadedFile;
7+
use yii\base\InvalidParamException;
78

89
class UploadFileBehaviorTest extends \tests\TestCase
910
{
1011

12+
/**
13+
* @inheritdoc
14+
*/
15+
public static function setUpBeforeClass()
16+
{
17+
parent::setUpBeforeClass();
18+
19+
$_FILES = [
20+
'User[image]' => [
21+
'name' => 'test-image.jpg',
22+
'type' => 'image/jpeg',
23+
'size' => 74463,
24+
'tmp_name' => __DIR__ . '/data/test-image.jpg',
25+
'error' => 0,
26+
],
27+
];
28+
}
29+
30+
public function testGetFileInstance()
31+
{
32+
$file = UploadedFile::getInstanceByName('User[image]');
33+
$this->assertTrue($file instanceof UploadedFile);
34+
}
35+
36+
public function testIsFile()
37+
{
38+
$file = UploadedFile::getInstanceByName('User[image]');
39+
$this->assertTrue($file && $file->tempName);
40+
}
41+
42+
public function testGetNewFileName()
43+
{
44+
$newFileName = "name";
45+
$file = UploadedFile::getInstanceByName('User[image]');
46+
$baseName = $file->baseName;
47+
$ext = $file->extension;
48+
49+
$result = $newFileName ?
50+
$newFileName . '.' . $file->extension :
51+
$file->baseName . '_' . $this->time . '.' . $file->extension;
52+
53+
$this->assertEquals('name.jpg', $result);
54+
}
55+
1156
public function testSome()
1257
{
1358
$this->assertTrue(true);

0 commit comments

Comments
 (0)