alias(static fn ($v): int => abs((int) $v)); Functions\when('wp_unslash')->returnArg(); Functions\when('sanitize_text_field')->returnArg(); Functions\when('get_current_user_id')->justReturn(5); $this->enrollments = Mockery::mock(EnrollmentRepository::class); $this->offerings = Mockery::mock(OfferingRepository::class); $this->gate = Mockery::mock(RegistrationGate::class); $this->payments = Mockery::mock(PaymentService::class); $this->endpoint = new EnrollmentEndpoint( $this->enrollments, $this->offerings, $this->gate, $this->payments, ); } private function offering(float $price): Offering { return new Offering(instructorId: 3, kind: Offering::KIND_GROUP_CLASS, title: 'Choir', price: $price, id: 8); } private function expectSuccessfulEnrollment(): void { $this->enrollments->shouldReceive('hasActiveEnrollment')->with(8, 5)->andReturn(false); $this->enrollments->shouldReceive('countActiveForOffering')->never(); $this->gate->shouldReceive('validate')->andReturn(null); $this->enrollments->shouldReceive('insert')->once()->andReturn(44); $this->gate->shouldReceive('record')->once(); } public function testEnrollInFreeClassReturnsNullPayment(): void { $this->offerings->shouldReceive('findById')->with(8)->andReturn($this->offering(0.0)); $this->expectSuccessfulEnrollment(); $this->payments->shouldNotReceive('createForRegistration'); $result = $this->endpoint->enroll(new \WP_REST_Request(['offering_id' => 8])); self::assertInstanceOf(\WP_REST_Response::class, $result); self::assertSame(201, $result->get_status()); self::assertSame(44, $result->get_data()['id']); self::assertNull($result->get_data()['payment']); } public function testEnrollInPricedClassReturnsPaymentSummary(): void { $this->offerings->shouldReceive('findById')->with(8)->andReturn($this->offering(120.0)); $this->expectSuccessfulEnrollment(); $this->payments->shouldReceive('createForRegistration') ->once() ->with(Payment::REG_ENROLLMENT, 44, 5, 3, 120.0, 'CAD', null) ->andReturn(new Payment( studentId: 5, instructorId: 3, registrationType: Payment::REG_ENROLLMENT, registrationId: 44, amount: 120.0, method: Payment::METHOD_ETRANSFER, status: Payment::STATUS_PENDING, id: 12, )); $result = $this->endpoint->enroll(new \WP_REST_Request(['offering_id' => 8])); self::assertInstanceOf(\WP_REST_Response::class, $result); self::assertSame( ['id' => 12, 'method' => Payment::METHOD_ETRANSFER, 'status' => Payment::STATUS_PENDING], $result->get_data()['payment'] ); } }