Replaced yml with xml mapping files

feature/symfony_upgrade
Hecht 4 years ago
parent 994e6e17cb
commit 1098e6c362

5
.gitignore vendored

@ -13,4 +13,9 @@
.phpunit.result.cache
/phpunit.xml
###< symfony/phpunit-bridge ###
###> Eclipse ###
.project
.buildpath
.settings
###< Eclipse ###

@ -0,0 +1,29 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping ../../vendor/doctrine/orm/doctrine-mapping.xsd ">
<entity name="App\Entity\Event">
<id name="id" type="integer">
<generator strategy="AUTO" />
</id>
<field name="type" type="integer" nullable="false" />
<field name="version" type="integer" nullable="false" />
<one-to-many field="heroes" mapped-by="event" target-entity="EventHero">
<cascade>
<cascade-persist />
<cascade-merge />
</cascade>
</one-to-many>
<one-to-many field="battles" mapped-by="event" target-entity="EventBattle">
<cascade>
<cascade-persist />
<cascade-merge />
</cascade>
</one-to-many>
</entity>
</doctrine-mapping>

@ -0,0 +1,30 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping
xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping ../../vendor/doctrine/orm/doctrine-mapping.xsd ">
<entity name="App\Entity\EventBattle">
<id name="event" association-key="true" />
<id name="eid" type="integer" />
<field name="value" type="json" />
<many-to-one field="event" target-entity="Event" inversed-by="battles" />
<many-to-many field="heroes" target-entity="EventHero">
<join-table name="event_battle_hero">
<join-columns>
<join-column name="event_id" referenced-column-name="event_id"></join-column>
<join-column name="event_battle_id" referenced-column-name="eid"></join-column>
</join-columns>
<inverse-join-columns>
<join-column name="event_id" referenced-column-name="event_id"></join-column>
<join-column name="event_hero_id" referenced-column-name="eid"></join-column>
</inverse-join-columns>
</join-table>
</many-to-many>
</entity>
</doctrine-mapping>

@ -0,0 +1,28 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping ../../vendor/doctrine/orm/doctrine-mapping.xsd ">
<entity name="App\Entity\EventHero">
<id name="event" association-key="true" />
<id name="eid" type="integer" />
<embedded name="skills" class="Skillz" />
<field name="name" type="string" length="32" unique="true" />
<field name="team" type="integer" />
<many-to-one field="user" target-entity="User">
<join-column on-delete="SET NULL" />
</many-to-one>
<many-to-one field="hero" target-entity="Hero">
<join-column on-delete="SET NULL" />
</many-to-one>
<many-to-one field="event" target-entity="Event" inversed-by="heroes">
<join-column on-delete="CASCADE" />
</many-to-one>
</entity>
</doctrine-mapping>

@ -0,0 +1,18 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping ../../vendor/doctrine/orm/doctrine-mapping.xsd ">
<entity name="App\Entity\Hero">
<id name="id" type="integer">
<generator strategy="AUTO" />
</id>
<embedded name="skills" class="Skillz" />
<field name="name" type="string" length="32" unique="true"/>
<many-to-one field="user" target-entity="User" />
</entity>
</doctrine-mapping>

@ -1,24 +0,0 @@
App\Entity\Hero:
type: entity
repositoryClass: App\Repository\HeroRepository
table: hero
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
name:
type: string
length: 32
unique: true
embedded:
skills:
class: Skillz
manyToOne:
user:
targetEntity: User
inversedBy: heroes
joinColumn:
onDelete: CASCADE
nullable: false

@ -0,0 +1,22 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping ../../vendor/doctrine/orm/doctrine-mapping.xsd ">
<embeddable name="App\Entity\Skillz">
<!-- basically the HP -->
<field name="constitution" type="integer" />
<!-- former MP! Some moves will require KI to function -->
<field name="ki" type="integer" />
<!-- damage dealer / block skill -->
<field name="strength" type="integer" />
<!-- evade / hit chance skill -->
<field name="agility" type="integer" />
<!-- if low => one is vulnerable for critical hits or slow attack speed / low damage -->
<field name="stamina" type="integer" />
</embeddable>
</doctrine-mapping>

