1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace site\tests\unit\models; |
4
|
|
|
|
5
|
|
|
use Yii; |
6
|
|
|
use \site\models\CustomBehaviorForm; |
7
|
|
|
use \yii\di\Container; |
8
|
|
|
use \common\models\Category; |
9
|
|
|
|
10
|
|
|
class CustomBehaviorFormTest extends \Codeception\Test\Unit { |
11
|
|
|
|
12
|
|
|
private $user; |
13
|
|
|
private $category; |
|
|
|
|
14
|
|
|
|
15
|
|
|
public function setUp() { |
16
|
|
|
$this->container = new Container; |
|
|
|
|
17
|
|
|
$this->container->set(\common\interfaces\UserInterface::class, '\site\tests\_support\MockUser'); |
18
|
|
|
$this->user = $this->container->get(\common\interfaces\UserInterface::class); |
19
|
|
|
} |
20
|
|
|
|
21
|
|
|
public function testRules() { |
22
|
|
|
$category = new Category(); |
23
|
|
|
$form = new CustomBehaviorForm($this->user, $category); |
24
|
|
|
|
25
|
|
|
$form->attributes = []; |
26
|
|
|
expect('with no values, the form should not pass validation', $this->assertFalse($form->validate())); |
|
|
|
|
27
|
|
|
|
28
|
|
|
$form->attributes = [ |
29
|
|
|
'name' => 'Clapping hands and smiling', |
30
|
|
|
'category_id' => 'definitely not a category id', |
31
|
|
|
]; |
32
|
|
|
expect('with an invalid category_id the form should not pass validation', $this->assertFalse($form->validate())); |
|
|
|
|
33
|
|
|
|
34
|
|
|
$form->attributes = [ |
35
|
|
|
'category_id' => 'definitely not a category id', |
36
|
|
|
]; |
37
|
|
|
expect('without a name the form should not pass validation', $this->assertFalse($form->validate())); |
|
|
|
|
38
|
|
|
$form->attributes = [ |
39
|
|
|
'name' => 'Clapping hands and smiling', |
40
|
|
|
'category_id' => 42, |
41
|
|
|
]; |
42
|
|
|
expect('with an invalid category_id the form should not pass validation', $this->assertFalse($form->validate())); |
|
|
|
|
43
|
|
|
|
44
|
|
|
$form->attributes = [ |
45
|
|
|
'name' => 'Clapping hands and smiling', |
46
|
|
|
'category_id' => 6, |
47
|
|
|
]; |
48
|
|
|
expect('with a name and valid category_id, the form should validate', $this->assertTrue($form->validate())); |
|
|
|
|
49
|
|
|
|
50
|
|
|
$form->attributes = [ |
51
|
|
|
'name' => 'Clapping hands and smiling', |
52
|
|
|
'category_id' => 6, |
53
|
|
|
]; |
54
|
|
|
expect('with a name and valid category_id, the form should validate', $this->assertTrue($form->validate())); |
|
|
|
|
55
|
|
|
|
56
|
|
|
$form->attributes = [ |
57
|
|
|
'name' => ' Clapping hands and smiling .. -- aa ', |
58
|
|
|
'category_id' => 6, |
59
|
|
|
]; |
60
|
|
|
$form->validate(); |
61
|
|
|
expect('the form should trim the name when validating', $this->assertEquals('Clapping hands and smiling .. -- aa', $form->name)); |
|
|
|
|
62
|
|
|
} |
63
|
|
|
|
64
|
|
|
public function testSave() { |
65
|
|
|
$category = new Category(); |
66
|
|
|
$user_id = 123; |
67
|
|
|
$this->user->id = $user_id; |
68
|
|
|
|
69
|
|
|
$custom_behavior = $this->getMockBuilder(\common\models\CustomBehavior::class) |
70
|
|
|
->setmethods(['getisnewrecord', 'attributes', 'save']) |
71
|
|
|
->getmock(); |
72
|
|
|
$custom_behavior |
73
|
|
|
->method('attributes') |
74
|
|
|
->willReturn([ |
75
|
|
|
'id', |
76
|
|
|
'user_id', |
77
|
|
|
'category_id', |
78
|
|
|
'name', |
79
|
|
|
'created_at', |
80
|
|
|
'updated_at', |
81
|
|
|
]); |
82
|
|
|
$custom_behavior |
83
|
|
|
->expects($this->once()) |
84
|
|
|
->method('save') |
85
|
|
|
->willReturn($custom_behavior); |
86
|
|
|
|
87
|
|
|
Yii::$container->set(\common\interfaces\CustomBehaviorInterface::class, $custom_behavior); |
88
|
|
|
$form = new CustomBehaviorForm($this->user, $category); |
89
|
|
|
$name = 'a new custom behavior'; |
90
|
|
|
$category_id = 4; |
91
|
|
|
$form->name = $name; |
92
|
|
|
$form->category_id = $category_id; |
93
|
|
|
|
94
|
|
|
$result = $form->save(); |
95
|
|
|
expect('should have name correctly set', $this->assertEquals($result->name, $name)); |
|
|
|
|
96
|
|
|
expect('should have category_id correctly set', $this->assertEquals($result->category_id, $category_id)); |
|
|
|
|
97
|
|
|
expect('should have name correctly set', $this->assertEquals($result->user_id, $user_id)); |
|
|
|
|
98
|
|
|
} |
99
|
|
|
} |
100
|
|
|
|