SearchableTest::testSearchMode()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 10

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 10
rs 9.9332
c 0
b 0
f 0
cc 1
nc 1
nop 0
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
/**
13
 * Class SearchableTest
14
 *
15
 * @author Vuong Minh <[email protected]>
16
 * @since 1.0.0
17
 */
18
class SearchableTest extends TestCase
19
{
20
21
    public function testBootable()
22
    {
23
        $this->assertNotNull(Yii::$app->get('searchable', false));
24
        $this->assertTrue(isset(Yii::$app->controllerMap['searchable']));
25
    }
26
27
    public function testMakeSearchable()
28
    {
29
        Model::deleteAllFromSearch();
30
        Model::makeAllSearchable();
31
32
        $this->assertTrue(file_exists(Model::getSearchable()->storagePath . '/articles.index'));
33
34
        $models = Model::find()->limit(5)->all();
35
36
        Model::deleteAllFromSearch();
37
        Model::makeSearchable($models);
38
39
        $this->assertTrue(file_exists(Model::getSearchable()->storagePath . '/articles.index'));
40
    }
41
42
    public function testDeleteSearchable()
43
    {
44
        Model::makeAllSearchable();
45
        $model = new Model([
46
            'title' => 'deleteSearchable',
47
            'article' => 'deleteSearchable'
48
        ]);
49
        $model->save();
50
        $this->assertNotEmpty(Model::searchIds('deleteSearchable'));
51
        Model::deleteSearchable($model);
52
        $this->assertEmpty(Model::searchIds('deleteSearchable'));
53
        $model->delete();
54
    }
55
56
    public function testDeleteAllFromSearch()
57
    {
58
        Model::makeAllSearchable();
59
        Model::deleteAllFromSearch();
60
61
        $this->assertFalse(file_exists(Model::getSearchable()->storagePath . '/articles.index'));
62
    }
63
64
    public function testSearchMode()
65
    {
66
        Model::deleteAllFromSearch();
67
        Model::makeAllSearchable();
68
69
        $booleanIds = Model::searchIds('romeo or hamlet', 'boolean');
70
        $fuzzyIds = Model::searchIds('romeo or hamlet', 'fuzzy', ['fuzziness' => true]);
71
72
        $this->assertNotEquals($fuzzyIds, $booleanIds);
73
    }
74
75
}
76