subMonths(3); $end = Carbon::now(); $stats = Stats::getBrandStats( $start->format('Y-m-d'), $end->format('Y-m-d') ); $this->assertIsArray($stats, 'Results should be an array'); $i = 0; $keys = [ 'date', 'active', 'inactive', 'old_accounts', 'new_accounts', 'total_accounts', 'total_opt_in' ]; do { $this->assertArrayHasKey($i, $stats); foreach ($keys as $key) { $this->assertArrayHasKey($key, $stats[$i]); } $this->assertEquals($start->format('Y-m-d'), $stats[$i++]['date']); $start = $start->addDay(); } while ($start->lessThanOrEqualTo($end)); } public function testStatsByBrandShouldFailIfStartDateIsAfterEndDate() { $this->expectException(ApiErrorException::class); $start = Carbon::now(); $end = Carbon::now()->subDays(6); try { Stats::getBrandStats( $start->format('Y-m-d'), $end->format('Y-m-d') ); } catch (ApiErrorException $e) { $this->assertEquals(ApiError::validationFailed(), $e->getApiError()); throw $e; } } public function testStatsByBrandShouldFailIfIntervalIsMoreThanThreeMonths() { $this->expectException(ApiErrorException::class); $start = Carbon::now()->subMonths(3)->subDay(); $end = Carbon::now(); try { Stats::getBrandStats( $start->format('Y-m-d'), $end->format('Y-m-d') ); } catch (ApiErrorException $e) { $this->assertEquals(ApiError::validationFailed(), $e->getApiError()); throw $e; } } public function testStatsByQuestion() { /** @var \App\Models\Question $question */ $question = Question::where('brand_id', self::$brand->id)->first(); $answersIds = $question->answers()->pluck('id'); $start = Carbon::now()->subMonths(3); $end = Carbon::now(); $stats = Stats::getQuestionStats( new ModelsQuestion([ 'id' => $question->id ]), $start->format('Y-m-d'), $end->format('Y-m-d') ); do { $startD = $start->format('Y-m-d'); $this->assertArrayHasKey($startD, $stats['stats']); // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed foreach ($answersIds as $key => $id) { $this->assertArrayHasKey($key, $stats['stats'][$startD]); } $start = $start->addDay(); } while ($start->lessThanOrEqualTo($end)); $this->assertIsArray($stats['stats'], 'Results should be an array'); } public function testStatsByQuestionShouldFailIfQuestionDoesNotBelongsToBrand() { $this->expectException(ApiErrorException::class); /** @var \App\Models\Question $question */ $question = Question::where('brand_id', '!=', self::$brand->id)->first(); $start = Carbon::now()->subMonths(3); $end = Carbon::now(); Stats::getQuestionStats( new ModelsQuestion([ 'id' => $question->id ]), $start->format('Y-m-d'), $end->format('Y-m-d') ); } public function testStatsByQuestionShouldFailIfStartDateIsAfterEndDate() { $this->expectException(ApiErrorException::class); /** @var \App\Models\Question $question */ $question = Question::where('brand_id', '!=', self::$brand->id)->first(); $start = Carbon::now(); $end = Carbon::now()->subDays(6); Stats::getQuestionStats( new ModelsQuestion([ 'id' => $question->id ]), $start->format('Y-m-d'), $end->format('Y-m-d') ); } public function testStatsByQuestionShouldFailIfIntervalIsMoreThanThreeMonths() { $this->expectException(ApiErrorException::class); /** @var \App\Models\Question $question */ $question = Question::where('brand_id', '!=', self::$brand->id)->first(); $start = Carbon::now()->subMonths(3)->subDay(); $end = Carbon::now(); Stats::getQuestionStats( new ModelsQuestion([ 'id' => $question->id ]), $start->format('Y-m-d'), $end->format('Y-m-d') ); } }