clientMock = $this->createMock(ClientApiInterface::class); $this->modelFactoryMock = $this->createMock(ModelFactory::class); $this->api = new Api('fake-token', $this->clientMock, $this->modelFactoryMock); } #[Test] public function constructorCanCreateDefaultDependencies(): void { $api = new Api('some-token'); $reflection = new ReflectionClass($api); $clientProp = $reflection->getProperty('client'); $this->assertInstanceOf(ClientApiInterface::class, $clientProp->getValue($api)); $factoryProp = $reflection->getProperty('modelFactory'); $this->assertInstanceOf(ModelFactory::class, $factoryProp->getValue($api)); } #[Test] public function getBotInfoCallsClientAndFactoryCorrectly(): void { $rawResponseData = ['user_id' => 123, 'first_name' => 'ApiTestBot']; $botInfo = new BotInfo( 123, 'ApiTestBot', null, null, true, 0, null, null, null, null, ); $this->clientMock ->expects($this->once()) ->method('request') ->with('GET', '/me') ->willReturn($rawResponseData); $this->modelFactoryMock ->expects($this->once()) ->method('createBotInfo') ->with($rawResponseData) ->willReturn($botInfo); $result = $this->api->getBotInfo(); $this->assertSame($botInfo, $result); } #[Test] public function testSubscribeCallsClientAndFactoryCorrectly(): void { $rawResponseData = [ 'subscriptions' => [ [ 'url' => 'https://example.com/webhook', 'time' => 1678886400000, 'update_types' => ['message_created'], 'version' => '0.0.1', ], ], ]; $subscription = new Subscription( 'https://example.com/webhook', 1678886400000, [UpdateType::MessageCreated], '0.0.1', ); $this->clientMock ->expects($this->once()) ->method('request') ->with('GET', '/subscriptions') ->willReturn($rawResponseData); $this->modelFactoryMock ->expects($this->once()) ->method('createSubscriptions') ->with($rawResponseData) ->willReturn([$subscription]); $result = $this->api->getSubscriptions(); $this->assertIsArray($result); $this->assertCount(1, $result); $this->assertInstanceOf(Subscription::class, $result[0]); $this->assertSame(UpdateType::MessageCreated, $result[0]->update_types[0]); } }