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)); } /** * Get stat end date is before start date. * * @return void * @throws ApiErrorException Because end date is before start date. */ 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; } } /** * Test interval 3 months max. * * @return void * @throws ApiErrorException Because interval is bigger thatn 3 months. */ 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; } } /** * Test get stat by question. * * @return void */ 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'); } /** * Get stats by question when it does not belongs to brand. * * @return void */ 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') ); } /** * Get stat by question when end date is before start date. * * @return void */ 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') ); } /** * Get stat by question when interval is more thatn 3 months. * * @return void */ 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') ); } }