Exemplo n.º 1
0
 /**
  * Assign device to me
  *
  * @param \Entity\Device $entity
  * @return Response
  */
 public function assign($entity)
 {
     if ($entity->getLocation()->getId() != $this->getUser()->getId()) {
         throw new DeviceNotFoundException();
     }
     if ($entity->getUser()) {
         throw new AlreadyHasOwnerException();
     }
     $entity->setUser($this->getUser());
     $this->flush();
     $response = new Response();
     $response->redirect('/device/location');
     return $response;
 }
Exemplo n.º 2
0
 private function prepareCloseByOwner()
 {
     $em = $this->getService('doctrine')->getEntityManager();
     $location1 = new Location();
     $location1->setName('Location name');
     $location1->setCity('Location city');
     $location1->setStreet('Location street');
     $location1->setNumber('Location number');
     $location1->setApartment('Location apartment');
     $location1->setPostal('00-000');
     $location1->setPhone('+48100000000');
     $location1->setEmail('*****@*****.**');
     $this->persist($location1);
     $location2 = new Location();
     $location2->setName('Location name 2');
     $location2->setCity('Location city');
     $location2->setStreet('Location street');
     $location2->setNumber('Location number');
     $location2->setApartment('Location apartment');
     $location2->setPostal('00-000');
     $location2->setPhone('+48100000000');
     $location2->setEmail('*****@*****.**');
     $this->persist($location2);
     $role = new Role();
     $role->setName('Admin');
     foreach ($em->getRepository('Entity\\Functionality')->findAll() as $functionality) {
         $role->getFunctionalities()->add($functionality);
     }
     $this->persist($role);
     $owner = new User();
     $owner->setEmail('*****@*****.**');
     $owner->setFirstName('first name');
     $owner->setLastName('last name');
     $owner->setLocation($location2);
     $owner->setRole($role);
     $this->persist($owner);
     $performer = new User();
     $performer->setEmail('*****@*****.**');
     $performer->setFirstName('first name');
     $performer->setLastName('last name');
     $performer->setLocation($location1);
     $performer->setRole($role);
     $this->persist($performer);
     $deviceTag = new DeviceTag();
     $deviceTag->setName('DeviceTag name');
     $this->persist($deviceTag);
     $device = new Device();
     $device->setName('Device name');
     $device->setPhoto('Device.photo.jpg');
     $device->getTags()->add($deviceTag);
     $device->setType($em->getRepository('Entity\\DeviceType')->findOneById(1));
     $device->setState($em->getRepository('Entity\\DeviceState')->findOneById(1));
     $device->setSerialNumber('Device serial number');
     $device->setState($em->getRepository('Entity\\DeviceState')->findOneById(2));
     $device->setSymbol('?');
     $device->setLocation($location1);
     $device->setUser($performer);
     $this->persist($device);
     $order = new Order();
     $order->setOwner($owner);
     $order->setState($em->getRepository('Entity\\OrderState')->findOneById(2));
     $order->setDevice($device);
     $order->setPerformer($performer);
     $order->setFetchedAt(new \DateTime());
     $this->persist($order);
     $this->flush();
     $session = $this->createSession();
     $session->set('user.id', $owner->getId());
     $client = $this->createClient($session);
     $client->loadPage('/order/show/' . $order->getId());
     $this->assertEquals(200, $client->getResponse()->getStatusCode(), 'Invalid status code.');
     $timeLines = $client->findElements('.timeline-entry');
     $this->assertCount(2, $timeLines, 'Invalid number steps');
     $buttons = $timeLines[1]->findElements('a');
     $this->assertCount(2, $buttons, 'Invalid buttons number');
     $this->assertEquals('Me', $buttons[0]->getHtml(), 'Invalid Me label');
     $this->assertEquals('My location', $buttons[1]->getHtml(), 'Invalid Location label');
     return array($buttons, $order, $performer, $device, $location2, $client, $owner);
 }