@ -1,18 +0,0 @@
App\Entity\Skillz:
type: embeddable
fields:
constitution: # basically the HP
type: integer
nullable: false
ki: # former MP! Some moves will require Ki to be available
type: integer
nullable: false
strength: # damage dealer / block skill?
type: integer
nullable: false
agility: # evade / hit chance skill??
type: integer
nullable: false
stamina: # if low => one is vulnerable for critical hits or slow attack speed / low damage
type: integer
nullable: false

@ -0,0 +1,24 @@
<?xml version="1.0" encoding="UTF-8"?>
<doctrine-mapping xmlns="http://doctrine-project.org/schemas/orm/doctrine-mapping"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://doctrine-project.org/schemas/orm/doctrine-mapping ../../vendor/doctrine/orm/doctrine-mapping.xsd ">
<entity name="App\Entity\User">
<id name="id" type="integer">
<generator strategy="AUTO" />
</id>
<field name="username" type="string" length="32" unique="true" />
<field name="password" />
<field name="apiToken" unique="true" nullable="true" />
<field name="roles" type="json" />
<one-to-many field="heroes" mapped-by="user" target-entity="Hero">
<cascade>
<cascade-persist />
<cascade-merge />
</cascade>
</one-to-many>
</entity>
</doctrine-mapping>

@ -1,26 +0,0 @@
App\Entity\User:
type: entity
repositoryClass: App\Repository\UserRepository
table: user
id:
id:
type: integer
generator: { strategy: AUTO }
fields:
username:
type: string
length: 32
unique: true
password: # the hashed password
type: string
apiToken:
type: string
unique: true
nullable: true
roles:
type: json
oneToMany:
heroes:
targetEntity: Hero
mappedBy: user
cascade: ["persist", "merge"]

@ -12,7 +12,7 @@ doctrine:
mappings:
App:
is_bundle: false
type: yml
type: xml
dir: '%kernel.project_dir%/config/orm'
prefix: 'App\Entity'
alias: App

@ -16,7 +16,11 @@ security:
authenticators:
- App\Security\TokenAuthenticator
stateless: true
anonymous: true
lazy: true
logout: ~
main:
stateless: true
anonymous: true
lazy: true
provider: users_in_memory

