Skip to content

Commit 69d6224

Browse files
committed
- Fixing styles.
- Unit test & cover all lines from App\Controllers\ServerSentEvent - Unit test remaning functions.
1 parent b58415e commit 69d6224

File tree

9 files changed

+366
-88
lines changed

9 files changed

+366
-88
lines changed

app/Helper.php

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,9 +279,14 @@ public static function getDbIpUrlFromRequestInfo($request_info)
279279
return false;
280280
}
281281

282+
public static function isTestingEnv()
283+
{
284+
return env('APP_ENV') === 'testing';
285+
}
286+
282287
public static function getTestEnvMockVar($var_name, $fallback)
283288
{
284-
if (env('APP_ENV') === 'testing' && isset($GLOBALS[$var_name])) {
289+
if (self::isTestingEnv() && isset($GLOBALS[$var_name])) {
285290
return $GLOBALS[$var_name];
286291
}
287292

app/Http/Controllers/ServerSentEventController.php

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace App\Http\Controllers;
44

5+
use App\Helper;
56
use App\Sse;
67
use Closure;
78
use Illuminate\Http\Request;
@@ -10,6 +11,8 @@
1011
class ServerSentEventController extends Controller
1112
{
1213
const CHUNK_SEPARATOR = "\n\n";
14+
const LINES_SEPARATOR = "\n";
15+
const KV_ASSIGNER = ': ';
1316

1417
private function parsePayload($payload)
1518
{
@@ -21,10 +24,10 @@ private function parsePayload($payload)
2124
$lines = [];
2225

2326
foreach ($payload as $key => $value) {
24-
$lines[] = "$key: $value";
27+
$lines[] = $key.self::KV_ASSIGNER.$value;
2528
}
2629

27-
$parsed_payload = implode("\n", $lines);
30+
$parsed_payload = implode(self::LINES_SEPARATOR, $lines);
2831
}
2932

3033
return $parsed_payload.self::CHUNK_SEPARATOR;
@@ -51,9 +54,11 @@ private function mainLoop(Request $request, Closure $main_loop, $timeout_seconds
5154
$id = 0;
5255

5356
while (1) {
57+
// @codeCoverageIgnoreStart
5458
if (connection_aborted()) {
5559
break;
5660
}
61+
// @codeCoverageIgnoreEnd
5762

5863
$payload = $main_loop($id);
5964

@@ -65,6 +70,15 @@ private function mainLoop(Request $request, Closure $main_loop, $timeout_seconds
6570
$this->sendPayload($payload);
6671
}
6772

73+
if (Helper::isTestingEnv()) {
74+
// You can't put this line of code after ob_get_level()
75+
// because then PHPUnit starts running it calls ob_start()
76+
// and that messes up the expectations
77+
flush();
78+
break;
79+
}
80+
81+
// @codeCoverageIgnoreStart
6882
if (ob_get_level() > 0) {
6983
ob_flush();
7084
ob_end_flush();
@@ -74,6 +88,7 @@ private function mainLoop(Request $request, Closure $main_loop, $timeout_seconds
7488

7589
sleep($timeout_seconds);
7690
$id++;
91+
// @codeCoverageIgnoreEnd
7792
}
7893
});
7994

@@ -87,9 +102,17 @@ private function mainLoop(Request $request, Closure $main_loop, $timeout_seconds
87102

88103
public function channel(Request $request, $channel)
89104
{
90-
$listener = Sse::listen($channel);
105+
$mocked_last_event = Helper::getTestEnvMockVar('Sse::last_event', null);
106+
107+
$listener = Sse::listen($channel, $mocked_last_event);
91108

92109
return $this->mainLoop($request, function ($id) use ($listener) {
110+
$mocked_content = Helper::getTestEnvMockVar('Sse::mocked_content', null);
111+
112+
if ($mocked_content !== null) {
113+
return $mocked_content;
114+
}
115+
93116
$event = $listener();
94117

95118
if (!$event) {

app/Http/Controllers/SurveyController.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -201,13 +201,13 @@ public function pause($uuid, Request $request)
201201

202202
$status = Surveys::pause($uuid, $request->user()->id);
203203

204-
$mocked_survey_already_paused = Helper::getTestEnvMockVar('Surveys::ERR_PAUSE_SURVEY_ALREADY_PAUSED', $status === Surveys::ERR_PAUSE_SURVEY_ALREADY_PAUSED);
204+
$mocked_survey_invalid_status = Helper::getTestEnvMockVar('Surveys::ERR_PAUSE_SURVEY_INVALID_STATUS', $status === Surveys::ERR_PAUSE_SURVEY_INVALID_STATUS);
205205

206-
if ($status === Surveys::ERR_PAUSE_SURVEY_INVALID_STATUS) {
206+
if ($mocked_survey_invalid_status) {
207207
$request->session()->flash('warning', 'Survey "'.$uuid.'" invalid status, it should be "ready".');
208208

209209
return redirect()->route('survey.edit', $uuid);
210-
} elseif ($mocked_survey_already_paused) {
210+
} elseif ($status === Surveys::ERR_PAUSE_SURVEY_ALREADY_PAUSED) {
211211
$request->session()->flash('warning', 'Survey "'.$uuid.'" is already paused.');
212212

213213
return redirect()->route('survey.edit', $uuid);

app/Questions.php

Lines changed: 40 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -21,17 +21,16 @@ public function getUpdatedAtRfc1123Attribute()
2121

2222
public static function getBySurvey($q_uuid, $s_id)
2323
{
24-
return (
2524
$questions = self::where([
2625
'uuid' => $q_uuid,
2726
'survey_id' => $s_id,
2827
'active' => '1',
2928
'version' => self::getLastVersion($s_id),
30-
])->get()
31-
) &&
32-
count($questions) === 1
33-
? $questions[0]
34-
: null;
29+
])->get();
30+
31+
$is_valid_question = $questions && count($questions) === 1;
32+
33+
return $is_valid_question ? $questions[0] : null;
3534
}
3635

3736
public static function getAllBySurveyIdPaginated($s_id)
@@ -40,9 +39,7 @@ public static function getAllBySurveyIdPaginated($s_id)
4039
'survey_id' => $s_id,
4140
'active' => '1',
4241
'version' => self::getLastVersion($s_id),
43-
])
44-
->orderBy('updated_at', 'desc')
45-
->paginate(5);
42+
])->orderBy('updated_at', 'desc')->paginate(5);
4643
}
4744

4845
public static function getAllBySurveyIdUnpaginated($s_id)
@@ -51,7 +48,10 @@ public static function getAllBySurveyIdUnpaginated($s_id)
5148
'survey_id' => $s_id,
5249
'active' => '1',
5350
'version' => self::getLastVersion($s_id),
54-
])->orderBy('order', 'asc')->get()->all();
51+
])
52+
->orderBy('order', 'asc')
53+
->get()
54+
->all();
5555
}
5656

5757
public static function getAllBySurveyId($s_id, $start_from = 0)
@@ -61,8 +61,8 @@ public static function getAllBySurveyId($s_id, $start_from = 0)
6161
'active' => '1',
6262
'version' => self::getLastVersion($s_id),
6363
])
64-
->orderBy('id', 'asc')
65-
->get();
64+
->orderBy('id', 'asc')
65+
->get();
6666
}
6767

6868
public static function getAllBySurveyIdOrdered($s_id, $start_from = 0)
@@ -72,8 +72,8 @@ public static function getAllBySurveyIdOrdered($s_id, $start_from = 0)
7272
'active' => '1',
7373
'version' => self::getLastVersion($s_id),
7474
])
75-
->orderBy('order', 'asc')
76-
->get();
75+
->orderBy('order', 'asc')
76+
->get();
7777
}
7878

7979
public static function deleteByOwner($s_uuid, $q_uuid, $user_id)
@@ -100,53 +100,38 @@ public static function deleteByOwner($s_uuid, $q_uuid, $user_id)
100100
'active' => '0',
101101
]);
102102

103-
if (!$update_active) {
103+
$mocked_update_active = Helper::getTestEnvMockVar('Questions::update_active_return', $update_active);
104+
105+
if (!$mocked_update_active) {
104106
return false;
105107
}
106108

107109
self::where([
108110
'survey_id' => $survey->id,
109111
'active' => '1',
110112
'version' => $version,
111-
])->where(
112-
'order', '>', $questions[0]->order
113-
)->decrement(
114-
'order', $questions[0]->order - 1
115-
);
113+
])
114+
->where('order', '>', $questions[0]->order)
115+
->decrement('order', $questions[0]->order - 1);
116116

117117
return true;
118118
}
119119

120-
public static function getByUuid($uuid)
121-
{
122-
return (
123-
$questions = self::where([
124-
'uuid' => $uuid,
125-
'active' => '1',
126-
])->get()
127-
) &&
128-
count($questions) === 1
129-
? $questions[0]
130-
: null;
131-
}
132-
133120
public static function getNextInOrder($s_id)
134121
{
135-
return (
136-
$question = self::where([
137-
'survey_id' => $s_id,
138-
'active' => '1',
139-
'version' => self::getLastVersion($s_id),
140-
])
141-
->orderBy('order', 'desc')
142-
->limit(1)
143-
->get()
144-
->all()
145-
) &&
146-
is_array($question) &&
147-
count($question) === 1
148-
? $question[0]->order + 1
149-
: 1;
122+
$question = self::where([
123+
'survey_id' => $s_id,
124+
'active' => '1',
125+
'version' => self::getLastVersion($s_id),
126+
])
127+
->orderBy('order', 'desc')
128+
->limit(1)
129+
->get()
130+
->all();
131+
132+
$is_valid_question = $question && is_array($question) && count($question) === 1;
133+
134+
return $is_valid_question ? $question[0]->order + 1 : 1;
150135
}
151136

152137
public static function updateOrder($id, $order)
@@ -194,6 +179,12 @@ public static function getLastVersion($survey_id)
194179

