Skip to content

Instantly share code, notes, and snippets.

@bwaidelich
Forked from tomasnorre/Player.php
Created June 16, 2015 21:46
Show Gist options
  • Save bwaidelich/6d840459132cea9425aa to your computer and use it in GitHub Desktop.
Save bwaidelich/6d840459132cea9425aa to your computer and use it in GitHub Desktop.
<?php
namespace TomasNorre\GolfnetInvitational\Domain\Model;
use TYPO3\Flow\Annotations as Flow;
use Doctrine\ORM\Mapping as ORM;
/**
* @Flow\Entity
*/
class Player {
/**
* @var string
*/
protected $name;
/**
* @var boolean
*/
protected $captain = FALSE;
/**
* @var Team
* @ORM\ManyToOne(inversedBy="players")
*/
protected $team;
/**
* @param string $name
* @param Team $team
*/
public function __construct($name, Team $team = NULL) {
$this->name = $name;
$this->team = $team;
}
/**
* @param string $name
* @return void
*/
public function rename($name) {
$this->name = $name;
}
/**
* @return string
*/
public function getName() {
return $this->name;
}
/**
* @param Team $team
* @return void
*/
public function changeTeam(Team $team) {
$this->team = $team;
}
/**
* @return Team
*/
public function getTeam() {
return $this->team;
}
/**
* @return boolean
*/
public function isCaptain() {
return $this->captain;
}
/**
* @return void
*/
public function makeCaptain() {
$this->captain = TRUE;
}
}
<?php
namespace TomasNorre\GolfnetInvitational\Domain\Repository;
use TomasNorre\GolfnetInvitational\Domain\Model\Team;
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Persistence\QueryResultInterface;
use TYPO3\Flow\Persistence\Repository;
/**
* @Flow\Scope("singleton")
*/
class PlayerRepository extends Repository {
/**
* @return QueryResultInterface
*/
public function findAllNotCaptain() {
$query = $this->createQuery();
$query->matching(
$query->equals('captain', FALSE)
);
return $query->execute();
}
/**
* @param Team $team
* @return QueryResultInterface
*/
public function findAllInTeam(Team $team ) {
$query = $this->createQuery();
$query->matching(
$query->equals('team', $team)
);
return $query->execute();
}
}
<?php
namespace TomasNorre\GolfnetInvitational\Tests\Functional\Domain\Repository;
use TomasNorre\GolfnetInvitational\Domain\Model\Player;
use TomasNorre\GolfnetInvitational\Domain\Model\Team;
use TomasNorre\GolfnetInvitational\Domain\Repository\PlayerRepository;
use TomasNorre\GolfnetInvitational\Domain\Repository\TeamRepository;
use TYPO3\Flow\Annotations as Flow;
use TYPO3\Flow\Persistence\Doctrine\PersistenceManager;
use TYPO3\Flow\Tests\FunctionalTestCase;
/**
* Functional tests for the PlayerRepository
*/
class PlayerRepositoryTest extends FunctionalTestCase {
/**
* @var PlayerRepository
*/
protected $playerRepository;
/**
* @var TeamRepository
*/
protected $teamRepository;
/**
* @var Team
*/
protected $teamA;
/**
* @var Team
*/
protected $teamB;
/**
* @var Player
*/
protected $playerA;
/**
* @var Player
*/
protected $playerB;
/**
* @var Player
*/
protected $playerC;
/**
* @var Player
*/
protected $playerD;
/**
* @var Player
*/
protected $playerE;
/**
* @var boolean
*/
static protected $testablePersistenceEnabled = TRUE;
/**
* Set up
*/
public function setUp() {
parent::setUp();
if (!$this->persistenceManager instanceof PersistenceManager) {
$this->markTestSkipped('Doctrine persistence is not enabled');
}
$this->playerRepository = new PlayerRepository();
$this->teamRepository = new TeamRepository();
$this->createPlayers();
}
/**
* @test
*/
public function findAllNotCaptain() {
$expectedPlayersNotCaptainArray = [$this->playerD, $this->playerE];
$actualPlayersNotCaptainArray = $this->playerRepository->findAllNotCaptain();
$this->assertSame($expectedPlayersNotCaptainArray, $actualPlayersNotCaptainArray->toArray());
}
/**
* @test
*/
public function findAllInTeam() {
$expectedPlayersInTeamA = [$this->playerA, $this->playerB];
$actualPlayersInTeamA = $this->playerRepository->findAllInTeam($this->teamA);
$this->assertSame($expectedPlayersInTeamA, $actualPlayersInTeamA->toArray());
}
/**
* Create Teams Data
*
* @return void
*/
protected function createPlayers() {
// Teams
$this->teamA = new Team('TeamA');
$this->teamRepository->add($this->teamA);
$this->teamB = new Team('TeamB');
$this->teamRepository->add($this->teamB);
// Players
$this->playerA = new Player('Luke Donald', $this->teamA);
$this->playerA->makeCaptain();
$this->playerRepository->add($this->playerA);
$this->playerB = new Player('Tiger Woods', $this->teamA);
$this->playerB->makeCaptain();
$this->playerRepository->add($this->playerB);
$this->playerC = new Player('Adam Scott');
$this->playerC->makeCaptain();
$this->playerRepository->add($this->playerC);
$this->playerD = new Player('Bubba Watson', $this->teamB);
$this->playerRepository->add($this->playerD);
$this->playerE = new Player('Rory McIlroy');
$this->playerRepository->add($this->playerE);
$this->persistenceManager->persistAll();
}
}
<?php
namespace TomasNorre\GolfnetInvitational\Domain\Model;
use Doctrine\Common\Collections\Collection;
use TYPO3\Flow\Annotations as Flow;
use Doctrine\ORM\Mapping as ORM;
/**
* @Flow\Entity
*/
class Team {
/**
* @var string
*/
protected $name;
/**
* @var Collection<Player>
* @ORM\OneToMany(mappedBy="team")
*/
protected $players;
/**
* @param string $name
*/
public function __construct($name) {
$this->name = $name;
}
/**
* @param string $name
* @return void
*/
public function rename($name) {
$this->name = $name;
}
/**
* @return string
*/
public function getName() {
return $this->name;
}
/**
* @return Collection<Player>
*/
public function getPlayers() {
return $this->players;
}
/**
* @param Player $player
* @return void
*/
public function addPlayer(Player $player) {
$this->players->add($player);
}
/**
* @param Player $player
* @return void
*/
public function removePlayer(Player $player) {
$this->players->removeElement($player);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment