Completed
Pull Request — master (#178)
by Corey
03:14
created

QuestionTest   A

Complexity

Total Complexity 5

Size/Duplication

Total Lines 182
Duplicated Lines 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
wmc 5
eloc 140
dl 0
loc 182
rs 10
c 1
b 0
f 0

2 Methods

Rating   Name   Duplication   Size   Complexity  
A setUp() 0 4 1
A testParseQuestionData() 0 31 4
1
<?php
2
3
namespace common\unit\models;
4
5
use Yii;
6
use Codeception\Specify;
7
use common\models\Question;
8
9
/**
10
 * Time test
11
 */
12
13
class QuestionTest extends \Codeception\Test\Unit {
14
  use Specify;
15
16
  private $question;
17
  private $questionData = [
18
    [
19
      'id' => 487,
20
      'user_id' => 1,
21
      'behavior_id' => 54,
22
      'user_behavior_id' => 2664,
23
      'question' => 1,
24
      'answer' => 'test1',
25
      'date' => '2019-10-27 21:07:35',
26
      'category_id' => 4,
27
    ], [
28
      'id' => 488,
29
      'user_id' => 1,
30
      'behavior_id' => 54,
31
      'user_behavior_id' => 2664,
32
      'question' => 2,
33
      'answer' => 'test2',
34
      'date' => '2019-10-27 21:07:35',
35
      'category_id' => 4,
36
    ], [
37
      'id' => 489,
38
      'user_id' => 1,
39
      'behavior_id' => 54,
40
      'user_behavior_id' => 2664,
41
      'question' => 3,
42
      'answer' => 'test3',
43
      'date' => '2019-10-27 21:07:35',
44
      'category_id' => 4,
45
    ], [
46
      'id' => 490,
47
      'user_id' => 1,
48
      'behavior_id' => 132,
49
      'user_behavior_id' => 2665,
50
      'question' => 1,
51
      'answer' => 'test1',
52
      'date' => '2019-10-27 21:07:35',
53
      'category_id' => 7,
54
    ], [
55
      'id' => 491,
56
      'user_id' => 1,
57
      'behavior_id' => 132,
58
      'user_behavior_id' => 2665,
59
      'question' => 2,
60
      'answer' => 'test2',
61
      'date' => '2019-10-27 21:07:35',
62
      'category_id' => 7,
63
    ], [
64
      'id' => 492,
65
      'user_id' => 1,
66
      'behavior_id' => 132,
67
      'user_behavior_id' => 2665,
68
      'question' => 3,
69
      'answer' => 'test3',
70
      'date' => '2019-10-27 21:07:35',
71
      'category_id' => 7,
72
    ], [
73
      'id' => 493,
74
      'user_id' => 1,
75
      'behavior_id' => NULL,
76
      'user_behavior_id' => 2666,
77
      'question' => 1,
78
      'answer' => 'test1',
79
      'date' => '2019-10-27 21:07:35',
80
      'category_id' => 2,
81
    ], [
82
      'id' => 494,
83
      'user_id' => 1,
84
      'behavior_id' => NULL,
85
      'user_behavior_id' => 2666,
86
      'question' => 2,
87
      'answer' => 'test2',
88
      'date' => '2019-10-27 21:07:35',
89
      'category_id' => 2,
90
    ], [
91
      'id' => 495,
92
      'user_id' => 1,
93
      'behavior_id' => NULL,
94
      'user_behavior_id' => 2666,
95
      'question' => 3,
96
      'answer' => 'test3',
97
      'date' => '2019-10-27 21:07:35',
98
      'category_id' => 2,
99
    ],
100
  ];
101
  private $userQuestions = [
102
    2664 => [
103
      'question' => [
104
        'user_behavior_id' => 2664,
105
        'behavior_name' => 'can\'t turn off thoughts',
106
      ],
107
      'answers' => [
108
        [
109
          'title' => 'How does it affect me? How do I act and feel?',
110
          'answer' => 'test1',
111
        ], [
112
          'title' => 'How does it affect the important people in my life?',
113
          'answer' => 'test2',
114
        ], [
115
          'title' => 'Why do I do this? What is the benefit for me?',
116
          'answer' => 'test3',
117
        ],
118
      ],
119
    ],
120
    2665 => [
121
      'question' => [
122
        'user_behavior_id' => 2665,
123
        'behavior_name' => 'aloneness',
124
      ],
125
      'answers' => [
126
        [
127
          'title' => 'How does it affect me? How do I act and feel?',
128
          'answer' => 'test1',
129
        ], [
130
          'title' => 'How does it affect the important people in my life?',
131
          'answer' => 'test2',
132
        ], [
133
          'title' => 'Why do I do this? What is the benefit for me?',
134
          'answer' => 'test3',
135
        ],
136
      ],
137
    ],
138
    2666=> [
139
      'question' => [
140
        'user_behavior_id' => 2666,
141
        'behavior_name' => 'some_custom_behavior',
142
      ],
143
      'answers' => [
144
        [
145
          'title' => 'How does it affect me? How do I act and feel?',
146
          'answer' => 'test1',
147
        ], [
148
          'title' => 'How does it affect the important people in my life?',
149
          'answer' => 'test2',
150
        ], [
151
          'title' => 'Why do I do this? What is the benefit for me?',
152
          'answer' => 'test3',
153
        ],
154
      ],
155
    ],
156
  ];
157
158
  public function setUp() {
159
    $this->question = $this->getMockBuilder('\common\models\Question')
160
         ->setMethods(['save', 'attributes'])
161
         ->getMock();
162
  }
163
164
  public function testParseQuestionData() {
165
    $questions = array_map(function ($d) {
166
      $q = $this->getMockBuilder('\common\models\Question')
167
         ->setMethods(['save', 'attributes'])
168
         ->getMock();
169
      $q->method('save')->willReturn(true);
170
      $q->method('attributes')
171
        ->willReturn([
172
          'id',
173
          'user_id',
174
          'behavior_id',
175
          'category_id',
176
          'user_behavior_id',
177
          'question',
178
          'answer',
179
          'date',
180
          'userBehavior',
181
        ]);
182
      foreach($d as $k => $v) {
183
        if($k === 'behavior_id' && $v === null) {
184
          $ub = new \StdClass();
185
          $ub->custom_behavior = 'some_custom_behavior';
186
          $q->userBehavior = $ub;
0 ignored issues
show
Bug introduced by
Accessing userBehavior on the interface PHPUnit\Framework\MockObject\MockObject suggest that you code against a concrete implementation. How about adding an instanceof check?
Loading history...
187
        } else {
188
          $q->$k = $v;
189
        }
190
      }
191
      return $q;
192
    }, $this->questionData);
193
    expect('parseQuestionData should return the correct structure with expected data', $this->assertEquals($this->question->parseQuestionData($questions), $this->userQuestions));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEquals($thi..., $this->userQuestions) targeting PHPUnit\Framework\Assert::assertEquals() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
194
    expect('parseQuestionData should return empty with the empty set', $this->assertEmpty($this->question->parseQuestionData([])));
0 ignored issues
show
Bug introduced by
Are you sure the usage of $this->assertEmpty($this...eQuestionData(array())) targeting PHPUnit\Framework\Assert::assertEmpty() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
195
  }
196
}
197