1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* @link https://github.com/vuongxuongminh/yii2-searchable |
4
|
|
|
* @copyright Copyright (c) 2019 Vuong Xuong Minh |
5
|
|
|
* @license [New BSD License](http://www.opensource.org/licenses/bsd-license.php) |
6
|
|
|
*/ |
7
|
|
|
|
8
|
|
|
namespace vxm\test\unit\searchable; |
9
|
|
|
|
10
|
|
|
use Yii; |
11
|
|
|
|
12
|
|
|
use yii\helpers\ArrayHelper; |
13
|
|
|
|
14
|
|
|
/** |
15
|
|
|
* Class SearchableBehaviorTest |
16
|
|
|
* |
17
|
|
|
* @author Vuong Minh <[email protected]> |
18
|
|
|
* @since 1.0.0 |
19
|
|
|
*/ |
20
|
|
|
class SearchableBehaviorTest extends TestCase |
21
|
|
|
{ |
22
|
|
|
|
23
|
|
View Code Duplication |
public function testSync() |
|
|
|
|
24
|
|
|
{ |
25
|
|
|
$model = Model::findOne(1); |
26
|
|
|
$model->article = 'testSync'; |
27
|
|
|
$model->save(false); |
28
|
|
|
|
29
|
|
|
$models = Model::search('testSync')->all(); |
30
|
|
|
$this->assertEquals(1, count($models)); |
31
|
|
|
} |
32
|
|
|
|
33
|
|
View Code Duplication |
public function testUnSync() |
|
|
|
|
34
|
|
|
{ |
35
|
|
|
Model::withoutSyncingToSearch(function () { |
36
|
|
|
$model = Model::findOne(1); |
37
|
|
|
$model->article = 'testUnSync'; |
38
|
|
|
$model->save(false); |
39
|
|
|
|
40
|
|
|
$models = Model::search('testUnSync')->all(); |
41
|
|
|
$this->assertEquals(0, count($models)); |
42
|
|
|
}); |
43
|
|
|
} |
44
|
|
|
|
45
|
|
View Code Duplication |
public function testShouldBeSearchable() |
|
|
|
|
46
|
|
|
{ |
47
|
|
|
$model = Model::findOne(1); |
48
|
|
|
$model->shouldBeSearchable = false; |
49
|
|
|
$model->article = 'testShouldBeSearchable'; |
50
|
|
|
$model->save(false); |
51
|
|
|
|
52
|
|
|
$models = Model::search('testShouldBeSearchable')->all(); |
53
|
|
|
$this->assertEquals(0, count($models)); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
View Code Duplication |
public function testDeleteSync() |
|
|
|
|
57
|
|
|
{ |
58
|
|
|
$model = new Model([ |
59
|
|
|
'title' => 'testDeleteSync title', |
60
|
|
|
'article' => 'testDeleteSync article' |
61
|
|
|
]); |
62
|
|
|
|
63
|
|
|
$model->save(false); |
64
|
|
|
$modelSearch = Model::search('testDeleteSync title')->one(); |
65
|
|
|
$this->assertNotNull($modelSearch); |
66
|
|
|
$modelSearch->delete(); |
67
|
|
|
$modelSearch = Model::search('testDeleteSync title')->one(); |
68
|
|
|
$this->assertNull($modelSearch); |
69
|
|
|
} |
70
|
|
|
|
71
|
|
View Code Duplication |
public function testDeleteUnSync() |
|
|
|
|
72
|
|
|
{ |
73
|
|
|
$model = new Model([ |
74
|
|
|
'title' => 'testDeleteUnSync title', |
75
|
|
|
'article' => 'testDeleteUnSync article' |
76
|
|
|
]); |
77
|
|
|
|
78
|
|
|
$model->save(false); |
79
|
|
|
$modelSearch = Model::search('testDeleteUnSync title')->one(); |
80
|
|
|
$this->assertNotNull($modelSearch); |
81
|
|
|
|
82
|
|
|
Model::withoutSyncingToSearch(function () use ($model) { |
83
|
|
|
$model->delete(); |
84
|
|
|
$modelSearch = Model::searchIds('testDeleteUnSync title'); |
85
|
|
|
$this->assertNotEmpty($modelSearch); |
86
|
|
|
}); |
87
|
|
|
} |
88
|
|
|
|
89
|
|
|
public function testOrderBy() |
90
|
|
|
{ |
91
|
|
|
Model::makeAllSearchable(); |
92
|
|
|
$ids = Model::searchIds('Romeo'); |
93
|
|
|
$models = Model::search('Romeo')->all(); |
94
|
|
|
$modelIds = ArrayHelper::getColumn($models, 'id'); |
95
|
|
|
|
96
|
|
|
$this->assertEquals($ids, $modelIds); |
97
|
|
|
|
98
|
|
|
$models = Model::search('Romeo')->addOrderBy(['article' => SORT_DESC])->all(); |
99
|
|
|
$modelIds = ArrayHelper::getColumn($models, 'id'); |
100
|
|
|
|
101
|
|
|
$this->assertNotEquals($ids, $modelIds); |
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.
You can also find more detailed suggestions in the “Code” section of your repository.