195180
public static function getAllByVersion($survey_id, $version)
196181
{
182+
$questions = self::where([
183+
'version' => $version,
184+
'survey_id' => $survey_id,
185+
'active' => '1',
186+
])->orderBy('order')->get()->all();
187+
197188
return array_map(function ($question) {
198189
$question->answers = array_map(function ($questions_answers_version) use ($question) {
199190
return [
@@ -203,10 +194,6 @@ public static function getAllByVersion($survey_id, $version)
203194
}, range(1, QuestionsOptionsView::getById($question->id)->last_version));
204195

205196
return $question;
206-
}, self::where([
207-
'version' => $version,
208-
'survey_id' => $survey_id,
209-
'active' => '1',
210-
])->orderBy('order')->get()->all());
197+
}, $questions);
211198
}
212199
}

app/Sse.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,10 @@ public static function listen($channel, $last_event = null, $limit = 1)
1414

1515
return function () use ($channel, &$last_event, $limit) {
1616
$events = self::where('created_at', '>', $last_event)
17-
->where('channel', '=', $channel)
18-
->limit($limit)
19-
->get()
20-
->all();
17+
->where('channel', '=', $channel)
18+
->limit($limit)
19+
->get()
20+
->all();
2121

2222
if (count($events) === 0) {
2323
return null;

app/Surveys.php

Lines changed: 28 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,18 @@ public static function run($uuid, $user_id)
7676

7777
if (!$survey) {
7878
return self::ERR_RUN_SURVEY_NOT_FOUND;
79-
} elseif ($survey->status !== 'draft') {
80-
return self::ERR_RUN_SURVEY_INVALID_STATUS;
81-
} elseif ($survey->status === 'ready') {
79+
}
80+
81+
if ($survey->status === 'ready') {
8282
return self::ERR_RUN_SURVEY_ALREADY_RUNNING;
8383
}
8484

85+
$mocked_invalid_status = Helper::getTestEnvMockVar('Surveys::ERR_RUN_SURVEY_INVALID_STATUS', $survey->status !== 'draft');
86+
87+
if ($mocked_invalid_status) {
88+
return self::ERR_RUN_SURVEY_INVALID_STATUS;
89+
}
90+
8591
$survey->status = 'ready';
8692
$survey->save();
8793

@@ -101,12 +107,18 @@ public static function pause($uuid, $user_id)
101107

102108
if (!$survey) {
103109
return self::ERR_PAUSE_SURVEY_NOT_FOUND;
104-
} elseif ($survey->status !== 'ready') {
105-
return self::ERR_PAUSE_SURVEY_INVALID_STATUS;
106-
} elseif ($survey->status === 'draft') {
110+
}
111+
112+
if ($survey->status === 'draft') {
107113
return self::ERR_PAUSE_SURVEY_ALREADY_PAUSED;
108114
}
109115

116+
$mocked_invalid_status = Helper::getTestEnvMockVar('Surveys::ERR_PAUSE_SURVEY_INVALID_STATUS', $survey->status !== 'ready');
117+
118+
if ($mocked_invalid_status) {
119+
return self::ERR_PAUSE_SURVEY_INVALID_STATUS;
120+
}
121+
110122
$survey->status = 'draft';
111123
$survey->save();
112124

@@ -118,10 +130,10 @@ public static function pause($uuid, $user_id)
118130
public static function getAvailables()
119131
{
120132
return DB::table('surveys')
121-
->select('surveys.*', 'users.name as author_name')
122-
->join('users', 'users.id', '=', 'surveys.user_id')
123-
->where('surveys.status', '=', 'ready')
124-
->get();
133+
->select('surveys.*', 'users.name as author_name')
134+
->join('users', 'users.id', '=', 'surveys.user_id')
135+
->where('surveys.status', '=', 'ready')
136+
->get();
125137
}
126138

127139
/************************************************/
@@ -138,23 +150,18 @@ public static function isRunning($uuid)
138150
return self::ERR_IS_RUNNING_SURVEY_NOT_FOUND;
139151
}
140152

141-
return $survey->status === 'ready'
142-
? self::ERR_IS_RUNNING_SURVEY_OK
143-
: self::ERR_IS_RUNNING_SURVEY_NOT_RUNNING;
153+
return $survey->status === 'ready' ? self::ERR_IS_RUNNING_SURVEY_OK : self::ERR_IS_RUNNING_SURVEY_NOT_RUNNING;
144154
}
145155

146156
/************************************************/
147157

148158
public static function getByUuid($uuid)
149159
{
150-
return (
151-
$surveys = self::where('uuid', '=', $uuid)
152-
->limit(1)
153-
->get()
154-
) &&
155-
count($surveys) === 1
156-
? $surveys[0]
157-
: null;
160+
$surveys = self::where('uuid', '=', $uuid)
161+
->limit(1)
162+
->get();
163+
164+
return $surveys && count($surveys) === 1 ? $surveys[0] : null;
158165
}
159166

160167
/************************************************/

0 commit comments

Comments
 (0)