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(): void { $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(): void { $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(): void { extract($this->getCustomer(true, false)); $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. $answer = Answer::findOrFail($answersIds->first()); DB::table('model_answer')->insert([ 'answer_id' => $answer->getKey(), 'model_id' => $customer->getKey(), 'model_type' => $customer->getMorphClass(), ]); $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->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(): void { $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(): void { $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(): void { $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()); } }