コード例 #1
0
 public function testPlayerShipsAndOtherShips()
 {
     $user1 = $this->prophesize('AppBundle\\Entity\\User');
     $user2 = $this->prophesize('AppBundle\\Entity\\User');
     $user1Ships = ['A1'];
     $user2Ships = ['B2'];
     $user1->getId()->willReturn(1);
     $user2->getId()->willReturn(2);
     $this->game->setUser1Ships($user1Ships);
     $this->game->setUser2Ships($user2Ships);
     $this->game->setUser1($user1->reveal());
     $this->game->setUser2($user2->reveal());
     $this->game->setLoggedUser($user1->reveal());
     $this->assertEquals($user1Ships, $this->game->getPlayerShips());
     $this->assertEquals($user2Ships, $this->game->getOtherShips());
     $this->game->setLoggedUser($user2->reveal());
     $this->assertEquals($user2Ships, $this->game->getPlayerShips());
     $this->assertEquals($user1Ships, $this->game->getOtherShips());
 }
コード例 #2
0
 /**
  * @param User $user
  * @param array $ships
  * @return Game
  */
 private function createGame(User $user, array $ships = [])
 {
     $entityManager = $this->getEntityManager();
     $game = new Game();
     $game->setLoggedUser($user)->setUser1($user)->setPlayerShips($ships);
     $entityManager->persist($game);
     $entityManager->flush();
     return $game;
 }
コード例 #3
0
 /**
  * Example request in <strong>Content</strong> (not required):
  *  <pre>{"playerShips":["A1","C2","D2","F2","H2","J2","F5","F6","I6","J6","A7","B7","C7","F7","F8","I9","J9","E10","F10","G10"]}</pre>
  *
  * @ApiDoc(
  *  resource=true,
  *  description="Create new game",
  *  section="Game",
  *  statusCodes={
  *     201="Game created",
  *     400="Incorrect 'playerShips' provided"
  *  }
  * )
  *
  * @Tag("games")
  * @RequestParam(name="playerShips", requirements="[A-J]([1-9]|10)", allowBlank=false, nullable=true, map=true)
  *
  * @param ParamFetcher $paramFetcher
  * @return Response
  */
 public function postGameAction(ParamFetcher $paramFetcher)
 {
     $user = $this->getUser();
     $playerShips = (array) $paramFetcher->get('playerShips');
     $game = new Game();
     $game->setLoggedUser($user)->setUser1($user)->setPlayerShips($playerShips);
     $this->entityManager->persist($game);
     if ($playerShips) {
         $this->createEvent($game, Event::TYPE_START_GAME);
     }
     $this->entityManager->flush();
     $view = $this->routeRedirectView('api_v1_get_game', ['game' => $game->getId()]);
     return $this->handleView($view);
 }
コード例 #4
0
 /**
  * @return Game
  */
 public function testAddEventShot()
 {
     $client = static::createClient();
     $client->enableProfiler();
     $user1 = $this->getUser(3);
     $user2 = $this->getUser(4);
     $game = new Game();
     $game->setLoggedUser($user1)->setUser1($user1)->setUser2($user2)->setUser1Ships(['A1', 'C2', 'D2', 'F2', 'H2', 'J2', 'F5', 'F6', 'I6', 'J6', 'A7', 'B7', 'C7', 'F7', 'F8', 'I9', 'J9', 'E10', 'F10', 'G10'])->setUser2Ships(['A10', 'C2', 'D2', 'F2', 'H2', 'J2', 'F5', 'F6', 'I6', 'J6', 'A7', 'B7', 'C7', 'F7', 'F8', 'I9', 'J9', 'E10', 'F10', 'G10']);
     // starting game
     $event1 = new Event();
     $event1->setGame($game)->setPlayer(1)->setType(Event::TYPE_START_GAME)->setValue(true);
     $event2 = new Event();
     $event2->setGame($game)->setPlayer(2)->setType(Event::TYPE_START_GAME)->setValue(true);
     $entityManager = $this->getEntityManager();
     $entityManager->persist($game);
     $entityManager->persist($event1);
     $entityManager->persist($event2);
     $entityManager->flush();
     $apiKey = $this->getUserApiKey(3);
     $body = ['type' => Event::TYPE_SHOT, 'value' => $game->getUser2Ships()[0]];
     $client->request('POST', sprintf('/v1/games/%d/events', $game->getId()), [], [], ['CONTENT_TYPE' => 'application/json', 'HTTP_ACCEPT' => 'application/json', 'HTTP_AUTHORIZATION' => 'Bearer ' . $apiKey], json_encode($body));
     $response = $client->getResponse();
     $jsonResponse = json_decode($response->getContent(), true);
     $this->validateAddEvent($response);
     /** @var DoctrineDataCollector $doctrineDataCollector */
     $doctrineDataCollector = $client->getProfile()->getCollector('db');
     // SELECT user, SELECT game, SELECT event (getAttackerShots), SELECT event (has game started), SELECT event (whoseTurn), START TRANSACTION, INSERT event, COMMIT
     $this->assertEquals(8, $doctrineDataCollector->getQueryCount());
     $this->assertEquals(['result' => BattleManager::SHOT_RESULT_SUNK], $jsonResponse, $response);
     return $game;
 }