diff --git a/.github/workflows/build.yaml b/.github/workflows/build.yaml index 28c7876..c709a01 100644 --- a/.github/workflows/build.yaml +++ b/.github/workflows/build.yaml @@ -14,6 +14,10 @@ jobs: include: - php: 8.0 coverage: "false" # PHPUnit 8.5.14 doesn't support code coverage under PHP 8 + - php: 8.1 + coverage: "false" # PHPUnit 8.5.14 doesn't support code coverage under PHP 8 + - php: 8.2 + coverage: "false" # PHPUnit 8.5.14 doesn't support code coverage under PHP 8 steps: - name: Checkout @@ -39,7 +43,7 @@ jobs: - name: Psalm run: | ./vendor/bin/psalm --shepherd - + - name: Coveralls if: ${{ matrix.coverage == 'true' }} env: diff --git a/src/Factory/AcsControlFactory.php b/src/Factory/AcsControlFactory.php index db98e7a..c08dfcb 100644 --- a/src/Factory/AcsControlFactory.php +++ b/src/Factory/AcsControlFactory.php @@ -56,7 +56,8 @@ public function create(array $data): AcsControlInterface [ 'title' => $resourceLinkData['title'] ?? null, 'text' => $resourceLinkData['description'] ?? null, - ]); + ] + ); $userData = $data['user'] ?? null; @@ -94,8 +95,8 @@ public function create(array $data): AcsControlInterface throw new InvalidArgumentException('Missing mandatory incident_time'); } - $extraTime = $data['extra_time'] ?? null; - $incidentSeverity = $data['incident_severity'] ?? null; + $extraTime = isset($data['extra_time']) ? (int)$data['extra_time'] : null; + $incidentSeverity = isset($data['incident_severity']) ? (float)$data['incident_severity'] : null; return new AcsControl( $resourceLink, @@ -104,8 +105,8 @@ public function create(array $data): AcsControlInterface new Carbon($incidentTime), $attemptNumber, $issuerIdentifier, - $extraTime ? intval($extraTime) : null, - $incidentSeverity ? floatval($incidentSeverity) : null, + $extraTime, + $incidentSeverity, $data['reason_code'] ?? null, $data['reason_msg'] ?? null ); diff --git a/src/Factory/AcsControlResultFactory.php b/src/Factory/AcsControlResultFactory.php index 6169243..e5270fe 100644 --- a/src/Factory/AcsControlResultFactory.php +++ b/src/Factory/AcsControlResultFactory.php @@ -40,8 +40,8 @@ public function create(array $data): AcsControlResultInterface throw new LtiException('Cannot create ACS control result: Invalid status'); } - $extraTime = $data['extra_time'] ?? null; + $extraTime = isset($data['extra_time']) ? (int)$data['extra_time'] : null; - return new AcsControlResult($status, $extraTime ? intval($extraTime) : null); + return new AcsControlResult($status, $extraTime); } } diff --git a/src/Model/AcsControl.php b/src/Model/AcsControl.php index 1a636d4..6ba7c44 100644 --- a/src/Model/AcsControl.php +++ b/src/Model/AcsControl.php @@ -237,7 +237,10 @@ public function jsonSerialize(): array 'incident_severity' => $this->incidentSeverity, 'reason_code' => $this->reasonCode, 'reason_msg' => $this->reasonMessage, - ] + ], + static function ($element) { + return $element !== null; + } ); } } diff --git a/src/Model/AcsControlResult.php b/src/Model/AcsControlResult.php index d7b569b..e861128 100644 --- a/src/Model/AcsControlResult.php +++ b/src/Model/AcsControlResult.php @@ -79,7 +79,10 @@ public function jsonSerialize(): array [ 'status' => $this->status, 'extra_time' => $this->extraTime, - ] + ], + static function($element) { + return $element !== null; + } ); } } diff --git a/tests/Unit/Factory/AcsControlFactoryTest.php b/tests/Unit/Factory/AcsControlFactoryTest.php index fa6dd67..e651430 100644 --- a/tests/Unit/Factory/AcsControlFactoryTest.php +++ b/tests/Unit/Factory/AcsControlFactoryTest.php @@ -80,6 +80,86 @@ public function testCreateSuccess(): void $this->assertEquals('message', $result->getReasonMessage()); } + public function testCreateSuccessWithZeroExtraTimeAndIncidentSeverity(): void + { + $date = Carbon::now(); + + $result = $this->subject->create( + [ + 'user' => [ + 'iss' => 'issuerIdentifier', + 'sub' => 'userIdentifier', + ], + 'resource_link' => [ + 'id' => 'resourceLinkIdentifier', + 'title' => 'resourceLinkTitle', + 'description' => 'resourceLinkDescription', + ], + 'attempt_number' => 1, + 'action' => AcsControlInterface::ACTION_UPDATE, + 'extra_time' => 0, + 'incident_time' => $date->format(DATE_ATOM), + 'incident_severity' => 0, + 'reason_code' => 'code', + 'reason_msg' => 'message', + ] + ); + + $this->assertInstanceOf(AcsControlInterface::class, $result); + + $this->assertEquals('resourceLinkIdentifier', $result->getResourceLink()->getIdentifier()); + $this->assertEquals('resourceLinkTitle', $result->getResourceLink()->getTitle()); + $this->assertEquals('resourceLinkDescription', $result->getResourceLink()->getText()); + $this->assertEquals('issuerIdentifier', $result->getIssuerIdentifier()); + $this->assertEquals('userIdentifier', $result->getUserIdentifier()); + $this->assertEquals(1, $result->getAttemptNumber()); + $this->assertEquals(AcsControlInterface::ACTION_UPDATE, $result->getAction()); + $this->assertEquals(0, $result->getExtraTime()); + $this->assertEquals($date->format(DATE_ATOM), $result->getIncidentTime()->format(DATE_ATOM)); + $this->assertEquals(0, $result->getIncidentSeverity()); + $this->assertEquals('code', $result->getReasonCode()); + $this->assertEquals('message', $result->getReasonMessage()); + } + + public function testCreateSuccessWithoutExtraTimeAndIncidentSeverity(): void + { + $date = Carbon::now(); + + $result = $this->subject->create( + [ + 'user' => [ + 'iss' => 'issuerIdentifier', + 'sub' => 'userIdentifier', + ], + 'resource_link' => [ + 'id' => 'resourceLinkIdentifier', + 'title' => 'resourceLinkTitle', + 'description' => 'resourceLinkDescription', + ], + 'attempt_number' => 1, + 'action' => AcsControlInterface::ACTION_UPDATE, + 'incident_time' => $date->format(DATE_ATOM), + 'reason_code' => 'code', + 'reason_msg' => 'message', + ] + ); + + $this->assertInstanceOf(AcsControlInterface::class, $result); + + $this->assertEquals('resourceLinkIdentifier', $result->getResourceLink()->getIdentifier()); + $this->assertEquals('resourceLinkTitle', $result->getResourceLink()->getTitle()); + $this->assertEquals('resourceLinkDescription', $result->getResourceLink()->getText()); + $this->assertEquals('issuerIdentifier', $result->getIssuerIdentifier()); + $this->assertEquals('userIdentifier', $result->getUserIdentifier()); + $this->assertEquals(1, $result->getAttemptNumber()); + $this->assertEquals(AcsControlInterface::ACTION_UPDATE, $result->getAction()); + $this->assertEquals(null, $result->getExtraTime()); + $this->assertEquals($date->format(DATE_ATOM), $result->getIncidentTime()->format(DATE_ATOM)); + $this->assertEquals(null, $result->getIncidentSeverity()); + $this->assertEquals('code', $result->getReasonCode()); + $this->assertEquals('message', $result->getReasonMessage()); + } + /** * @dataProvider provideFailingData */