You cannot select more than 25 topics
			Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
		
		
		
		
		
			
		
			
				
	
	
		
			230 lines
		
	
	
		
			7.6 KiB
		
	
	
	
		
			PHP
		
	
			
		
		
	
	
			230 lines
		
	
	
		
			7.6 KiB
		
	
	
	
		
			PHP
		
	
| <?php
 | |
| namespace App\Tests;
 | |
| 
 | |
| use App\Entity\Dojo;
 | |
| use App\Factory\CharacterFactory;
 | |
| use App\Factory\DojoFactory;
 | |
| use App\Factory\TechniqueFactory;
 | |
| use App\Factory\UserFactory;
 | |
| use Zenstruck\Foundry\Proxy;
 | |
| 
 | |
| class CharacterTest extends AbstractTest
 | |
| {
 | |
| 
 | |
|     private function createCharacters(Proxy|Dojo $dojo): array
 | |
|     {
 | |
|         $technique = TechniqueFactory::createOne();
 | |
| 
 | |
|         $characters = CharacterFactory::createMany(4, [
 | |
|             'dojo' => $dojo,
 | |
|             'techniques' => [
 | |
|                 $technique
 | |
|             ]
 | |
|         ]);
 | |
| 
 | |
|         return $characters;
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Requirement: A user should be able see all characters from a dojo, but only the public fields!
 | |
|      */
 | |
|     public function testRetrieveCharactersFromDojoPublic(): void
 | |
|     {
 | |
|         $requestUser = UserFactory::createOne();
 | |
|         $dojo = DojoFactory::createOne();
 | |
|         CharacterFactory::createMany(4, [
 | |
|             'dojo' => $dojo
 | |
|         ]);
 | |
|         CharacterFactory::createMany(10);
 | |
| 
 | |
|         $response = static::createClientWithToken($requestUser->authName)->request('GET',
 | |
|             $this->getIri($dojo) . '/characters');
 | |
| 
 | |
|         $this->assertResponseStatusCodeSame(200);
 | |
| 
 | |
|         $this->assertNotEquals("[[],[],[],[]]", $response->getContent());
 | |
| 
 | |
|         // Because test fixtures are automatically loaded between each test, you can assert on them
 | |
|         $this->assertCount(4, $response->toArray());
 | |
| 
 | |
|         $chars = $response->toArray();
 | |
|         $this->assertEquals(4, count($chars[0]));
 | |
|         $this->assertArrayHasKey('id', $chars[0]);
 | |
|         $this->assertArrayHasKey('name', $chars[0]);
 | |
|         $this->assertEquals('/api/dojos/' . $dojo->getId()
 | |
|             ->toBase32(), $chars[0]['dojo']);
 | |
|         $this->assertArrayHasKey('age', $chars[0]);
 | |
|         $this->assertArrayNotHasKey('freeSkillPoints', $chars[0]);
 | |
|         $this->assertArrayNotHasKey('strength', $chars[0]); // not accessible via this route
 | |
|         $this->assertArrayNotHasKey('constitution', $chars[0]); // not accessible via this route
 | |
|         $this->assertArrayNotHasKey('agility', $chars[0]); // not accessible via this route
 | |
|         $this->assertArrayNotHasKey('chi', $chars[0]); // not accessible via this route
 | |
|         $this->assertArrayNotHasKey('techniques', $chars[0]); // not accessible via this route
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Requirement: A user should be able see all of the characters from his dojo, not restricted by public fields!
 | |
|      */
 | |
|     public function testRetrieveCharactersFromOwnDojoDetail(): void
 | |
|     {
 | |
|         $dojo = DojoFactory::createOne([
 | |
|             'owner' => UserFactory::createOne()
 | |
|         ]);
 | |
| 
 | |
|         $technique = TechniqueFactory::createOne();
 | |
| 
 | |
|         $foo = CharacterFactory::createMany(4, [
 | |
|             'dojo' => $dojo,
 | |
|             'techniques' => [
 | |
|                 $technique
 | |
|             ]
 | |
|         ]);
 | |
| 
 | |
|         $this->assertEquals(1, count($foo[0]->getTechniques()));
 | |
| 
 | |
|         CharacterFactory::createMany(10);
 | |
| 
 | |
|         $response = static::createClientWithToken($dojo->getOwner()->authName)->request('GET', '/api/dojo/characters');
 | |
| 
 | |
|         $this->assertResponseStatusCodeSame(200);
 | |
| 
 | |
|         $this->assertNotEquals("[[],[],[],[]]", $response->getContent());
 | |
| 
 | |
|         $chars = $response->toArray();
 | |
|         $this->assertEquals(10, count($chars[0]));
 | |
|         $this->assertArrayHasKey('id', $chars[0]);
 | |
|         $this->assertArrayHasKey('name', $chars[0]);
 | |
|         $this->assertArrayHasKey('dojo', $chars[0]);
 | |
|         $this->assertEquals('/api/dojos/' . $dojo->getId()
 | |
|             ->toBase32(), $chars[0]['dojo']);
 | |
|         $this->assertArrayHasKey('age', $chars[0]);
 | |
|         $this->assertArrayHasKey('freeSkillPoints', $chars[0]);
 | |
|         $this->assertArrayHasKey('strength', $chars[0]);
 | |
|         $this->assertArrayHasKey('constitution', $chars[0]);
 | |
|         $this->assertArrayHasKey('agility', $chars[0]);
 | |
|         $this->assertArrayHasKey('chi', $chars[0]);
 | |
|         $this->assertArrayHasKey('techniques', $chars[0]);
 | |
|         $this->assertEquals('/api/techniques/' . $technique->getId()
 | |
|             ->toBase32(), $chars[0]['techniques'][0]);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Regression test, to verify that the method will also work when multiple dojos are created.
 | |
|      */
 | |
|     public function testRetrieveCharactersFromOwnDojoOnly(): void
 | |
|     {
 | |
|         $dojo = DojoFactory::createOne([
 | |
|             'owner' => UserFactory::createOne()
 | |
|         ]);
 | |
|         $chars = $this->createCharacters($dojo);
 | |
| 
 | |
|         $otherDojos = DojoFactory::createMany(10);
 | |
|         foreach ($otherDojos as $otherDojo) {
 | |
|             $this->createCharacters($otherDojo);
 | |
|         }
 | |
| 
 | |
|         $response = static::createClientWithToken($dojo->getOwner()->authName)->request('GET', '/api/dojo/characters');
 | |
|         $this->assertResponseStatusCodeSame(200);
 | |
| 
 | |
|         $readChars = $response->toArray();
 | |
| 
 | |
|         $this->assertEquals(count($chars), count($readChars));
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Requirement: MVP only (in the future the recuitment will be different).
 | |
|      * A user should be able to create a single character.
 | |
|      */
 | |
|     public function testCreateCharacter(): void
 | |
|     {
 | |
|         $dojo = DojoFactory::createOne([
 | |
|             'owner' => UserFactory::createOne()
 | |
|         ]);
 | |
| 
 | |
|         $tech = TechniqueFactory::createOne([
 | |
|             'prerequisite' => NULL
 | |
|         ]);
 | |
| 
 | |
|         $response = static::createClientWithToken($dojo->getOwner()->authName)->request('POST', '/api/characters',
 | |
|             [
 | |
|                 'json' => [
 | |
|                     'name' => 'Dude',
 | |
|                     'strength' => 1,
 | |
|                     'constitution' => 1,
 | |
|                     'agility' => 1,
 | |
|                     'chi' => 1,
 | |
|                     'techniques' => [
 | |
|                         $this->getIri($tech)
 | |
|                     ]
 | |
|                 ]
 | |
|             ]);
 | |
| 
 | |
|         $this->assertResponseStatusCodeSame(201);
 | |
| 
 | |
|         $this->assertArrayHasKey('id', $response->toArray());
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Requirement: MVP only (in the future the recuitment will be different).
 | |
|      * A user should NOT be able spent more skill points than available.
 | |
|      */
 | |
|     public function testCreateCharacterStatsTooHigh(): void
 | |
|     {
 | |
|         $dojo = DojoFactory::createOne([
 | |
|             'owner' => UserFactory::createOne()
 | |
|         ]);
 | |
| 
 | |
|         $tech = TechniqueFactory::createOne([
 | |
|             'prerequisite' => NULL
 | |
|         ]);
 | |
| 
 | |
|         static::createClientWithToken($dojo->getOwner()->authName)->request('POST', '/api/characters',
 | |
|             [
 | |
|                 'json' => [
 | |
|                     'name' => 'Dude',
 | |
|                     'strength' => 99,
 | |
|                     'constitution' => 1,
 | |
|                     'agility' => 1,
 | |
|                     'chi' => 1,
 | |
|                     'techniques' => [
 | |
|                         $this->getIri($tech)
 | |
|                     ]
 | |
|                 ]
 | |
|             ]);
 | |
| 
 | |
|         $this->assertResponseStatusCodeSame(422);
 | |
|     }
 | |
| 
 | |
|     /**
 | |
|      * Requirement: MVP only (in the future the recuitment will be different).
 | |
|      * A user should NOT be able spent more skill points than available.
 | |
|      */
 | |
|     public function testCreateCharacterTooExpensiveTechnique(): void
 | |
|     {
 | |
|         $dojo = DojoFactory::createOne([
 | |
|             'owner' => UserFactory::createOne()
 | |
|         ]);
 | |
| 
 | |
|         $tech = TechniqueFactory::createOne([
 | |
|             'costs' => 99
 | |
|         ]);
 | |
| 
 | |
|         static::createClientWithToken($dojo->getOwner()->authName)->request('POST', '/api/characters',
 | |
|             [
 | |
|                 'json' => [
 | |
|                     'name' => 'Dude',
 | |
|                     'strength' => 1,
 | |
|                     'constitution' => 1,
 | |
|                     'agility' => 1,
 | |
|                     'chi' => 1,
 | |
|                     'techniques' => [
 | |
|                         $this->getIri($tech)
 | |
|                     ]
 | |
|                 ]
 | |
|             ]);
 | |
| 
 | |
|         $this->assertResponseStatusCodeSame(422);
 | |
|     }
 | |
| }
 | |
| 
 |