/**
  * Registers a user with the details within the HTTP Request object if no user currently exists
  * with a matching username or email address.
  *
  * @param Request       $request The HTTP Request object.
  * @param Response      $response The HTTP Response object.
  * @param array         $args The array containing arguments provided.
  *
  * @return string       The message from the registration process.
  */
 public function register(Request $request, Response $response, array $args)
 {
     //get post variables from request body
     $post = $request->getParams();
     //validate post variables (exist, and as expected)
     /** @var Validator $v */
     $v = new Validator($post);
     $v->rule('required', ['username', 'password', 'email', 'first_name', 'last_name', 'date_of_birth']);
     $v->rule('email', 'email');
     $ret = array();
     if ($v->validate()) {
         if ($this->dbService->userExists($post['username'], $post['email'])) {
             $ret['message'] = "User already exists.";
             $ret['success'] = false;
         } else {
             if ($key = $this->dbService->addNewUser($post) ?: false) {
                 $this->emailService->sendVerificationEmail($post['email'], $post['first_name'], $post['last_name'], $key);
                 $ret['message'] = "You are now registered! A confirmation email has been sent to you. Please open it and follow\r\n                    the instructions provided.";
                 $ret['success'] = true;
             } else {
                 $ret['message'] = "Something went wrong. Please try again later.";
                 $ret['success'] = false;
             }
         }
     } else {
         $ret['message'] = "Please complete all fields.";
         $ret['success'] = false;
     }
     return json_encode($ret);
 }
 public function testCanAddNewUser()
 {
     $data = array("username" => "TestUser", "password" => "test123", "email" => "*****@*****.**", "first_name" => "Testy", "last_name" => "McTesterson", "date_of_birth" => "1970-01-01");
     $this->dbService->addNewUser($data);
     $stmt = self::$db->prepare("SELECT * \r\n                                    FROM user_profile \r\n                                    WHERE username = :username");
     $stmt->execute(array(':username' => $data['username']));
     $this->assertEquals(1, $stmt->rowCount());
     $results = $stmt->fetch(PDO::FETCH_ASSOC);
     $this->assertEquals($data['username'], $results['username']);
     $this->assertEquals($this->dbService->getPassword($data['username']), $results['password']);
     $this->assertEquals($data['email'], $results['email']);
     $this->assertEquals($data['first_name'], $results['first_name']);
     $this->assertEquals($data['last_name'], $results['last_name']);
     $this->assertEquals($data['date_of_birth'], $results['date_of_birth']);
     $stmt = self::$db->prepare("SELECT *\r\n                          FROM user_registration\r\n                          WHERE user_id = :id");
     $stmt->execute(array(':id' => $results['id']));
     $results = $stmt->fetch(PDO::FETCH_ASSOC);
     $data['key'] = $results['verification'];
     $this->assertEquals(1, $stmt->rowCount());
     return $data;
 }