callApiWithToken($service, 'get', route('apiV2.stats.brandStats'), [ 'start' => Carbon::now()->subMonths(3)->format('Y-m-d'), 'end' => Carbon::now()->format('Y-m-d') ]); $json = $response->decodeResponseJson(); $this->assertApiResponseStatusIs($json, ApiStatus::OK()); $json->assertStructure([ 'data' => [ 'stats' => [ '*' => [ 'date', 'active', 'inactive', 'old_accounts', 'new_accounts', 'total_accounts', 'total_opt_in' ] ] ] ]); } /** * Test get stats by brand should fail with date bigger than 3 month. * route: apiV2.stats.brandStats * @return void */ public function testStatsByBrandShouldFailWithInvalidDate() { $service = Service::firstOrFail(); $response = $this->callApiWithToken($service, 'get', route('apiV2.stats.brandStats'), [ 'start' => Carbon::now()->subMonths(3)->subDay()->format('Y-m-d'), 'end' => Carbon::now()->format('Y-m-d') ]); $json = $response->decodeResponseJson(); $this->assertApiResponseStatusIs($json, ApiStatus::KO()); $this->assertEquals($json->json('data.code'), ApiError::validationFailed()->value); } /** * Test get stats by brand should fail with invalid date format. * route: apiV2.stats.brandStats * @return void */ public function testStatsByBrandShouldFailWithInvalidDateFormat() { $service = Service::firstOrFail(); $response = $this->callApiWithToken($service, 'get', route('apiV2.stats.brandStats'), [ 'start' => Carbon::now()->subMonths(3)->subDay()->format('Y-m-d h:iDD'), 'end' => Carbon::now()->format('Y-m-d') ]); $json = $response->decodeResponseJson(); $this->assertApiResponseStatusIs($json, ApiStatus::KO()); $this->assertEquals($json->json('data.code'), ApiError::validationFailed()->value); } /** * Test stats by question. * route: apiV2.stats.questionStats * @return void */ public function testStatsByQuestion() { $service = Service::firstOrFail(); $question = Question::whereBrandId($service->brand->id)->firstOrFail(); $answersIds = $question->answers()->pluck('id'); $start = Carbon::now()->subDays(3); $end = Carbon::now(); // * Make sure a customer answer is set. /** @var \App\Models\CustomerAnswer $custAnswer */ $custAnswer = CustomerAnswer::whereHas('answer', function ($q) use ($answersIds) { $q->whereId($answersIds->first()); })->firstOrNew(); $custAnswer->customer()->associate($custAnswer->customer ?? Customer::query()->firstOrFail()); $custAnswer->answer()->associate($answersIds->first()); $custAnswer->saveOrFail(); $response = $this->callApiWithToken($service, 'get', route('apiV2.stats.questionStats', $question), [ 'start' => $start->format('Y-m-d'), 'end' => $end->format('Y-m-d') ]); $datesIndexes = []; do { $datesIndexes[$start->format('Y-m-d')] = $answersIds // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundInExtendedClassBeforeLastUsed ->keys()->all(); $start = $start->addDay(); } while ($start->lessThanOrEqualTo($end)); $json = $response->decodeResponseJson(); $this->assertApiResponseStatusIs($json, ApiStatus::OK()); $json->assertStructure([ 'data' => [ 'answers' => [ '*' => [ 'id', 'answer' ] ], 'stats' => $datesIndexes ] ]); } /** * Test stats by question when it does not belong to brand. * route: apiV2.stats.questionStats * @return void */ public function testStatsByQuestionShouldFailWithInvalidQuestion() { $service = Service::firstOrFail(); $question = Question::where('brand_id', '!=', $service->brand->id)->firstOrFail(); $response = $this->callApiWithToken($service, 'get', route('apiV2.stats.questionStats', $question), [ 'start' => Carbon::now()->subMonths(3)->subDay()->format('Y-m-d'), 'end' => Carbon::now()->format('Y-m-d') ]); $json = $response->decodeResponseJson(); $this->assertApiResponseStatusIs($json, ApiStatus::KO()); $this->assertEquals($json->json('data.code'), ApiError::validationFailed()->value); } /** * Test stats by question should fail with date bigger than 3 month. * route: apiV2.stats.questionStats * @return void */ public function testStatsByQuestionShouldFailWithInvalidDate() { $service = Service::firstOrFail(); $question = Question::whereBrandId($service->brand->id)->firstOrFail(); $response = $this->callApiWithToken($service, 'get', route('apiV2.stats.questionStats', $question), [ 'start' => Carbon::now()->subMonths(3)->subDay()->format('Y-m-d'), 'end' => Carbon::now()->format('Y-m-d') ]); $json = $response->decodeResponseJson(); $this->assertApiResponseStatusIs($json, ApiStatus::KO()); $this->assertEquals($json->json('data.code'), ApiError::validationFailed()->value); } /** * Test stats by question should fail with invalid date format. * route: apiV2.stats.questionStats * @return void */ public function testStatsByQuestionShouldFailWithInvalidDateFormat() { $service = Service::firstOrFail(); $question = Question::whereBrandId($service->brand->id)->firstOrFail(); $response = $this->callApiWithToken($service, 'get', route('apiV2.stats.questionStats', $question), [ 'start' => Carbon::now()->subMonths(3)->format('Y-m Ddh'), 'end' => Carbon::now()->format('Y-m-d') ]); $json = $response->decodeResponseJson(); $this->assertApiResponseStatusIs($json, ApiStatus::KO()); $this->assertEquals($json->json('data.code'), ApiError::validationFailed()->value); } }