Passed
Push — master ( 482b77...45d699 )
by Julito
09:12
created

GetSessionFromExtraFieldTest::test()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 109
Code Lines 68

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 68
c 1
b 0
f 0
nc 1
nop 0
dl 0
loc 109
rs 8.6981

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
require_once __DIR__.'/V2TestCase.php';
5
require_once __DIR__.'/../../../../vendor/autoload.php';
6
7
8
/**
9
 * Class GetSessionFromExtraFieldTest
10
 *
11
 * GET_SESSION_FROM_EXTRA_FIELD webservice unit tests
12
 */
13
class GetSessionFromExtraFieldTest extends V2TestCase
14
{
15
    public function action()
16
    {
17
        return 'get_session_from_extra_field';
18
    }
19
20
    /**
21
     * creates two extra fields and 2 sessions
22
     * asserts that the sessions can be found one by one but not together
23
     *
24
     */
25
    public function test()
26
    {
27
        // create 2 extra fields
28
        $extraFieldModel = new ExtraField('session');
29
        $firstExtraFieldName = 'firstExtraField'.time();
30
        $firstExtraFieldId = $extraFieldModel->save(
31
            [
32
                'field_type' => ExtraField::FIELD_TYPE_TEXT,
33
                'variable' => $firstExtraFieldName,
34
                'display_text' => $firstExtraFieldName,
35
                'visible_to_self' => 1,
36
                'visible_to_others' => 1,
37
                'changeable' => 1,
38
                'filter' => 1,
39
            ]
40
        );
41
        $secondExtraFieldName = 'secondExtraField'.time();
42
        $secondExtraFieldId = $extraFieldModel->save(
43
            [
44
                'field_type' => ExtraField::FIELD_TYPE_TEXT,
45
                'variable' => $secondExtraFieldName,
46
                'display_text' => $secondExtraFieldName,
47
                'visible_to_self' => 1,
48
                'visible_to_others' => 1,
49
                'changeable' => 1,
50
                'filter' => 1,
51
            ]
52
        );
53
54
        // create 2 sessions
55
        $firstSessionId = SessionManager::create_session(
56
            'First session'.time(),
57
            '2019-01-01 00:00',
58
            '2019-08-31 00:00',
59
            '2019-01-01 00:00',
60
            '2019-08-31 00:00',
61
            '2019-01-01 00:00',
62
            '2019-08-31 00:00',
63
            null,
64
            null
65
        );
66
        $secondSessionId = SessionManager::create_session(
67
            'Second session'.time(),
68
            '2019-09-01 00:00',
69
            '2019-12-31 00:00',
70
            '2019-09-01 00:00',
71
            '2019-12-31 00:00',
72
            '2019-09-01 00:00',
73
            '2019-12-31 00:00',
74
            null,
75
            null
76
        );
77
78
        // assign unique distinct value in first field to each session
79
        SessionManager::update_session_extra_field_value($firstSessionId, $firstExtraFieldName, $firstSessionId);
0 ignored issues
show
Bug introduced by
It seems like $firstSessionId can also be of type false; however, parameter $value of SessionManager::update_session_extra_field_value() does only seem to accept string, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
        SessionManager::update_session_extra_field_value($firstSessionId, $firstExtraFieldName, /** @scrutinizer ignore-type */ $firstSessionId);
Loading history...
Bug introduced by
It seems like $firstSessionId can also be of type false and string; however, parameter $sessionId of SessionManager::update_session_extra_field_value() does only seem to accept integer, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

79
        SessionManager::update_session_extra_field_value(/** @scrutinizer ignore-type */ $firstSessionId, $firstExtraFieldName, $firstSessionId);
Loading history...
80
        SessionManager::update_session_extra_field_value($secondSessionId, $firstExtraFieldName, $secondSessionId);
81
82
        // assign the same value in second field to all sessions
83
        $commonValue = 'common value';
84
        SessionManager::update_session_extra_field_value($firstSessionId, $secondExtraFieldName, $commonValue);
85
        SessionManager::update_session_extra_field_value($secondSessionId, $secondExtraFieldName, $commonValue);
86
87
        // assert that the correct session id is returned using each unique value
88
        $this->assertSame(
89
            $firstSessionId,
90
            $this->integer(
91
                [
92
                    'field_name' => $firstExtraFieldName,
93
                    'field_value' => $firstSessionId,
94
                ]
95
            )
96
        );
97
        $this->assertSame(
98
            $secondSessionId,
99
            $this->integer(
100
                [
101
                    'field_name' => $firstExtraFieldName,
102
                    'field_value' => $secondSessionId,
103
                ]
104
            )
105
        );
106
107
        // assert search for common value in second field generates the right error message
108
        $this->assertSame(
109
            get_lang('MoreThanOneSessionMatched'),
110
            $this->errorMessageString(
111
                [
112
                    'field_name' => $secondExtraFieldName,
113
                    'field_value' => $commonValue,
114
                ]
115
            )
116
        );
117
118
        // assert search for unknown value generates the right error message
119
        $this->assertSame(
120
            get_lang('NoSessionMatched'),
121
            $this->errorMessageString(
122
                [
123
                    'field_name' => $secondExtraFieldName,
124
                    'field_value' => 'non-existent value',
125
                ]
126
            )
127
        );
128
129
        // clean up
130
        SessionManager::delete($firstSessionId);
0 ignored issues
show
Bug introduced by
It seems like $firstSessionId can also be of type false and integer and string; however, parameter $id_checked of SessionManager::delete() does only seem to accept array, maybe add an additional type check? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

130
        SessionManager::delete(/** @scrutinizer ignore-type */ $firstSessionId);
Loading history...
131
        SessionManager::delete($secondSessionId);
132
        $extraFieldModel->delete($firstExtraFieldId);
133
        $extraFieldModel->delete($secondExtraFieldId);
134
    }
135
}
136