109 lines
3.8 KiB
PHP
109 lines
3.8 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace App\Tests\Services\HomeAssistant;
|
|
|
|
use App\Core\Services\Home\HomeEntityInterface;
|
|
use App\Core\Services\Home\HomeServiceInterface;
|
|
use App\Services\HomeAssistant\HomeAssistantClient;
|
|
use App\Services\HomeAssistant\HomeAssistantEntity;
|
|
use App\Services\HomeAssistant\HomeAssistantHomeService;
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
final class HomeAssistantHomeServiceTest extends TestCase
|
|
{
|
|
private HomeAssistantClient $homeAssistantClient;
|
|
|
|
private HomeServiceInterface $homeService;
|
|
|
|
protected function setUp(): void
|
|
{
|
|
$this->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);
|
|
}
|
|
}
|