diff --git a/data/Migrations/Version20210522230437.php b/data/Migrations/Version20210522230437.php new file mode 100644 index 00000000..778513c4 --- /dev/null +++ b/data/Migrations/Version20210522230437.php @@ -0,0 +1,37 @@ +abortIf( + $this->connection->getDatabasePlatform()->getName() !== 'mysql', + 'Migration can only be executed safely on \'mysql\'.' + ); + + $this->addSql('ALTER TABLE provision + CHANGE valuta date DATE NOT NULL, + CHANGE importo amount NUMERIC(8, 2) NOT NULL, + CHANGE descrizione description VARCHAR(255) NOT NULL'); + } + + public function down(Schema $schema): void + { + $this->abortIf( + $this->connection->getDatabasePlatform()->getName() !== 'mysql', + 'Migration can only be executed safely on \'mysql\'.' + ); + + $this->addSql('ALTER TABLE provision + CHANGE date valuta DATE NOT NULL, + CHANGE amount importo NUMERIC(8, 2) NOT NULL, + CHANGE description descrizione VARCHAR(255) CHARACTER SET utf8 NOT NULL COLLATE `utf8_unicode_ci`'); + } +} diff --git a/data/Migrations/Version20210523181349.php b/data/Migrations/Version20210523181349.php new file mode 100644 index 00000000..4da1c538 --- /dev/null +++ b/data/Migrations/Version20210523181349.php @@ -0,0 +1,37 @@ +abortIf( + $this->connection->getDatabasePlatform()->getName() !== 'mysql', + 'Migration can only be executed safely on \'mysql\'.' + ); + + $this->addSql('ALTER TABLE setting + CHANGE payday paypay SMALLINT UNSIGNED DEFAULT 1 NOT NULL, + CHANGE monthsretrospective months SMALLINT UNSIGNED DEFAULT 12 NOT NULL, + CHANGE stored provisioning TINYINT(1) DEFAULT 0 NOT NULL'); + } + + public function down(Schema $schema): void + { + $this->abortIf( + $this->connection->getDatabasePlatform()->getName() !== 'mysql', + 'Migration can only be executed safely on \'mysql\'.' + ); + + $this->addSql('ALTER TABLE setting + CHANGE paypay payDay SMALLINT UNSIGNED DEFAULT 1 NOT NULL, + CHANGE months monthsRetrospective SMALLINT UNSIGNED DEFAULT 12 NOT NULL, + CHANGE provisioning stored TINYINT(1) DEFAULT 0 NOT NULL'); + } +} diff --git a/module/Application/src/Application/Entity/Account.php b/module/Application/src/Application/Entity/Account.php index 7cad254e..7767055e 100644 --- a/module/Application/src/Application/Entity/Account.php +++ b/module/Application/src/Application/Entity/Account.php @@ -88,7 +88,14 @@ public function isClosed(): bool public function getArrayCopy(): array { - return get_object_vars($this); + return [ + 'id' => $this->id, + 'user' => $this->user, + 'name' => $this->name, + 'recap' => $this->recap, + 'closed' => $this->closed, + 'movements' => $this->movements, + ]; } public function exchangeArray(array $data = []): self diff --git a/module/Application/src/Application/Entity/Category.php b/module/Application/src/Application/Entity/Category.php index 1805f35a..cab1804e 100644 --- a/module/Application/src/Application/Entity/Category.php +++ b/module/Application/src/Application/Entity/Category.php @@ -89,7 +89,12 @@ public function setStatus(int $status): void public function getArrayCopy(): array { - return get_object_vars($this); + return [ + 'id' => $this->id, + 'user' => $this->user, + 'description' => $this->description, + 'status' => $this->status, + ]; } public function exchangeArray(array $data = []): void diff --git a/module/Application/src/Application/Entity/Movement.php b/module/Application/src/Application/Entity/Movement.php index 00692841..94de8e3a 100644 --- a/module/Application/src/Application/Entity/Movement.php +++ b/module/Application/src/Application/Entity/Movement.php @@ -119,7 +119,14 @@ public function setDescription(string $description): void public function getArrayCopy(): array { - return get_object_vars($this); + return [ + 'id' => $this->id, + 'account' => $this->amount, + 'category' => $this->category, + 'date' => $this->date, + 'amount' => $this->amount, + 'description' => $this->description, + ]; } public function exchangeArray(array $data): void diff --git a/module/Application/src/Application/Entity/Provision.php b/module/Application/src/Application/Entity/Provision.php index d54cda80..6e8ed1d8 100644 --- a/module/Application/src/Application/Entity/Provision.php +++ b/module/Application/src/Application/Entity/Provision.php @@ -36,22 +36,22 @@ class Provision implements InputFilterAwareInterface private $user; /** - * @ORM\Column(name="valuta", type="date", nullable=false) + * @ORM\Column(name="date", type="date", nullable=false) * @var \DateTime */ - private $valuta; + private $date; /** - * @ORM\Column(name="importo", type="decimal", precision=8, scale=2, nullable=false) + * @ORM\Column(name="amount", type="decimal", precision=8, scale=2, nullable=false) * @var float */ - private $importo; + private $amount; /** - * @ORM\Column(name="descrizione", type="string", nullable=false) + * @ORM\Column(name="description", type="string", nullable=false) * @var string */ - private $descrizione; + private $description; public function getId(): int { @@ -63,64 +63,63 @@ public function setUser(User $user): void $this->user = $user; } - public function getValuta(): \DateTime + public function getDate(): \DateTime { - return $this->valuta; + return $this->date; } - public function setValuta(\DateTime $valuta): void + public function setDate(\DateTime $date): void { - $this->valuta = $valuta; + $this->date = $date; } - public function getImporto(): float + public function getAmount(): float { - return $this->importo; + return $this->amount; } - public function setImporto(float $importo): void + public function setAmount(float $amount): void { - $this->importo = $importo; + $this->amount = $amount; } - public function getDescrizione(): string + public function getDescription(): string { - return $this->descrizione; + return $this->description; } - public function setDescrizione(string $descrizione): void + public function setDescription(string $description): void { - $this->descrizione = $descrizione; + $this->description = $description; } public function getArrayCopy(): array { - return get_object_vars($this); + return [ + 'id' => $this->id, + 'user' => $this->user, + 'date' => $this->date, + 'amount' => $this->amount, + 'description' => $this->description, + ]; } - /** - * @throws \Exception - */ public function exchangeArray(array $data = []): void { - if (isset($data['id'])) { - $this->id = $data['id']; + if (isset($data['user'])) { + $this->user = $data['user']; + } + if (isset($data['date'])) { + $this->date = new \DateTime($data['date']); } - if (isset($data['userId'])) { - $this->user = $data['userId']; + if (isset($data['amount'])) { + $this->amount = $data['amount']; } - if (isset($data['valuta'])) { - $this->valuta = new \DateTime($data['valuta']); + if (isset($data['description'])) { + $this->description = $data['description']; } - $this->importo = $data['importo'] ?? null; - $this->descrizione = $data['descrizione'] ?? null; } - /** - * @param \Laminas\InputFilter\InputFilterInterface $inputFilter - * @return \Laminas\InputFilter\InputFilterAwareInterface - * @throws \Exception - */ public function setInputFilter(InputFilterInterface $inputFilter): InputFilterAwareInterface { throw new \Exception('Not used'); @@ -132,17 +131,17 @@ public function getInputFilter(): InputFilterInterface $inputFilter = new InputFilter(); $inputFilter->add([ - 'name' => 'valuta', + 'name' => 'date', 'required' => true, 'filters' => [['name' => StringTrim::class]], ]); $inputFilter->add([ - 'name' => 'importo', + 'name' => 'amount', 'required' => true, 'filters' => [['name' => StringTrim::class]], ]); $inputFilter->add([ - 'name' => 'descrizione', + 'name' => 'description', 'required' => true, 'filters' => [['name' => StringTrim::class]], ]); diff --git a/module/Application/src/Application/Entity/Setting.php b/module/Application/src/Application/Entity/Setting.php index 1fabe62d..f65724dd 100644 --- a/module/Application/src/Application/Entity/Setting.php +++ b/module/Application/src/Application/Entity/Setting.php @@ -1,5 +1,7 @@ user = $user; } - public function getPayDay(): int + public function getPayday(): int { - return $this->payDay; + return $this->payday; } - public function setPayDay(int $payDay): void + public function setPayday(int $payday): void { - if ($payDay < 1 || $payDay > 28) { - throw new \RuntimeException("Invalid payDay value: $payDay"); + if ($payday < 1 || $payday > 28) { + throw new \RuntimeException("Invalid payDay value: $payday"); } - $this->payDay = $payDay; + $this->payday = $payday; } - public function getMonthsRetrospective(): int + public function getMonths(): int { - return $this->monthsRetrospective; + return $this->months; } - public function setMonthsRetrospective(int $monthsRetrospective): void + public function setMonths(int $months): void { - $this->monthsRetrospective = $monthsRetrospective; + $this->months = $months; } - public function hasStored(): bool + public function hasProvisioning(): bool { - return $this->stored; + return $this->provisioning; } - public function setStored(bool $stored): void + public function setProvisioning(bool $provisioning): void { - $this->stored = $stored; + $this->provisioning = $provisioning; } public function getArrayCopy(): array { - return get_object_vars($this); + return [ + 'user' => $this->user, + 'payday' => $this->payday, + 'months' => $this->months, + 'provisioning' => $this->provisioning, + ]; } - /** - * Populate from an array. - * - * @param array $data - * @return Setting - */ - public function exchangeArray(array $data): self + public function exchangeArray(array $data): void { - if (isset($data['payDay'])) { - $this->setPayDay((int) $data['payDay']); + if (isset($data['payday'])) { + $this->setPayday((int) $data['payday']); } - if (isset($data['monthsRetrospective'])) { - $this->setMonthsRetrospective((int) $data['monthsRetrospective']); + if (isset($data['months'])) { + $this->setMonths((int) $data['months']); } - if (isset($data['stored'])) { - $this->setStored((bool) $data['stored']); + if (isset($data['provisioning'])) { + $this->setProvisioning((bool) $data['provisioning']); } - return $this; } - /** - * Set input filter - * @param \Laminas\InputFilter\InputFilterInterface $inputFilter - * @return $this - */ - public function setInputFilter(InputFilterInterface $inputFilter): self + public function setInputFilter(InputFilterInterface $inputFilter) { - $this->inputFilter = $inputFilter; - return $this; + throw new \Exception('Not used'); } public function getInputFilter(): InputFilterInterface @@ -124,18 +118,18 @@ public function getInputFilter(): InputFilterInterface if (!$this->inputFilter) { $this->inputFilter = new InputFilter(); $this->inputFilter->add([ - 'filters' => [['name' => ToInt::class]], - 'name' => 'payDay', + 'filters' => [['name' => ToInt::class]], + 'name' => 'payday', 'required' => true, ]); $this->inputFilter->add([ - 'filters' => [['name' => ToInt::class]], - 'name' => 'monthsRetrospective', + 'filters' => [['name' => ToInt::class]], + 'name' => 'months', 'required' => true, ]); $this->inputFilter->add([ - 'filters' => [['name' => ToInt::class]], - 'name' => 'stored', + 'filters' => [['name' => ToInt::class]], + 'name' => 'provisioning', 'required' => true, ]); } diff --git a/module/Application/src/Application/Entity/User.php b/module/Application/src/Application/Entity/User.php index b5ad82f7..b9cdc5a1 100644 --- a/module/Application/src/Application/Entity/User.php +++ b/module/Application/src/Application/Entity/User.php @@ -5,8 +5,8 @@ namespace Application\Entity; use Doctrine\ORM\Mapping as ORM; -use Laminas\InputFilter\InputFilterAwareInterface; use Laminas\InputFilter\InputFilter; +use Laminas\InputFilter\InputFilterAwareInterface; use Laminas\InputFilter\InputFilterInterface; /** @@ -92,53 +92,51 @@ class User implements InputFilterAwareInterface */ private $setting; - /** - * Convert the object to an array. - * - * @return array - */ public function getArrayCopy(): array { - return get_object_vars($this); + return [ + 'id' => $this->id, + 'email' => $this->email, + 'name' => $this->name, + 'surname' => $this->surname, + 'password' => $this->password, + 'salt' => $this->salt, + 'status' => $this->status, + 'role' => $this->role, + 'registrationToken' => $this->registrationToken, + 'lastLogin' => $this->lastLogin, + 'setting' => $this->setting, + ]; } - /** - * Populate from an array. - * - * @param array $data - */ public function exchangeArray(array $data = []): void { - $this->id = $data['id'] ?? null; - $this->name = $data['name'] ?? null; - $this->surname = $data['surname'] ?? null; - - if (array_key_exists('email', $data)) { - $this->email = $data['email']; + if (isset($data['name'])) { + $this->setName($data['name']); } - if (array_key_exists('password', $data)) { - $this->password = $data['password']; + if (isset($data['surname'])) { + $this->setSurname($data['surname']); } - if (array_key_exists('salt', $data)) { - $this->salt = $data['salt']; + if (isset($data['email'])) { + $this->setEmail($data['email']); } - if (array_key_exists('status', $data)) { - $this->status = $data['status']; + if (isset($data['password'])) { + $this->setPassword($data['password']); } - if (array_key_exists('role', $data)) { - $this->role = $data['role']; + if (isset($data['salt'])) { + $this->setSalt($data['salt']); } - if (array_key_exists('registrationToken', $data)) { + if (isset($data['status'])) { + $this->setStatus((int) $data['status']); + } + if (isset($data['role'])) { + $this->setRole($data['role']); + } + if (isset($data['registrationToken'])) { $this->registrationToken = $data['registrationToken']; } } - /** - * ATTENZIONE: filtri e form devono avere esattamente gli stessi campi - * - * @param InputFilterInterface $inputFilter - * @return $this - */ public function setInputFilter(InputFilterInterface $inputFilter): self { $this->inputFilter = $inputFilter; @@ -190,21 +188,14 @@ public function getId(): int return $this->id; } - public function setId(int $id): self - { - $this->id = $id; - return $this; - } - public function getEmail(): string { return $this->email; } - public function setEmail(string $email): self + public function setEmail(string $email): void { $this->email = $email; - return $this; } public function getName(): string @@ -212,10 +203,9 @@ public function getName(): string return $this->name; } - public function setName(string $name): self + public function setName(string $name): void { $this->name = $name; - return $this; } public function getSurname(): string @@ -223,10 +213,9 @@ public function getSurname(): string return $this->surname; } - public function setSurname(string $surname): self + public function setSurname(string $surname): void { $this->surname = $surname; - return $this; } public function getRole(): string @@ -234,16 +223,14 @@ public function getRole(): string return $this->role; } - public function setRole(string $role): self + public function setRole(string $role): void { $this->role = $role; - return $this; } - public function setLastLogin(\DateTime $date): self + public function setLastLogin(\DateTime $date): void { $this->lastLogin = $date; - return $this; } public function getLastLogin(): \DateTime @@ -256,10 +243,9 @@ public function getPassword(): string return $this->password; } - public function setPassword(string $password): self + public function setPassword(string $password): void { $this->password = $password; - return $this; } public function getSalt(): string @@ -267,10 +253,9 @@ public function getSalt(): string return $this->salt; } - public function setSalt(string $salt): self + public function setSalt(string $salt): void { $this->salt = $salt; - return $this; } public function getStatus(): int @@ -278,10 +263,12 @@ public function getStatus(): int return $this->status; } - public function setStatus(int $status): self + public function setStatus(int $status): void { + if (!in_array($status, [self::STATUS_NOT_CONFIRMED, self::STATUS_CONFIRMED])) { + throw new \Exception("Invalid status: $status"); + } $this->status = $status; - return $this; } public function getSetting(): Setting @@ -289,10 +276,9 @@ public function getSetting(): Setting return $this->setting; } - public function setSetting(Setting $setting): self + public function setSetting(Setting $setting): void { $this->setting = $setting; - return $this; } public function getRegistrationToken(): string diff --git a/module/Application/src/Application/Repository/ProvisionRepository.php b/module/Application/src/Application/Repository/ProvisionRepository.php index d4ff3c11..36e6f99a 100644 --- a/module/Application/src/Application/Repository/ProvisionRepository.php +++ b/module/Application/src/Application/Repository/ProvisionRepository.php @@ -4,8 +4,8 @@ namespace Application\Repository; -use Application\Entity\Provision; use Application\Entity\Movement; +use Application\Entity\Provision; use Doctrine\ORM\EntityRepository; class ProvisionRepository extends EntityRepository @@ -15,7 +15,7 @@ public function getBalance(int $userId): float $em = $this->getEntityManager(); $qb = $em ->createQueryBuilder() - ->select('COALESCE(SUM(Provision.importo), 0) AS total') + ->select('COALESCE(SUM(Provision.amount), 0) AS total') ->from(Provision::class, 'Provision') ->where('Provision.user=:userId') ->setParameter(':userId', $userId); @@ -40,11 +40,11 @@ public function search(array $params = []): array $cleanParams['userId'] = $params['userId']; } if (!empty($params['dateMin'])) { - $qb->andWhere('Provision.valuta >= :dateMin'); + $qb->andWhere('Provision.date >= :dateMin'); $cleanParams['dateMin'] = $params['dateMin']; } if (!empty($params['dateMax'])) { - $qb->andWhere('Provision.valuta <= :dateMax'); + $qb->andWhere('Provision.date <= :dateMax'); $cleanParams['dateMax'] = $params['dateMax']; } if (!empty($params['description'])) { @@ -52,7 +52,7 @@ public function search(array $params = []): array $cleanParams['description'] = '%' . $params['description'] . '%'; } - $query = $qb->setParameters($cleanParams)->orderBy('Provision.valuta', 'DESC')->getQuery(); + $query = $qb->setParameters($cleanParams)->orderBy('Provision.date', 'DESC')->getQuery(); return $query->getResult(); } @@ -66,7 +66,7 @@ public function getSum(int $userId): float { $em = $this->getEntityManager(); $qb = $em->createQueryBuilder() - ->select('COALESCE(SUM(Provision.importo), 0) AS total') + ->select('COALESCE(SUM(Provision.amount), 0) AS total') ->from(Provision::class, 'Provision') ->where('Provision.user=:userId') ->setParameter(':userId', $userId); diff --git a/module/Application/src/Application/ViewHelper/UserData.php b/module/Application/src/Application/ViewHelper/UserData.php index f9a79ef3..9c10f53b 100644 --- a/module/Application/src/Application/ViewHelper/UserData.php +++ b/module/Application/src/Application/ViewHelper/UserData.php @@ -7,25 +7,25 @@ class UserData extends AbstractHelper { /** - * @var ?\Auth\Service\UserData + * @var ?\stdClass */ - private $data; + private $identity; public function __invoke(): self { - if (!$this->data) { - $this->data = new \Auth\Service\UserData(); + if (!$this->identity) { + $this->identity = $this->view->identity(); } return $this; } public function getFullName(): ?string { - return $this->data ? $this->data->getName() . ' ' . $this->data->getSurname() : null; + return $this->identity ? $this->identity->name . ' ' . $this->identity->surname : null; } public function hasStored(): ?bool { - return $this->data ? (bool) $this->data->getSettings()['stored'] : null; + return $this->identity ? (bool) $this->identity->setting->hasProvisioning() : null; } } diff --git a/module/Application/test/Entity/ProvisionTest.php b/module/Application/test/Entity/ProvisionTest.php new file mode 100644 index 00000000..a42752b5 --- /dev/null +++ b/module/Application/test/Entity/ProvisionTest.php @@ -0,0 +1,57 @@ +setAmount($amount); + self::assertSame($amount, $provision->getAmount()); + + $date = new \DateTime(); + $provision->setDate($date); + self::assertSame($date, $provision->getDate()); + + $description = 'Description'; + $provision->setDescription($description); + self::assertSame($description, $provision->getDescription()); + + self::assertInstanceOf(InputFilterInterface::class, $provision->getInputFilter()); + + self::expectException(\Exception::class); + $provision->setInputFilter(new InputFilter()); + } + + public function testArrayExchangeAndCopy(): void + { + $provision = new Provision(); + + $amount = 23.34; + $date = '2021-04-13'; + $description = 'Description'; + $user = 1; + + $provision->exchangeArray([ + 'amount' => $amount, + 'date' => $date, + 'description' => $description, + 'user' => $user, + ]); + + $copy = $provision->getArrayCopy(); + + self::assertSame($amount, $copy['amount']); + self::assertSame($date, $copy['date']->format('Y-m-d')); + self::assertSame($description, $copy['description']); + self::assertSame($user, $copy['user']); + } +} diff --git a/module/Application/test/Entity/SettingTest.php b/module/Application/test/Entity/SettingTest.php new file mode 100644 index 00000000..6c4c3b22 --- /dev/null +++ b/module/Application/test/Entity/SettingTest.php @@ -0,0 +1,65 @@ +setMonths($months); + self::assertSame($months, $setting->getMonths()); + + $payday = 2; + $setting->setPayday($payday); + self::assertSame($payday, $setting->getPayday()); + + $provisioning = true; + $setting->setProvisioning($provisioning); + self::assertSame($provisioning, $setting->hasProvisioning()); + + self::assertInstanceOf(InputFilter::class, $setting->getInputFilter()); + + self::expectException(\Exception::class); + $setting->setInputFilter(new InputFilter()); + } + + public function setPaydayException(): void + { + $user = new User(); + $setting = new Setting($user); + + self::expectException(\Exception::class); + $setting->setPayday(29); + } + + public function testArrayExchangeAndCopy(): void + { + $user = new User(); + $setting = new Setting($user); + + $months = 54; + $payday = 12; + $provisioning = true; + + $setting->exchangeArray([ + 'months' => $months, + 'payday' => $payday, + 'provisioning' => $provisioning, + ]); + + $copy = $setting->getArrayCopy(); + + self::assertSame($months, $copy['months']); + self::assertSame($payday, $copy['payday']); + self::assertSame($provisioning, $copy['provisioning']); + } +} diff --git a/module/Application/test/Entity/UserTest.php b/module/Application/test/Entity/UserTest.php new file mode 100644 index 00000000..29e1ef14 --- /dev/null +++ b/module/Application/test/Entity/UserTest.php @@ -0,0 +1,92 @@ +setStatus($status); + self::assertSame($status, $user->getStatus()); + + $password = 'password'; + $user->setPassword($password); + self::assertSame($password, $user->getPassword()); + + $email = 'email'; + $user->setEmail($email); + self::assertSame($email, $user->getEmail()); + + $lastLogin = new \DateTime(); + $user->setLastLogin($lastLogin); + self::assertSame($lastLogin, $user->getLastLogin()); + + $name = 'name'; + $user->setName($name); + self::assertSame($name, $user->getName()); + + $role = 'role'; + $user->setRole($role); + self::assertSame($role, $user->getRole()); + + $salt = 'salt'; + $user->setSalt($salt); + self::assertSame($salt, $user->getSalt()); + + $setting = new Setting($user); + $user->setSetting($setting); + self::assertSame($setting, $user->getSetting()); + + $surname = 'surname'; + $user->setSurname($surname); + self::assertSame($surname, $user->getSurname()); + + $inputFilter = new InputFilter(); + $user->setInputFilter($inputFilter); + self::assertSame($inputFilter, $user->getInputFilter()); + } + + public function testArrayExchangeAndCopy(): void + { + $user = new User(); + + $email = 'email'; + $name = 'name'; + $password = 'password'; + $registrationToken = 'registrationToken'; + $role = 'role'; + $salt = 'salt'; + $status = User::STATUS_NOT_CONFIRMED; + $surname = 'surname'; + + $user->exchangeArray([ + 'email' => $email, + 'name' => $name, + 'password' => $password, + 'registrationToken' => $registrationToken, + 'role' => $role, + 'salt' => $salt, + 'status' => $status, + 'surname' => $surname, + ]); + + $copy = $user->getArrayCopy(); + + self::assertSame($email, $copy['email']); + self::assertSame($name, $copy['name']); + self::assertSame($password, $copy['password']); + self::assertSame($registrationToken, $copy['registrationToken']); + self::assertSame($role, $copy['role']); + self::assertSame($salt, $copy['salt']); + self::assertSame($status, $copy['status']); + self::assertSame($surname, $copy['surname']); + } +} diff --git a/module/Application/view/layout/layout.phtml b/module/Application/view/layout/layout.phtml index f9ca0e45..4c37ddde 100644 --- a/module/Application/view/layout/layout.phtml +++ b/module/Application/view/layout/layout.phtml @@ -2,7 +2,9 @@ /** * @var \Laminas\View\Renderer\PhpRenderer $this * @var string $content + * @var stdClass $identity */ +$identity = $this->identity(); ?> @@ -88,16 +90,12 @@ MoneyLog - identity(); - ?> identity()): ?>