diff --git a/.gitignore b/.gitignore index fa0ab12..ab33d5f 100644 --- a/.gitignore +++ b/.gitignore @@ -13,4 +13,9 @@ .phpunit.result.cache /phpunit.xml ###< symfony/phpunit-bridge ### + +###> Eclipse ### .project +.buildpath +.settings +###< Eclipse ### diff --git a/config/orm/Event.orm.xml b/config/orm/Event.orm.xml new file mode 100644 index 0000000..69ad800 --- /dev/null +++ b/config/orm/Event.orm.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/config/orm/EventBattle.orm.xml b/config/orm/EventBattle.orm.xml new file mode 100644 index 0000000..e4ce8aa --- /dev/null +++ b/config/orm/EventBattle.orm.xml @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/config/orm/EventHero.orm.xml b/config/orm/EventHero.orm.xml new file mode 100644 index 0000000..fde938b --- /dev/null +++ b/config/orm/EventHero.orm.xml @@ -0,0 +1,28 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/config/orm/Hero.orm.xml b/config/orm/Hero.orm.xml new file mode 100644 index 0000000..329fb79 --- /dev/null +++ b/config/orm/Hero.orm.xml @@ -0,0 +1,18 @@ + + + + + + + + + + + + + + + + diff --git a/config/orm/Hero.orm.yml b/config/orm/Hero.orm.yml deleted file mode 100644 index db71bee..0000000 --- a/config/orm/Hero.orm.yml +++ /dev/null @@ -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 \ No newline at end of file diff --git a/config/orm/Skillz.orm.xml b/config/orm/Skillz.orm.xml new file mode 100644 index 0000000..2e3c703 --- /dev/null +++ b/config/orm/Skillz.orm.xml @@ -0,0 +1,22 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/config/orm/Skillz.orm.yml b/config/orm/Skillz.orm.yml deleted file mode 100644 index 13f3e90..0000000 --- a/config/orm/Skillz.orm.yml +++ /dev/null @@ -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 diff --git a/config/orm/User.orm.xml b/config/orm/User.orm.xml new file mode 100644 index 0000000..b4d34b8 --- /dev/null +++ b/config/orm/User.orm.xml @@ -0,0 +1,24 @@ + + + + + + + + + + + + + + + + + + + + + + diff --git a/config/orm/User.orm.yml b/config/orm/User.orm.yml deleted file mode 100644 index ee42936..0000000 --- a/config/orm/User.orm.yml +++ /dev/null @@ -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"] \ No newline at end of file diff --git a/config/packages/doctrine.yaml b/config/packages/doctrine.yaml index c744034..23933dc 100644 --- a/config/packages/doctrine.yaml +++ b/config/packages/doctrine.yaml @@ -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 diff --git a/config/packages/security.yaml b/config/packages/security.yaml index abf61a8..f4430e7 100644 --- a/config/packages/security.yaml +++ b/config/packages/security.yaml @@ -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 diff --git a/src/Controller/HeroController.php b/src/Controller/HeroController.php index 65293e6..eee38e2 100644 --- a/src/Controller/HeroController.php +++ b/src/Controller/HeroController.php @@ -1,39 +1,41 @@ 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); } diff --git a/src/Controller/UserController.php b/src/Controller/UserController.php index 8f31b66..653faf3 100644 --- a/src/Controller/UserController.php +++ b/src/Controller/UserController.php @@ -1,41 +1,42 @@ 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); } - } diff --git a/src/DataFixtures/EventBattleFixtures.php b/src/DataFixtures/EventBattleFixtures.php new file mode 100644 index 0000000..84dfabd --- /dev/null +++ b/src/DataFixtures/EventBattleFixtures.php @@ -0,0 +1,47 @@ +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 + ); + } +} + diff --git a/src/DataFixtures/EventFixtures.php b/src/DataFixtures/EventFixtures.php new file mode 100644 index 0000000..d7a43c9 --- /dev/null +++ b/src/DataFixtures/EventFixtures.php @@ -0,0 +1,32 @@ +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); + } +} + diff --git a/src/DataFixtures/EventHeroFixtures.php b/src/DataFixtures/EventHeroFixtures.php new file mode 100644 index 0000000..a4b5b05 --- /dev/null +++ b/src/DataFixtures/EventHeroFixtures.php @@ -0,0 +1,44 @@ +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 + ); + } +} + diff --git a/src/DataFixtures/HeroFixtures.php b/src/DataFixtures/HeroFixtures.php index e2f1671..dad7d21 100644 --- a/src/DataFixtures/HeroFixtures.php +++ b/src/DataFixtures/HeroFixtures.php @@ -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(); } diff --git a/src/Entity/Event.php b/src/Entity/Event.php new file mode 100644 index 0000000..799dbd6 --- /dev/null +++ b/src/Entity/Event.php @@ -0,0 +1,128 @@ +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; + } +} + diff --git a/src/Entity/EventBattle.php b/src/Entity/EventBattle.php new file mode 100644 index 0000000..30746a1 --- /dev/null +++ b/src/Entity/EventBattle.php @@ -0,0 +1,112 @@ +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; + } +} + diff --git a/src/Entity/EventBattleActions.php b/src/Entity/EventBattleActions.php new file mode 100644 index 0000000..c6127e9 --- /dev/null +++ b/src/Entity/EventBattleActions.php @@ -0,0 +1,55 @@ +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; + } +} + diff --git a/src/Entity/EventHero.php b/src/Entity/EventHero.php new file mode 100644 index 0000000..d0c1bf3 --- /dev/null +++ b/src/Entity/EventHero.php @@ -0,0 +1,114 @@ +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; + } +} + diff --git a/src/Entity/Hero.php b/src/Entity/Hero.php index 1ff9ba2..8b63a68 100644 --- a/src/Entity/Hero.php +++ b/src/Entity/Hero.php @@ -16,7 +16,7 @@ class Hero { $this->skills = new Skillz(); } - + public function getId(): ?int { return $this->id; diff --git a/src/Entity/User.php b/src/Entity/User.php index 12ff661..9d43c49 100644 --- a/src/Entity/User.php +++ b/src/Entity/User.php @@ -29,10 +29,7 @@ class User implements UserInterface { return $this->id; } - - /** - * @see UserInterface - */ + public function getUsername(): ?string { return $this->username; diff --git a/src/Security/TokenAuthenticator.php b/src/Security/TokenAuthenticator.php index b8124a8..e124560 100644 --- a/src/Security/TokenAuthenticator.php +++ b/src/Security/TokenAuthenticator.php @@ -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) diff --git a/tests/Controller/RestTestBase.php b/tests/Controller/RestTestBase.php index 64b8d14..7091a3f 100644 --- a/tests/Controller/RestTestBase.php +++ b/tests/Controller/RestTestBase.php @@ -1,5 +1,4 @@ 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'); diff --git a/tests/Controller/UserControllerTest.php b/tests/Controller/UserControllerTest.php index 37004dc..dda09bf 100644 --- a/tests/Controller/UserControllerTest.php +++ b/tests/Controller/UserControllerTest.php @@ -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()); - } - }