-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' of github.com:Horat1us/yii2-base
- Loading branch information
Showing
6 changed files
with
194 additions
and
31 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<?php | ||
|
||
namespace Horat1us\Yii\Behaviors; | ||
|
||
use yii\base\Behavior; | ||
use yii\base\Model; | ||
use yii\web\UploadedFile; | ||
|
||
class FileLoaderBehavior extends Behavior | ||
{ | ||
/** @var string */ | ||
public $attribute; | ||
|
||
/** @var bool */ | ||
public $multiple = false; | ||
|
||
public function events() | ||
{ | ||
return [ | ||
Model::EVENT_BEFORE_VALIDATE => 'load', | ||
]; | ||
} | ||
|
||
public function load() | ||
{ | ||
$this->owner->{$this->attribute} = $this->multiple | ||
? UploadedFile::getInstances($this->owner, $this->attribute) | ||
: UploadedFile::getInstance($this->owner, $this->attribute); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -39,5 +39,6 @@ public function tearDown() | |
{ | ||
\Yii::$app = null; | ||
\Yii::$container = new Container(); | ||
$_FILES = []; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
<?php | ||
|
||
namespace Horat1us\Yii\Tests\Behaviors; | ||
|
||
use Faker\Factory; | ||
use Horat1us\Yii\Tests\AbstractTestCase; | ||
use Horat1us\Yii\Tests\Mocks\FileLoaderBehaviorMock; | ||
use yii\web\UploadedFile; | ||
|
||
class FileLoaderBehaviorTest extends AbstractTestCase | ||
{ | ||
public function setUp() | ||
{ | ||
$faker = Factory::create(); | ||
$file = $faker->image(); | ||
$_FILES = [ | ||
'FileLoaderBehaviorMock' => [ | ||
'name' => [ | ||
'file' => $faker->name, | ||
], | ||
'type' => [ | ||
'file' => 'application/octet-stream', | ||
], | ||
'tmp_name' => [ | ||
'file' => $file, | ||
], | ||
'error' => [ | ||
'file' => 0, | ||
], | ||
'size' => [ | ||
'file' => filesize($file), | ||
], | ||
], | ||
]; | ||
return parent::setUp(); | ||
} | ||
|
||
public function testLoadingSingleFile() | ||
{ | ||
$model = new FileLoaderBehaviorMock(); | ||
|
||
$model->validate(); | ||
$this->assertInstanceOf(UploadedFile::class, $model->file); | ||
} | ||
|
||
public function testLoadingMultipleFiles() | ||
{ | ||
$model = new FileLoaderBehaviorMock(['multiple' => true,]); | ||
$model->validate(); | ||
$this->assertNotEmpty($model->file); | ||
$this->assertTrue(is_array($model->file)); | ||
$this->assertGreaterThanOrEqual(1, count($model->file)); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
<?php | ||
|
||
namespace Horat1us\Yii\Tests\Mocks; | ||
|
||
use Horat1us\Yii\Behaviors\FileLoaderBehavior; | ||
use yii\db\ActiveRecord; | ||
use yii\web\UploadedFile; | ||
|
||
class FileLoaderBehaviorMock extends ActiveRecord | ||
{ | ||
/** @var bool */ | ||
public $multiple = false; | ||
|
||
/** @var UploadedFile|UploadedFile[] */ | ||
public $file; | ||
|
||
public function behaviors() | ||
{ | ||
return [ | ||
'file' => [ | ||
'class' => FileLoaderBehavior::class, | ||
'attribute' => 'file', | ||
'multiple' => $this->multiple, | ||
], | ||
]; | ||
} | ||
} |