mirror of
https://github.com/php-win-ext/phpy.git
synced 2026-03-24 17:02:15 +01:00
80 lines
2.2 KiB
PHP
80 lines
2.2 KiB
PHP
<?php
|
|
|
|
|
|
use PHPUnit\Framework\TestCase;
|
|
|
|
class ObjectTest extends TestCase
|
|
{
|
|
protected function assertForKwargs($inst)
|
|
{
|
|
$today = date('Y-m-d');
|
|
$this->assertEquals(strval($inst->args['a']), 'a');
|
|
$this->assertEquals(strval($inst->args['name']), 'default_name');
|
|
$this->assertEquals(strval($inst->args['date']), $today);
|
|
}
|
|
|
|
public function testKwargs()
|
|
{
|
|
$user = PyCore::import('app.user');
|
|
$class = $user->KwargsCtor;
|
|
// __invoke
|
|
$inst = $class('a', 'b', date: date('Y-m-d'));
|
|
$this->assertForKwargs($inst);
|
|
// __call
|
|
$inst = $user->KwargsCtor('a', 'b', date: date('Y-m-d'));
|
|
$this->assertForKwargs($inst);
|
|
}
|
|
|
|
public function testGetItem()
|
|
{
|
|
$user = PyCore::import('app.user');
|
|
$kv = $user->KvReadonly('ikey', 'skey');
|
|
$this->assertEquals($kv['ikey'], 123456);
|
|
$this->assertEquals($kv['skey'], 'hello');
|
|
try {
|
|
$kv['skey'] = 'world';
|
|
} catch (\PyError $error) {
|
|
$this->assertStringContainsString('object does not support item assignment', $error->getMessage());
|
|
}
|
|
}
|
|
|
|
public function testSetItem()
|
|
{
|
|
$user = PyCore::import('app.user');
|
|
$kv = $user->Kv('ikey', 'skey');
|
|
$this->assertEquals($kv['ikey'], 123456);
|
|
$this->assertEquals($kv['skey'], 'hello');
|
|
$val = uniqid();
|
|
$kv['val'] = $val;
|
|
$this->assertEquals($kv['val'], $val);
|
|
// offsetExists always succeeds
|
|
$this->assertArrayHasKey('ikey', $kv);
|
|
$this->assertArrayHasKey('ikey2', $kv);
|
|
}
|
|
|
|
public function testCount()
|
|
{
|
|
$user = PyCore::import('app.user');
|
|
$kv = $user->KvReadonly('ikey', 'skey');
|
|
$this->assertEquals(-1, count($kv));
|
|
|
|
$kvc = $user->KvCount('ikey', 'skey');
|
|
$this->assertEquals(2, count($kvc));
|
|
}
|
|
|
|
public function testIter()
|
|
{
|
|
$arr = [1, 2, 3, 7, 8];
|
|
$fset = PyCore::frozenset($arr);
|
|
$data = iterator_to_array($fset);
|
|
$this->assertEquals($data, $arr);
|
|
}
|
|
|
|
public function testBytes2Str()
|
|
{
|
|
$bytes = random_bytes(128);
|
|
$data = PyCore::bytes($bytes);
|
|
$this->assertEquals($bytes, strval($data));
|
|
}
|
|
}
|