homeAssistantClient = $this->createMock(HomeAssistantClient::class); $this->homeService = new HomeAssistantHomeService($this->homeAssistantClient); } public function testFindEntity(): void { // Create expected raw entity state response $rawEntityState = [ 'entity_id' => 'light.living_room', 'state' => 'on', 'attributes' => ['friendly_name' => 'Living Room Light'], 'last_changed' => '2023-03-14T12:00:00+00:00', 'last_updated' => '2023-03-14T12:00:00+00:00', ]; // Configure the mock to return our test entity state $this->homeAssistantClient->expects($this->once()) ->method('getEntityState') ->with('light.living_room') ->willReturn($rawEntityState) ; // Call the method under test $entity = $this->homeService->findEntity('light.living_room'); // Assert the result is a HomeEntityInterface $this->assertInstanceOf(HomeEntityInterface::class, $entity); $this->assertInstanceOf(HomeAssistantEntity::class, $entity); // Assert the properties match $this->assertEquals('light.living_room', $entity->getId()); $this->assertEquals('on', $entity->getState()); $this->assertEquals('Living Room Light', $entity->getName()); } public function testFindAllEntities(): void { // Create expected raw entity state response $rawEntityState = [ 'entity_id' => 'light.living_room', 'state' => 'on', 'attributes' => ['friendly_name' => 'Living Room Light'], 'last_changed' => '2023-03-14T12:00:00+00:00', 'last_updated' => '2023-03-14T12:00:00+00:00', ]; // Configure the mock to return our test entity states $this->homeAssistantClient->expects($this->once()) ->method('getStates') ->willReturn([$rawEntityState]) ; // Call the method under test $entities = $this->homeService->findAllEntities(); // Verify result is an array with one entity $this->assertIsArray($entities); $this->assertCount(1, $entities); $entity = $entities[0]; // Assert the result is a HomeEntityInterface $this->assertInstanceOf(HomeEntityInterface::class, $entity); $this->assertInstanceOf(HomeAssistantEntity::class, $entity); // Assert it returns the first entity $this->assertEquals('light.living_room', $entity->getId()); } public function testCallService(): void { // Expected result from HomeAssistant $expectedResult = ['success' => true]; // Configure the mock to return our test result $this->homeAssistantClient->expects($this->once()) ->method('callService') ->with('light', 'turn_on', ['entity_id' => 'light.living_room']) ->willReturn($expectedResult) ; // Call the method under test $result = $this->homeService->callService('light.turn_on', ['entity_id' => 'light.living_room']); // Assert the result is as expected $this->assertEquals($expectedResult, $result); } }