/**
  * @test
  */
 public function testWordSelectorWithError()
 {
     $this->request->shouldReceive('get')->andReturn(null);
     $this->wordSelector->shouldReceive('getRandomWord')->andThrow(new \InvalidArgumentException('ex-text'));
     $controller = new WordController($this->wordSelector);
     $response = $controller->random($this->request);
     $this->assertInstanceOf(Response::class, $response);
     $this->assertEquals(json_encode(['error' => 'ex-text']), $response->getContent());
     $this->assertEquals(Response::HTTP_NOT_FOUND, $response->getStatusCode());
     $this->assertEquals('application/json', $response->headers->get('Content-Type'));
 }
Exemplo n.º 2
0
 public function random(Request $request)
 {
     $headers = ['Content-Type' => 'application/json'];
     $length = $request->get('length', 5);
     $lang = $request->get('lang', 'en');
     $complexity = $request->get('complexity');
     $word = null;
     try {
         $word = $this->wordSelector->getRandomWord($length, $lang, $complexity);
     } catch (\InvalidArgumentException $e) {
         return new Response(json_encode(['error' => $e->getMessage()]), Response::HTTP_NOT_FOUND, $headers);
     }
     return new Response(json_encode($word), Response::HTTP_OK, $headers);
 }