Exemplo n.º 3
0
 public function testEdit()
 {
     $em = $this->getService('doctrine')->getEntityManager();
     $location = new Location();
     $location->setName('Location name');
     $location->setCity('Location city');
     $location->setStreet('Location street');
     $location->setNumber('Location number');
     $location->setApartment('Location apartment');
     $location->setPostal('00-000');
     $location->setPhone('+48100000000');
     $location->setEmail('*****@*****.**');
     $this->persist($location);
     $deviceTag = new DeviceTag();
     $deviceTag->setName('DeviceTag name');
     $this->persist($deviceTag);
     $device = new Device();
     $device->setName('Device name');
     $device->setPhoto('Device.photo.jpg');
     $device->getTags()->add($deviceTag);
     $device->setType($em->getRepository('Entity\\DeviceType')->findOneById(1));
     $device->setWarrantyExpirationDate(new \DateTime('2015-01-01'));
     $device->setPurchaseDate(new \DateTime('2014-01-01'));
     $device->setPrice(12.05);
     $device->setNote('Note');
     $device->setSerialNumber('Device serial number');
     $device->setState($em->getRepository('Entity\\DeviceState')->findOneById(1));
     $device->setLocation($location);
     $device->setSymbol('REF1');
     $this->persist($device);
     $this->flush();
     $session = $this->createSession();
     $session->set('user.id', $this->user->getId());
     $client = $this->createClient($session);
     $client->loadPage('/device/edit/' . $device->getId());
     $this->assertEquals(200, $client->getResponse()->getStatusCode(), 'Invalid status code.');
     $form = $client->getElement('form');
     $fields = $form->getFields();
     $this->assertCount(9, $fields, 'Invalid number fields');
     $this->assertEquals('Device name', $fields[0]->getData(), 'Invalid value for name');
     $this->assertEquals('2015-01-01', $fields[1]->getData(), 'Invalid value for warranty expiration date');
     $this->assertEquals('2014-01-01', $fields[2]->getData(), 'Invalid value for purchase date');
     $this->assertEquals('12.05', $fields[3]->getData(), 'Invalid value for price');
     $this->assertEquals('', $fields[4]->getData(), 'Invalid value for photo');
     $this->assertEquals('DeviceTag name', $fields[5]->getData(), 'Invalid value for tags');
     $this->assertEquals('Device serial number', $fields[6]->getData(), 'Invalid value for serial number');
     $this->assertEquals('Note', $fields[7]->getData(), 'Invalid value for note');
     $this->assertEquals('1', $fields[8]->getData(), 'Invalid value for type');
     $fields[0]->setData('');
     $fields[1]->setData('');
     $fields[2]->setData('');
     $fields[3]->setData('');
     $fields[4]->setData('');
     $fields[5]->setData('');
     $fields[6]->setData('');
     $fields[7]->setData('');
     $fields[8]->setData('');
     $form->submit();
     $form = $client->getElement('form');
     $fields = $form->getFields();
     $this->assertCount(9, $fields, 'Invalid number fields');
     $this->assertEquals('Value can not empty', $fields[0]->getParent()->getElement('label')->getText(), 'Invalid error message for name');
     $this->assertFalse($fields[1]->getParent()->hasElement('label'), 'Redundant error message for warranty expiration date');
     $this->assertFalse($fields[2]->getParent()->hasElement('label'), 'Redundant error message for warranty expiration date');
     $this->assertFalse($fields[3]->getParent()->hasElement('label'), 'Redundant error message for price');
     $this->assertFalse($fields[4]->getParent()->hasElement('label'), 'Redundant error message for photo');
     $this->assertEquals('Value can not empty', $fields[5]->getParent()->getElement('label')->getText(), 'Invalid error message for tags');
     $this->assertEquals('Value can not empty', $fields[6]->getParent()->getElement('label')->getText(), 'Invalid error message for serial number');
     $this->assertFalse($fields[7]->getParent()->hasElement('label'), 'Redundant error message for note');
     $this->assertEquals('Value can not empty', $fields[8]->getParent()->getElement('label')->getText(), 'Invalid error message for type');
     $fields[0]->setData('Name edit');
     $fields[1]->setData('2016-01-01');
     $fields[2]->setData('2015-01-01');
     $fields[3]->setData('5.32');
     $fields[5]->setData('tag 1,tag 2');
     $fields[6]->setData('serial number');
     $fields[7]->setData('note edit');
     $fields[8]->setData('2');
     $form->submit();
     $this->assertEquals('/device', $client->getUrl(), 'Invalid url form after submit');
     $em->clear();
     $device = $em->getRepository('Entity\\Device')->findOneBy(array('id' => $device->getId()));
     $this->assertEquals('Name edit', $device->getName(), 'Invalid device name');
     $this->assertEquals('2016-01-01', $device->getWarrantyExpirationDate()->format('Y-m-d'), 'Invalid device warranty expiration date');
     $this->assertEquals('2015-01-01', $device->getPurchaseDate()->format('Y-m-d'), 'Invalid device purchase date');
     $this->assertEquals(5.32, $device->getPrice(), 'Invalid device price');
     $this->assertEquals('note edit', $device->getNote(), 'Invalid device note');
     $this->assertEquals(2, $device->getType()->getId(), 'Invalid device type');
     $this->assertEquals($location->getId(), $device->getLocation()->getId(), 'Invalid device location');
     $tags = $device->getTags();
     $this->assertCount(2, $tags, 'Invalid count device tags');
     $this->assertTrue(in_array('tag 1', array($tags[0]->getName(), $tags[1]->getName())), 'Invalid device tag 1 name');
     $this->assertTrue(in_array('tag 2', array($tags[0]->getName(), $tags[1]->getName())), 'Invalid device tag 2 name');
     $this->assertEquals('serial number', $device->getSerialNumber(), 'Invalid device serial number');
 }
