/** * Test save : post and put. */ public function testSaveGetDelete() { $startTime = new DateTime(); $endTime = new DateTime(); $endTime->modify('+8 hours'); $data = ['manager_id' => 1000000, 'employee_id' => 1, 'break' => 0.5, 'start_time' => $startTime, 'end_time' => $endTime, 'created_at' => $startTime, 'updated_at' => $startTime]; // Post $result = $this->shiftApi->save($data); $this->assertNotFalse($result); $shift = $this->em->getRepository('App\\Entity\\Shift')->findOneBy(['manager_id' => 1000000]); $this->assertInstanceOf('App\\Entity\\Shift', $shift); // Put $data['id'] = $shift->getId(); $data['manager_id'] = 2000000; $data['updated_at'] = new DateTime(); $result = $this->shiftApi->save($data); $this->assertNotFalse($result); // Get $shift = $this->shiftApi->get(12345678912345.0); $this->assertEmpty($shift['data']); $result = $this->shiftApi->get($data['id']); $this->assertInternalType('array', $result['data']); // Delete. $result = $this->shiftApi->delete($result['data']['id']); $this->assertNotFalse($result); }
/** * @param array $data * @return bool */ public static function addUser(array $data) { $em = DoctrineAdapter::getEntityManager(); $user = new User(); $user->setName($data['name']); $user->setRole($data['role']); $user->setEmail($data['email']); $user->setPhone($data['phone']); $user->setCreatedAt($data['created_at']); $user->setUpdatedAt($data['updated_at']); $em->persist($user); try { $em->flush(); return $user; } catch (\Exception $e) { return false; } }
/** * Tests adding a User. */ public function testAddUser() { $data = ['name' => 'userX', 'role' => AccountApi::$roles['manager'], 'email' => '*****@*****.**', 'phone' => '5555555555', 'created_at' => new DateTime(), 'updated_at' => new DateTime()]; $user = AccountApi::addUser($data); $this->assertNotFalse($user); $this->assertInternalType('int', $user->getId()); $this->assertEquals($data['name'], $user->getName()); $this->assertEquals($data['role'], $user->getRole()); $this->assertEquals($data['email'], $user->getEmail()); $this->assertEquals($data['phone'], $user->getphone()); $this->assertInstanceOf('DateTime', $user->getCreatedAt()); $this->assertEquals($data['created_at'], $user->getCreatedAt()); $this->assertInstanceOf('DateTime', $user->getUpdatedAt()); $this->assertEquals($data['updated_at'], $user->getUpdatedAt()); if ($user) { $em = DoctrineAdapter::getEntityManager(); $user = $em->find('App\\Entity\\User', ['id' => $user->getId()]); $em->remove($user); $em->flush(); } }
/** * @param Request $request * @param int $type * @param bool $catch * @return \Symfony\Component\HttpFoundation\Response */ public function handle(Request $request, $type = self::MASTER_REQUEST, $catch = true) { $em = DoctrineAdapter::getEntityManager(); if (isset($_COOKIE['token'])) { $token = unserialize($_COOKIE['token']); $key = $token['key']; $user = $em->find('App\\Entity\\User', $token['id']); $this->app->user = $user; $role = $user->getRole(); } else { $key = null; $role = null; } $route = $request->getPathInfo(); $protectedRoutes = ['/shifts' => ['access' => ['manager']]]; if (array_key_exists($route, $protectedRoutes) && (!$key || $key !== AccountApi::$secretKey || !in_array($role, $protectedRoutes[$route]['access']))) { header('Content-Type', 'application/json'); $response = new StandardResponse(); $response->setMessage('You are not authorized to access this route.'); echo json_encode($response->getObjectVars()); } else { return $this->app->handle($request); } }
/** * Set up. */ public function setup() { $this->userApi = new UserApi(); $this->em = DoctrineAdapter::getEntityManager(); }
/** * Tests the DoctrineAdapter class static EntityManger getter. */ public function testCanGetEntityManager() { $em = DoctrineAdapter::getEntityManager(); $this->assertInstanceOf('Doctrine\\ORM\\EntityManager', $em); }
/** * Construct. */ public function __construct() { $this->em = DoctrineAdapter::getEntityManager(); }
<?php require 'vendor/autoload.php'; use Doctrine\ORM\Tools\Console\ConsoleRunner; use App\Adapter\DoctrineAdapter; return ConsoleRunner::createHelperSet(DoctrineAdapter::getEntityManager());