public function getById(Uuid $id)
 {
     $result = mysqli_query($this->connection, "SELECT `id`, `username`, `firstName`, `lastName` FROM `user` WHERE `id` = '{$id}';");
     if ($row = mysqli_fetch_array($result, MYSQLI_ASSOC)) {
         $user = new User(new Uuid($row['id']));
         $user->setUsername($row['username']);
         $user->setFirstName($row['firstName']);
         $user->setLastName($row['lastName']);
         return $user;
     }
     return false;
 }
 public function decodeUpdateUser(Request $request, $id)
 {
     if (!(0 === strpos($request->headers->get('Content-Type'), 'application/json'))) {
         return false;
     }
     $data = json_decode($request->getContent(), true);
     if (!array_key_exists('username', $data)) {
         return false;
     }
     $newUser = new User(new Uuid($id));
     $newUser->setUsername(new StringLiteral($data['username']));
     if (array_key_exists('is_admin', $data)) {
         $newUser->setIsAdmin($data['is_admin']);
     }
     if (array_key_exists('is_owner', $data)) {
         $newUser->setIsOwner($data['is_owner']);
     }
     return $newUser;
 }
Beispiel #3
0
 */
use Notes\Domain\Entity\User;
use Notes\Domain\ValueObject\StringLiteral;
use Notes\Domain\ValueObject\Uuid;
describe('Notes\\Domain\\Entity\\User', function () {
    describe('->__construct()', function () {
        it('should return a User object', function () {
            $actual = new User(new Uuid());
            expect($actual)->to->be->instanceof('\\Notes\\Domain\\Entity\\User');
        });
    });
    describe('->getId()', function () {
        it('should return the user\'s username', function () {
            $uuid = new Uuid();
            expect($uuid->isValidV4())->to->be->true();
            $user = new User($uuid);
            $actual = $user->getId();
            expect($actual)->to->be->instanceof('\\Notes\\Domain\\ValueObject\\Uuid');
            expect($actual->__toString())->equal($uuid->__toString());
        });
    });
    describe('->get/setUsername', function () {
        it('should get/set the correct the user\'s username', function () {
            $user = new User(new Uuid());
            $user->setUsername(new StringLiteral('Joe'));
            $actual = $user->getUsername();
            expect($actual)->to->be->instanceof('\\Notes\\Domain\\ValueObject\\StringLiteral');
            expect($actual->__toString())->equal('Joe');
        });
    });
});
         $exception = null;
         try {
             new User($username, $invalidPassword, $invalidEmail);
         } catch (exception $e) {
             $exception = $e;
         }
         expect($exception)->to->be->instanceof('\\InvalidArgumentException');
     });
 });
 // tests set and get user
 describe('->getUsername()', function () {
     it('should return the users user name', function () {
         $faker = \Faker\Factory::create();
         $username = $faker->userName;
         $user = new User();
         $user->setUsername($username);
         // set method would likely be used by an admin to change the user's name if they have an inappropriate username
         //or if a user account is created by an admin without information it can be set later
         expect($user->getUsername())->equal($username);
     });
 });
 // tests set and get password
 describe('->getPassword()', function () {
     it('shoud return the users hashed password', function () {
         //$faker = \Faker\Factory::create(); wasnt creating the properly formatted password
         $password = '******';
         $user = new User();
         $user->setPassword($password);
         // setPassword would likely only be used for empty user objects made by an admin or to reset a password
         $hashpass = hash(sha256, $password);
         expect($user->getPassword())->equal($hashpass);
            $user->setUsername("Bazil900");
            $newUserRecord = $this->pdo->insert("users", [$user->getId()->__toString(), $user->getUsername()]);
            $actual = $this->pdo->delete("users", [$user->getId()->__toString()]);
            expect($actual)->to->be->true;
        });
    });
    describe('->select($table, $criteria = [])', function () {
        it('should retrieve (select) data from the database', function () {
            $user = new User(new Uuid());
            $user->setUsername("Bazil Alrashed001");
            $newUserRecord = $this->pdo->insert("users", [$user->getId()->__toString(), $user->getUsername()]);
            $actual = $this->pdo->select("users", [$user->getId()->__toString()]);
            expect(count($actual))->equal(1);
        });
    });
    describe('->sql($sql, $data = [])', function () {
        it('should execute any sql command against the database', function () {
            $user = new User(new Uuid());
            $user->setUsername("Bazil---999");
            $newUserRecord = $this->pdo->insert("users", [$user->getId()->__toString(), $user->getUsername()]);
            $actual = $this->pdo->sql("UPDATE users SET username=? WHERE userID=?", ["Basil999", $user->getId()->__toString()]);
            expect($actual)->to->be->true;
        });
    });
    describe('->close()', function () {
        it('should close the connection of the database', function () {
            $actual = $this->pdo->close();
            expect($actual)->to->be->null;
        });
    });
});
         $before = $actual->getUsers();
         $user = '******';
         $actual->addUser($user);
         $after = $actual->getUsers();
         expect(!in_array($user, $before));
         expect(in_array($user, $after));
     });
 });
 describe('->removeById($uuid)', function () {
     it('should delete a user that the admin is responsible for', function () {
         $uuid = new Uuid();
         $user = new User($uuid);
         $user->setUsername(new StringLiteral('mbrow234'));
         $value[] = $user;
         $user = new User($uuid);
         $user->setUsername(new StringLiteral('randomUser1'));
         $value[] = $user;
         $removeUuid = new Uuid();
         $checkUser = new User($removeUuid);
         $checkUser->setUsername(new StringLiteral('coolUser85'));
         $value[] = $checkUser;
         $actual = new Group(new Uuid());
         $actual->setUsers($value);
         $before = $actual->getUsers();
         $actual->removeUserById($removeUuid);
         $after = $actual->getUsers();
         expect(in_array($checkUser, $before));
         expect(!in_array($checkUser, $after));
     });
 });
 describe('->setName()', function () {
Beispiel #7
0
         $userFactory = new UserFactory();
         $faker = \Faker\Factory::create();
         $uuid = new StringLiteral($faker->uuid);
         $actual = new User($uuid);
         $actual = $userFactory->create();
         expect($actual)->to->be->instanceof('Notes\\Domain\\Entity\\User');
         expect($actual->getId())->to->be->instanceof('Notes\\Domain\\ValueObject\\Uuid');
     });
 });
 describe('->getUsername()', function () {
     it('should return the User\'s username', function () {
         $faker = \Faker\Factory::create();
         $uuid = new StringLiteral($faker->uuid);
         $username = new StringLiteral($faker->userName);
         $actual = new User($uuid);
         $actual->setUsername($username);
         expect($actual)->to->be->instanceof('Notes\\Domain\\Entity\\User');
         expect($actual->getUsername())->equal($username);
     });
 });
 describe('->getFirstName()', function () {
     it('should return the User\'s first name', function () {
         $faker = \Faker\Factory::create();
         $uuid = new StringLiteral($faker->uuid);
         $firstName = new StringLiteral($faker->firstName);
         $actual = new User($uuid);
         $actual->setFirstName($firstName);
         expect($actual)->to->be->instanceof('Notes\\Domain\\Entity\\User');
         expect($actual->getFirstName())->equal($firstName);
     });
 });
 /**
  * @param $row
  * @return User
  */
 private function generateUserFromRow($row)
 {
     $id = new Uuid($row[0]);
     $user = new User($id);
     $user->setUsername(new StringLiteral($row[1]));
     $user->setIsAdmin(intval($row[2]));
     $user->setIsOwner(intval($row[3]));
     return $user;
 }