Exemplo n.º 4
0
 /**
  * Save device to database
  *
  * @param \Entity\Device $entity
  * @param $data
  * @param $serialNumber
  * @param null $state
  */
 private function saveEntity($entity, $data, $serialNumber, $state = null)
 {
     $entity->setName($data['name']);
     $entity->setDimensions($data['dimensions']);
     $entity->setWeight($data['weight']);
     $entity->setType($this->cast('Mapper\\DeviceType', $data['type']));
     $entity->setSerialNumber($serialNumber);
     $entity->setWarrantyExpirationDate($data['warrantyExpirationDate'] ? new \DateTime($data['warrantyExpirationDate']) : NULL);
     $entity->setNote($data['note']);
     $entity->setPrice($data['price'] ? $data['price'] : NULL);
     if ($state) {
         $entity->setState($this->cast('Mapper\\DeviceState', $state));
     }
     if (isset($data['location'])) {
         $entity->setLocation($this->cast('Mapper\\Location', $data['location']));
     }
     $this->persist($entity);
     $tagsPart = explode(',', $data['tags']);
     $entity->getTags()->clear();
     foreach ($tagsPart as $tag) {
         $tag = trim($tag);
         $tagEntity = $this->findOne('DeviceTag', array('name' => $tag));
         if ($tagEntity == null) {
             $tagEntity = new \Entity\DeviceTag();
             $tagEntity->setName($tag);
             $this->persist($tagEntity);
             $this->flush();
         }
         $entity->getTags()->add($tagEntity);
     }
     if ($data['photo']) {
         $dir = 'uploaded/device/photo/';
         $name = '';
         do {
             $name = rand() . '_' . $data['photo']->getName();
         } while (file_exists($dir . $name));
         copy($data['tmpPhoto'], $dir . $name);
         $entity->setPhoto($name);
     }
 }
