ProjectTest.php (8006B)
1 <?php 2 3 namespace PartKeepr\ProjectBundle\Tests; 4 5 use Doctrine\Common\DataFixtures\ProxyReferenceRepository; 6 use PartKeepr\CoreBundle\Tests\WebTestCase; 7 use PartKeepr\PartBundle\Entity\Part; 8 use PartKeepr\ProjectBundle\Entity\Project; 9 use PartKeepr\ProjectBundle\Entity\ProjectAttachment; 10 use PartKeepr\ProjectBundle\Entity\ProjectPart; 11 use Symfony\Component\HttpFoundation\File\UploadedFile; 12 13 class ProjectTest extends WebTestCase 14 { 15 /** 16 * @var ProxyReferenceRepository 17 */ 18 protected $fixtures; 19 20 public function setUp() 21 { 22 $this->fixtures = $this->loadFixtures( 23 [ 24 'PartKeepr\StorageLocationBundle\DataFixtures\CategoryDataLoader', 25 'PartKeepr\StorageLocationBundle\DataFixtures\StorageLocationLoader', 26 'PartKeepr\PartBundle\DataFixtures\CategoryDataLoader', 27 'PartKeepr\PartBundle\DataFixtures\PartDataLoader', 28 'PartKeepr\ProjectBundle\DataFixtures\ProjectFixtureLoader', 29 ] 30 )->getReferenceRepository(); 31 } 32 33 public function testCreateProject() 34 { 35 $client = static::makeClient(true); 36 37 $file = __DIR__.'/../../UploadedFileBundle/Tests/Fixtures/files/uploadtest.png'; 38 $originalFilename = 'uploadtest.png'; 39 $mimeType = 'image/png'; 40 41 $image = new UploadedFile( 42 $file, 43 $originalFilename, 44 $mimeType, 45 filesize($file) 46 ); 47 48 $client->request( 49 'POST', 50 '/api/temp_uploaded_files/upload', 51 [], 52 ['userfile' => $image] 53 ); 54 55 $uploadedFile = json_decode($client->getResponse()->getContent()); 56 57 /** 58 * @var Part 59 */ 60 $part = $this->fixtures->getReference('part.1'); 61 $part2 = $this->fixtures->getReference('part.2'); 62 63 $serializedPart1 = $this->getContainer()->get('serializer')->normalize( 64 $part, 65 'jsonld' 66 ); 67 68 $serializedPart2 = $this->getContainer()->get('serializer')->normalize( 69 $part2, 70 'jsonld' 71 ); 72 73 $project = [ 74 'name' => 'foobar', 75 'description' => 'testdescription', 76 'attachments' => [ 77 $uploadedFile->image, 78 ], 79 'parts' => [ 80 [ 81 'quantity' => 1, 82 'part' => $serializedPart1, 83 'remarks' => 'testremark', 84 'overageType' => ProjectPart::OVERAGE_TYPE_ABSOLUTE, 85 'overage' => 0, 86 ], 87 [ 88 'quantity' => 2, 89 'part' => $serializedPart2, 90 'remarks' => 'testremark2', 91 'overageType' => ProjectPart::OVERAGE_TYPE_ABSOLUTE, 92 'overage' => 0, 93 ], 94 ], 95 ]; 96 97 $client->request( 98 'POST', 99 '/api/projects', 100 [], 101 [], 102 [], 103 json_encode($project) 104 ); 105 106 $response = json_decode($client->getResponse()->getContent()); 107 108 $this->assertObjectHasAttribute('@type', $response); 109 $this->assertEquals('Project', $response->{'@type'}); 110 111 $this->assertObjectHasAttribute('name', $response); 112 $this->assertEquals('foobar', $response->name); 113 114 $this->assertObjectHasAttribute('description', $response); 115 $this->assertEquals('testdescription', $response->description); 116 117 $this->assertObjectHasAttribute('parts', $response); 118 $this->assertInternalType('array', $response->parts); 119 120 $this->assertEquals(2, count($response->parts)); 121 $this->assertArrayHasKey(0, $response->parts); 122 $this->assertEquals('ProjectPart', $response->parts[0]->{'@type'}); 123 $this->assertEquals(1, $response->parts[0]->quantity); 124 $this->assertEquals('testremark', $response->parts[0]->remarks); 125 $this->assertEquals('Part', $response->parts[0]->part->{'@type'}); 126 127 $this->assertObjectHasAttribute('attachments', $response); 128 $this->assertEquals(1, count($response->attachments)); 129 $this->assertArrayHasKey(0, $response->attachments); 130 $this->assertEquals('ProjectAttachment', $response->attachments[0]->{'@type'}); 131 132 unset($response->parts[0]); 133 } 134 135 public function testProjectPartRemoval() 136 { 137 $client = static::makeClient(true); 138 139 $project = $this->fixtures->getReference('project'); 140 $projectPart = $this->fixtures->getReference('projectpart.1'); 141 $project->removePart($projectPart); 142 143 $serializedProject = $this->getContainer()->get('serializer')->normalize( 144 $project, 145 'jsonld' 146 ); 147 148 $iriConverter = $this->getContainer()->get('api.iri_converter'); 149 $iri = $iriConverter->getIriFromItem($project); 150 151 $client->request( 152 'PUT', 153 $iri, 154 [], 155 [], 156 [], 157 json_encode($serializedProject) 158 ); 159 160 $response = json_decode($client->getResponse()->getContent()); 161 162 $this->assertInternalType('array', $response->parts); 163 $this->assertArrayNotHasKey( 164 1, 165 $response->parts, 166 'When removing an entry from the ArrayCollection, the array must be resorted!' 167 ); 168 $this->assertEquals(1, count($response->parts)); 169 } 170 171 public function testProjectAttachmentRemoval() 172 { 173 $client = static::makeClient(true); 174 175 /** 176 * @var Project 177 */ 178 $project = $this->fixtures->getReference('project'); 179 180 $projectAttachment = new ProjectAttachment(); 181 182 $fileService = $this->getContainer()->get('partkeepr_uploadedfile_service'); 183 $fileService->replaceFromData($projectAttachment, 'BLA', 'test.txt'); 184 185 $project->addAttachment($projectAttachment); 186 $this->getContainer()->get('doctrine.orm.default_entity_manager')->flush($project); 187 188 $project->removeAttachment($projectAttachment); 189 190 $serializedProject = $this->getContainer()->get('serializer')->normalize( 191 $project, 192 'jsonld' 193 ); 194 195 $iriConverter = $this->getContainer()->get('api.iri_converter'); 196 $iri = $iriConverter->getIriFromItem($project); 197 198 $client->request( 199 'PUT', 200 $iri, 201 [], 202 [], 203 [], 204 json_encode($serializedProject) 205 ); 206 207 $response = json_decode($client->getResponse()->getContent()); 208 209 $this->assertInternalType('array', $response->attachments); 210 $this->assertArrayNotHasKey( 211 1, 212 $response->attachments, 213 'When removing an entry from the ArrayCollection, the array must be resorted!' 214 ); 215 216 $this->assertEquals(0, count($response->attachments)); 217 } 218 219 /** 220 * Tests that the project part does not contain a reference to the project. This is because we serialize the 221 * project reference as IRI and not as object, which causes problems when reading in the project part in the 222 * frontend and serializing it back. 223 */ 224 public function testAbsentProjectReference() 225 { 226 $client = static::makeClient(true); 227 228 $project = $this->fixtures->getReference('project'); 229 230 $iriConverter = $this->getContainer()->get('api.iri_converter'); 231 $iri = $iriConverter->getIriFromItem($project); 232 233 $client->request( 234 'GET', 235 $iri, 236 [], 237 [], 238 [] 239 ); 240 241 $project = json_decode($client->getResponse()->getContent()); 242 243 $this->assertObjectHasAttribute("parts", $project); 244 $this->assertInternalType("array", $project->parts); 245 246 foreach ($project->parts as $part) { 247 $this->assertObjectNotHasAttribute("project", $part); 248 } 249 } 250 }