From c6d827065cf8f7cd28552e642b8a46875aa4ad15 Mon Sep 17 00:00:00 2001 From: bwaidelich Date: Thu, 15 Jun 2023 10:52:25 +0200 Subject: [PATCH] BUGFIX: Disallow updating course capacity if the value is not new --- src/Model/Aggregate/CourseCapacityAggregate.php | 3 +++ src/Model/CourseCapacity.php | 5 +++++ tests/Behat/CourseConstraints.feature | 8 ++++++++ 3 files changed, 16 insertions(+) diff --git a/src/Model/Aggregate/CourseCapacityAggregate.php b/src/Model/Aggregate/CourseCapacityAggregate.php index 7c17593..8f6e876 100644 --- a/src/Model/Aggregate/CourseCapacityAggregate.php +++ b/src/Model/Aggregate/CourseCapacityAggregate.php @@ -48,6 +48,9 @@ public function apply(DomainEvent $domainEvent): void public function changeCourseCapacity(CourseCapacity $newCapacity): void { + if ($newCapacity->equals($this->state->courseCapacity)) { + throw new ConstraintException(sprintf('Failed to change capacity of course with id "%s" to %d because that is already the courses capacity', $this->courseId->value, $newCapacity->value), 1686819073); + } if ($this->state->numberOfSubscriptions > $newCapacity->value) { throw new ConstraintException(sprintf('Failed to change capacity of course with id "%s" to %d because it already has %d active subscriptions', $this->courseId->value, $newCapacity->value, $this->state->numberOfSubscriptions), 1684604361); } diff --git a/src/Model/CourseCapacity.php b/src/Model/CourseCapacity.php index d9c79ce..8fe1538 100644 --- a/src/Model/CourseCapacity.php +++ b/src/Model/CourseCapacity.php @@ -22,6 +22,11 @@ public static function fromInteger(int $value): self return new self($value); } + public function equals(self $other): bool + { + return $other->value === $this->value; + } + public function jsonSerialize(): int { return $this->value; diff --git a/tests/Behat/CourseConstraints.feature b/tests/Behat/CourseConstraints.feature index a17a1ea..19eee00 100644 --- a/tests/Behat/CourseConstraints.feature +++ b/tests/Behat/CourseConstraints.feature @@ -22,6 +22,14 @@ Feature: Failed to change capacity of course with id "non-existing" to 3 because a course with that id does not exist """ + Scenario: + Given course "c1" exists with a capacity of 3 + When course "c1" capacity is changed to 3 + Then the command should be rejected with the following message: + """ + Failed to change capacity of course with id "c1" to 3 because that is already the courses capacity + """ + Scenario: Given course "c1" exists with the title "Some Course Title" When course "c1" is renamed to "Some Course Title"