Exemplo n.º 5
0
 public function testAssign()
 {
     //prepare data
     $em = $this->getService('doctrine')->getEntityManager();
     $deviceTag = new DeviceTag();
     $deviceTag->setName('DeviceTag name');
     $this->persist($deviceTag);
     $device = new Device();
     $device->setName('Device name');
     $device->setPhoto('Device.photo.jpg');
     $device->getTags()->add($deviceTag);
     $device->setType($em->getRepository('Entity\\DeviceType')->findOneById(1));
     $device->setSerialNumber('Device serial number');
     $device->setState($em->getRepository('Entity\\DeviceState')->findOneById(2));
     $device->setSymbol('ABC');
     $device->setLocation($this->user->getLocation());
     $this->persist($device);
     $this->flush();
     //prepare client
     $session = $this->createSession();
     $session->set('user.id', $this->user->getId());
     $client = $this->createClient($session);
     $client->loadPage('/device/location/assign/' . $device->getId());
     //load page
     $this->assertEquals(200, $client->getResponse()->getStatusCode(), 'Invalid status code.');
     $panelBody = $client->getElement('.panel-body');
     $buttons = $panelBody->findElements('a');
     $this->assertCount(2, $buttons, 'Invalid number buttons');
     $this->assertEquals('Yes', $buttons[0]->getText(), 'Invalid text button YES');
     $this->assertEquals('No', $buttons[1]->getText(), 'Invalid text button NO');
     $buttons[1]->click();
     $this->assertEquals('/device/location', $client->getUrl(), 'Invalid url button NO.');
     $buttons[0]->click();
     $this->assertEquals('/device/location', $client->getUrl(), 'Invalid url button YES.');
     //check data in database
     $em->clear();
     $device = $em->getRepository('Entity\\Device')->findOneBy(array('id' => $device->getId()));
     $this->assertEquals($this->user->getId(), $device->getUser()->getId(), 'Invalid device user.');
 }
Exemplo n.º 6
0
 /**
  * Assign device to my location
  *
  * @param \Entity\Device $entity
  * @return Response
  */
 public function assign($entity)
 {
     if ($entity->getUser()->getId() != $this->getUser()->getId()) {
         throw new YouAreNotOwnerException();
     }
     $entity->setUser(null);
     $this->flush();
     $response = new Response();
     $response->redirect('/device/my');
     return $response;
 }
Exemplo n.º 7
0
 public function testRemove()
 {
     $em = $this->getService('doctrine')->getEntityManager();
     $deviceType = new DeviceType();
     $deviceType->setName('Test type');
     $deviceType->setSymbolPrefix('TEST');
     $this->persist($deviceType);
     $location = new Location();
     $location->setName('Location name');
     $location->setCity('Location city');
     $location->setStreet('Location street');
     $location->setNumber('Location number');
     $location->setApartment('Location apartment');
     $location->setPostal('00-000');
     $location->setPhone('+48100000000');
     $location->setEmail('*****@*****.**');
     $this->persist($location);
     $deviceTag = new DeviceTag();
     $deviceTag->setName('DeviceTag name');
     $this->persist($deviceTag);
     $device = new Device();
     $device->setName('Device name');
     $device->setPhoto('Device.photo.jpg');
     $device->getTags()->add($deviceTag);
     $device->setType($deviceType);
     $device->setSerialNumber('Device serial number');
     $device->setState($em->getRepository('Entity\\DeviceState')->findOneById(1));
     $device->setLocation($location);
     $device->setSymbol($deviceType->getSymbolPrefix() . '1');
     $this->persist($device);
     $this->flush();
     $session = $this->createSession();
     $session->set('user.id', $this->user->getId());
     $client = $this->createClient($session);
     $client->loadPage('/devicetype/remove/' . $deviceType->getId());
     $this->assertEquals(200, $client->getResponse()->getStatusCode(), 'Invalid status code.');
     $panelBody = $client->getElement('.panel-body');
     $buttons = $panelBody->findElements('a');
     $this->assertCount(2, $buttons, 'Invalid number buttons');
     $this->assertEquals('Yes', $buttons[0]->getText(), 'Invalid text button YES');
     $this->assertEquals('No', $buttons[1]->getText(), 'Invalid text button NO');
     $buttons[1]->click();
     $this->assertEquals('/devicetype', $client->getUrl(), 'Invalid url button NO.');
     $buttons[0]->click();
     $this->assertEquals('/devicetype', $client->getUrl(), 'Invalid url button YES.');
     //check removed in database
     $this->assertCount(2, $em->getRepository('Entity\\DeviceType')->findAll());
     $this->assertCount(0, $em->getRepository('Entity\\Device')->findAll());
 }