@ -1,39 +1,41 @@
<?php
namespace App\Controller;
use App\Repository\HeroRepository;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use Symfony\Component\HttpFoundation\Response;
use App\Entity\Hero;
/**
*
* @Rest\Route("api/hero")
*/
class HeroController extends AbstractFOSRestController
{
protected HeroRepository $heroRepository;
public function __construct(HeroRepository $heroRepository)
{
$this->heroRepository = $heroRepository;
}
/**
*
* @Rest\Route("es")
* @Rest\View()
*/
public function cgetAction() : array
public function cgetAction(): array
{
return $this->heroRepository->findAll();
}
/**
*
* @Rest\Route("/{id}"))
* @Rest\View()
*/
public function getAction(int $id) : ?Hero
public function getAction(int $id): ?Hero
{
return $this->heroRepository->find($id);
}

@ -1,41 +1,42 @@
<?php
namespace App\Controller;
use App\Repository\UserRepository;
use FOS\RestBundle\Controller\Annotations as Rest;
use FOS\RestBundle\Controller\AbstractFOSRestController;
use Symfony\Component\HttpFoundation\Response;
use App\Entity\User;
/**
*
* @Rest\Route("api/user")
*/
class UserController extends AbstractFOSRestController
{
protected UserRepository $userRepository;
public function __construct(UserRepository $userRepository)
{
$this->userRepository = $userRepository;
}
/**
*
* @Rest\Route("s")
* @Rest\View()
*/
public function cgetAction() : array
public function cgetAction(): array
{
return $this->userRepository->findAll();
}
/**
*
* @Rest\Route("/{id}"))
* @Rest\View()
*/
public function getAction(int $id) : ?User
public function getAction(int $id): ?User
{
return $this->userRepository->find($id);
}
}

@ -0,0 +1,47 @@
<?php
namespace App\DataFixtures;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use Doctrine\Persistence\ObjectManager;
use App\Entity\EventBattle;
use App\Entity\Event;
use Psr\Log\LoggerInterface;
use App\Entity\Hero;
class EventBattleFixtures extends Fixture implements DependentFixtureInterface
{
private LoggerInterface $logger;
public function __construct(LoggerInterface $logger)
{
$this->logger = $logger;
}
public function load(ObjectManager $manager)
{
/**
*
* @var Event $event
*/
$event = $this->getReference(EventFixtures::REF_1);
$eventBattle = new EventBattle($event, 1);
$eventBattle->setEvent($event);
$eventBattle->addHero($event->getHeroes()[0]);
$eventBattle->addHero($event->getHeroes()[1]);
$manager->persist($eventBattle);
$manager->flush();
}
public function getDependencies()
{
return array(
EventHeroFixtures::class
);
}
}

@ -0,0 +1,32 @@
<?php
namespace App\DataFixtures;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use App\Entity\Event;
class EventFixtures extends Fixture
{
public const REF_1 = 'event-1-reference';
public const REF_2 = 'event-2-reference';
public function load(ObjectManager $manager)
{
$event1 = new Event();
$event1->setType(Event::FREE_BATTLE);
$manager->persist($event1);
$event2 = new Event();
$event2->setType(Event::TOURNAMENT);
$manager->persist($event2);
$manager->flush();
$this->addReference(self::REF_1, $event1);
$this->addReference(self::REF_2, $event2);
}
}

@ -0,0 +1,44 @@
<?php
namespace App\DataFixtures;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
use App\Entity\EventHero;
use App\Entity\Hero;
use App\Entity\Event;
class EventHeroFixtures extends Fixture implements DependentFixtureInterface
{
public function load(ObjectManager $manager)
{
$heroUser1 = $this->getReference(UserFixtures::ADMIN_USER_REFERENCE)->getHeroes()[0];
$heroUser2 = $this->getReference(UserFixtures::DUDE_USER_REFERENCE)->getHeroes()[0];
$event1 = $this->getReference(EventFixtures::REF_1);
$eventHero1 = $this->createEventHero($event1, $heroUser1, 1);
$manager->persist($eventHero1);
$eventHero2 = $this->createEventHero($event1, $heroUser2, 2);
$manager->persist($eventHero2);
$manager->flush();
}
private function createEventHero(Event $event, Hero $hero, int $team)
{
return new EventHero($event, $hero, $team);
}
public function getDependencies()
{
return array(
UserFixtures::class,
HeroFixtures::class,
EventFixtures::class
);
}
}

@ -5,7 +5,7 @@ namespace App\DataFixtures;
use App\Entity\Hero;
use Doctrine\Bundle\FixturesBundle\Fixture;
use Doctrine\Persistence\ObjectManager;
use Doctrine\DataFixtures\DependentFixtureInterface;
use Doctrine\Common\DataFixtures\DependentFixtureInterface;
class HeroFixtures extends Fixture implements DependentFixtureInterface
{
@ -16,6 +16,12 @@ class HeroFixtures extends Fixture implements DependentFixtureInterface
$hero->setUser($this->getReference(UserFixtures::ADMIN_USER_REFERENCE));
$manager->persist($hero);
$hero = new Hero();
$hero->setName('goku');
$hero->setUser($this->getReference(UserFixtures::DUDE_USER_REFERENCE));
$manager->persist($hero);
$manager->flush();
}

@ -0,0 +1,128 @@
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
class Event
{
const QUEST = 1;
const TOURNAMENT = 2;
const FREE_BATTLE = 3;
const LEAGUE_MATCHDAY = 4;
// Reserved value (was "wanted" battles)
const CLAN_BATTLE = 6;
private ?int $id;
private int $type = self::QUEST;
private int $version = 1;
private Collection $heroes;
private Collection $battles;
public function __construct()
{
$this->heroes = new ArrayCollection();
$this->battles = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getType(): ?int
{
return $this->type;
}
public function setType(int $type): self
{
$this->type = $type;
return $this;
}
/**
* @return Collection|EventHero[]
*/
public function getHeroes(): Collection
{
return $this->heroes;
}
public function addHero(EventHero $hero): self
{
if (!$this->heroes->contains($hero)) {
$this->heroes[] = $hero;
$hero->setEvent($this);
}
return $this;
}
public function removeHero(EventHero $hero): self
{
if ($this->heroes->contains($hero)) {
$this->heroes->removeElement($hero);
// set the owning side to null (unless already changed)
if ($hero->getEvent() === $this) {
$hero->setEvent(null);
}
}
return $this;
}
/**
* @return Collection|EventBattle[]
*/
public function getBattles(): Collection
{
return $this->battles;
}
public function addBattle(EventBattle $battle): self
{
if (!$this->battles->contains($battle)) {
$this->battles[] = $battle;
$battle->setEvent($this);
}
return $this;
}
public function removeBattle(EventBattle $battle): self
{
if ($this->battles->contains($battle)) {
$this->battles->removeElement($battle);
// set the owning side to null (unless already changed)
if ($battle->getEvent() === $this) {
$battle->setEvent(null);
}
}
return $this;
}
public function getVersion(): ?int
{
return $this->version;
}
public function setVersion(int $version): self
{
$this->version = $version;
return $this;
}
}

@ -0,0 +1,112 @@
<?php
namespace App\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
class EventBattle
{
private ?Event $event = null;
private int $eid = 0;
private Collection $heroes;
private Collection $actions;
private array $value = [];
public function __construct(Event $event, int $eid)
{
$this->event = $event;
$this->eid = $eid;
$this->heroes = new ArrayCollection();
$this->actions = new ArrayCollection();
}
public function getId(): ?int
{
return $this->id;
}
public function getEvent(): ?Event
{
return $this->event;
}
public function setEvent(?Event $event): self
{
$this->event = $event;
return $this;
}
public function getHeroLhs(): ?EventHero
{
return $this->heroLhs;
}
public function setHeroLhs(?EventHero $heroLhs): self
{
$this->heroLhs = $heroLhs;
return $this;
}
public function getHeroRhs(): ?EventHero
{
return $this->heroRhs;
}
public function setHeroRhs(?EventHero $heroRhs): self
{
$this->heroRhs = $heroRhs;
return $this;
}
/**
* @return Collection|EventHero[]
*/
public function getHeroes(): Collection
{
return $this->heroes;
}
public function addHero(EventHero $hero): self
{
if (!$this->heroes->contains($hero)) {
$this->heroes[] = $hero;
}
return $this;
}
public function removeHero(EventHero $hero): self
{
if ($this->heroes->contains($hero)) {
$this->heroes->removeElement($hero);
}
return $this;
}
public function getEid(): ?int
{
return $this->eid;
}
public function getValue(): ?array
{
return $this->value;
}
public function setValue(array $value): self
{
$this->value = $value;
return $this;
}
}

@ -0,0 +1,55 @@
<?php
namespace App\Entity;
class EventBattleActions
{
private ?int $id;
private int $timeInSeconds;
private array $action;
private ?EventBattle $battle;
public function getId(): ?int
{
return $this->id;
}
public function getTimeInSeconds(): ?int
{
return $this->timeInSeconds;
}
public function setTimeInSeconds(int $timeInSeconds): self
{
$this->timeInSeconds = $timeInSeconds;
return $this;
}
public function getAction(): ?array
{
return $this->action;
}
public function setAction(array $action): self
{
$this->action = $action;
return $this;
}
public function getBattle(): ?EventBattle
{
return $this->battle;
}
public function setBattle(?EventBattle $battle): self
{
$this->battle = $battle;
return $this;
}
}

@ -0,0 +1,114 @@
<?php
namespace App\Entity;
class EventHero
{
private int $eid = 0;
private string $name;
private int $team = 0;
private Skillz $skills;
private Hero $hero;
private User $user;
private Event $event;
public function __construct(Event $event, Hero $hero, int $team)
{
$this->event = $event;
$this->eid = $hero->getId();
$this->name = $hero->getName();
$this->skills = $hero->getSkills();
$this->team = $team;
$this->hero = $hero;
$this->user = $hero->getUser();
}
public function getSkills(): Skillz
{
return $this->skills;
}
public function getId(): ?int
{
return $this->id;
}
public function getName(): ?string
{
return $this->name;
}
public function getUser(): ?User
{
return $this->user;
}
public function getEvent(): ?Event
{
return $this->event;
}
public function getTeam(): ?int
{
return $this->team;
}
public function getHero(): ?Hero
{
return $this->hero;
}
public function getEid(): ?int
{
return $this->eid;
}
public function setSkills(Skillz $skills): self
{
$this->skills = $skills;
return $this;
}
public function setName(string $name): self
{
$this->name = $name;
return $this;
}
public function setTeam(int $team): self
{
$this->team = $team;
return $this;
}
public function setUser(?User $user): self
{
$this->user = $user;
return $this;
}
public function setHero(?Hero $hero): self
{
$this->hero = $hero;
return $this;
}
public function setEvent(?Event $event): self
{
$this->event = $event;
return $this;
}
}

@ -16,7 +16,7 @@ class Hero
{
$this->skills = new Skillz();
}
public function getId(): ?int
{
return $this->id;

@ -29,10 +29,7 @@ class User implements UserInterface
{
return $this->id;
}
/**
* @see UserInterface
*/
public function getUsername(): ?string
{
return $this->username;

@ -37,21 +37,17 @@ class TokenAuthenticator extends AbstractGuardAuthenticator
*/
public function getCredentials(Request $request)
{
return array(
'token' => $request->headers->get('X-AUTH-TOKEN'),
);
return $request->headers->get('X-AUTH-TOKEN');
}
public function getUser($credentials, UserProviderInterface $userProvider)
{
$apiToken = $credentials['token'];
if (null === $apiToken) {
return;
if (null === $credentials) {
return null;
}
// if a User object, checkCredentials() is called
return $this->userRepository->findOneBy(['apiToken' => $apiToken]);
return $this->userRepository->findOneBy(['apiToken' => $credentials]);
}
public function checkCredentials($credentials, UserInterface $user)

@ -1,5 +1,4 @@
<?php
namespace App\Tests\Controller;
use Doctrine\ORM\EntityManagerInterface;
@ -11,6 +10,7 @@ use Symfony\Bundle\FrameworkBundle\KernelBrowser;
class RestTestBase extends WebTestCase
{
protected KernelBrowser $client;
protected function setUp()
@ -27,10 +27,9 @@ class RestTestBase extends WebTestCase
// index values all along the tests
self::runCommand('doctrine:schema:drop --force -n');
self::runCommand('doctrine:schema:create -n');
self::runCommand('doctrine:migration:migrate -n');
self::runCommand('doctrine:fixtures:load --purge-with-truncate -n');
}
protected function runCommand($command)
{
$application = new Application($this->client->getKernel());
@ -44,7 +43,7 @@ class RestTestBase extends WebTestCase
$builder->setAcceptType($acceptType);
return $builder;
}
protected function retrieveEntityManager(): EntityManagerInterface
{
return $this->client->getContainer()->get('doctrine.orm.entity_manager');

@ -3,8 +3,6 @@
namespace App\Tests\Controller;
use App\DataFixtures\UserFixtures;
use App\Entity\User;
use App\Entity\Hero;
class UserControllerTest extends RestTestBase
{
@ -48,14 +46,8 @@ class UserControllerTest extends RestTestBase
$response->headers->contains('Content-Type', 'application/json'),
'the "Content-Type" header is "' . $response->headers->get('Content-Type') . '"' // optional message shown on failure
);
$this->assertEquals(200, $response->getStatusCode(), 'Status code was ' . $response->getStatusCode() . ' but expected 200: ' . $response->getContent());
}
public function testUserDefaults()
{
$hero = new Hero();
$this->assertEquals(0, $hero->getStrength());
}
}

Loading…
Cancel
Save