callApiWithToken($service, 'get', route('apiV2.stats.brandStats'), [ 'start' => Carbon::now()->subMonths(3)->isoFormat('YYYY-MM-DD'), 'end' => Carbon::now()->isoFormat('YYYY-MM-DD') ]); $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()->isoFormat('YYYY-MM-DD'), 'end' => Carbon::now()->isoFormat('YYYY-MM-DD') ]); $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()->isoFormat('YYYY-MM-DD HH:mmDD'), 'end' => Carbon::now()->isoFormat('YYYY-MM-DD') ]); $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->isoFormat('YYYY-MM-DD'), 'end' => $end->isoFormat('YYYY-MM-DD') ]); $datesIndexes = []; do { $datesIndexes[$start->isoFormat('YYYY-MM-DD')] = $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: apiv.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()->isoFormat('YYYY-MM-DD'), 'end' => Carbon::now()->isoFormat('YYYY-MM-DD') ]); $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()->isoFormat('YYYY-MM-DD'), 'end' => Carbon::now()->isoFormat('YYYY-MM-DD') ]); $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)->isoFormat('YYYY-MM Ddh'), 'end' => Carbon::now()->isoFormat('YYYY-MM-DD') ]); $json = $response->decodeResponseJson(); $this->assertApiResponseStatusIs($json, ApiStatus::KO); $this->assertEquals($json->json('data.code'), ApiError::validationFailed->value()); } }