Added AudioAttachmentRequest & ContactAttachmentRequest & FileAttachmentRequest & LocationAttachmentRequest & ShareAttachmentRequest & StickerAttachmentRequest & VideoAttachmentRequest

This commit is contained in:
Alex
2025-07-26 09:19:22 +03:00
parent 0c1d515c72
commit e80e1176a0
45 changed files with 1714 additions and 105 deletions
+138 -2
View File
@@ -10,6 +10,7 @@ use BushlanovDev\MaxMessengerBot\Models\AbstractModel;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\Test;
use PHPUnit\Framework\TestCase;
use stdClass;
#[CoversClass(AbstractModel::class)]
#[CoversClass(ArrayOf::class)]
@@ -76,6 +77,139 @@ final class AbstractModelMappingTest extends TestCase
$this->assertNull($result->childModels);
}
#[Test]
public function itCorrectlyCastsInteger(): void
{
$result = DummyModelForMapping::fromArray(['untyped_int' => 123]);
$this->assertIsInt($result->untypedInt);
$this->assertSame(123, $result->untypedInt);
}
#[Test]
public function itCorrectlyCastsFloat(): void
{
$result = DummyModelForMapping::fromArray(['untyped_float' => 1.23]);
$this->assertIsFloat($result->untypedFloat);
$this->assertSame(1.23, $result->untypedFloat);
}
#[Test]
public function itCorrectlyCastsBoolean(): void
{
$result = DummyModelForMapping::fromArray(['untyped_bool' => true]);
$this->assertIsBool($result->untypedBool);
$this->assertTrue($result->untypedBool);
}
#[Test]
public function itReturnsValueAsIsForUnmanagedObjectType(): void
{
$externalObject = new stdClass();
$externalObject->data = 'some value';
$rawData = [
'name' => 'Test With External Object',
'external_object' => $externalObject,
];
$result = ModelWithExternalObject::fromArray($rawData);
$this->assertInstanceOf(ModelWithExternalObject::class, $result);
$this->assertSame($externalObject, $result->externalObject);
$this->assertSame('some value', $result->externalObject->data);
}
#[Test]
public function castArrayHandlesNonArrayValueForArrayProperty(): void
{
$rawData = [
'name' => 'Test with scalar instead of array',
'tags' => 'single_tag_value',
];
$result = DummyModelForArrayCast::fromArray($rawData);
$this->assertInstanceOf(DummyModelForArrayCast::class, $result);
$this->assertIsArray($result->tags);
$this->assertSame(['single_tag_value'], $result->tags);
}
#[Test]
public function castArrayReturnsArrayAsIsForUnmanagedObjectTypesInArrayOf(): void
{
$items = [
(object)['id' => 1, 'name' => 'Item A'],
(object)['id' => 2, 'name' => 'Item B'],
];
$rawData = [
'name' => 'Test with unmanaged objects',
'items' => $items,
];
$result = ModelWithUnmanagedArray::fromArray($rawData);
$this->assertInstanceOf(ModelWithUnmanagedArray::class, $result);
$this->assertIsArray($result->items);
$this->assertSame($items, $result->items);
$this->assertSame('Item A', $result->items[0]->name);
}
#[Test]
public function toArraySkipsUninitializedProperties(): void
{
$reflection = new \ReflectionClass(DummyModelForUninitializedProperty::class);
$instance = $reflection->newInstanceWithoutConstructor();
$initializedProp = $reflection->getProperty('initializedProp');
$initializedProp->setValue($instance, 'I have a value');
$resultArray = $instance->toArray();
$expectedArray = [
'initialized_prop' => 'I have a value',
];
$this->assertEquals($expectedArray, $resultArray);
$this->assertArrayNotHasKey('uninitialized_prop', $resultArray);
}
}
final readonly class DummyModelForUninitializedProperty extends AbstractModel
{
public string $initializedProp;
public int $uninitializedProp;
}
final readonly class DummyModelForArrayCast extends AbstractModel
{
public function __construct(
public ?string $name,
public ?array $tags,
) {
}
}
final readonly class ModelWithUnmanagedArray extends AbstractModel
{
public function __construct(
public ?string $name,
#[ArrayOf(stdClass::class)]
public ?array $items,
) {
}
}
final readonly class ModelWithExternalObject extends AbstractModel
{
public function __construct(
public ?string $name,
public ?stdClass $externalObject,
) {
}
}
final readonly class DummyModelForMapping extends AbstractModel
@@ -87,8 +221,10 @@ final readonly class DummyModelForMapping extends AbstractModel
#[ArrayOf(DummyChildModel::class)]
public ?array $childModels,
public ?array $untypedArray,
)
{
public ?int $untypedInt,
public ?float $untypedFloat,
public ?bool $untypedBool,
) {
}
}