|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Atk4\Data\Tests; |
| 6 | + |
| 7 | +use Atk4\Data\Exception; |
| 8 | +use Atk4\Data\Model; |
| 9 | +use Atk4\Data\Schema\TestCase; |
| 10 | +use Atk4\Data\Types\LocalObjectHandle; |
| 11 | +use Atk4\Data\Types\LocalObjectType; |
| 12 | +use Doctrine\DBAL\Types as DbalTypes; |
| 13 | + |
| 14 | +class LocalObjectTest extends TestCase |
| 15 | +{ |
| 16 | + /** |
| 17 | + * @return \WeakMap<object, LocalObjectHandle> |
| 18 | + */ |
| 19 | + protected function getLocalObjectHandles(LocalObjectType $type = null): \WeakMap |
| 20 | + { |
| 21 | + if ($type === null) { |
| 22 | + /** @var LocalObjectType */ |
| 23 | + $type = DbalTypes\Type::getType('atk4_local_object'); |
| 24 | + } |
| 25 | + |
| 26 | + $platform = $this->getDatabasePlatform(); |
| 27 | + |
| 28 | + return \Closure::bind(function () use ($type, $platform) { |
| 29 | + // make sure handles are initialized |
| 30 | + $type->convertToDatabaseValue(new \stdClass(), $platform); |
| 31 | + |
| 32 | + TestCase::assertSame(count($type->handles), count($type->handlesIndex)); |
| 33 | + |
| 34 | + return $type->handles; |
| 35 | + }, null, LocalObjectType::class)(); |
| 36 | + } |
| 37 | + |
| 38 | + protected function setUp(): void |
| 39 | + { |
| 40 | + parent::setUp(); |
| 41 | + |
| 42 | + static::assertCount(0, $this->getLocalObjectHandles()); |
| 43 | + } |
| 44 | + |
| 45 | + protected function tearDown(): void |
| 46 | + { |
| 47 | + static::assertCount(0, $this->getLocalObjectHandles()); |
| 48 | + |
| 49 | + parent::tearDown(); |
| 50 | + } |
| 51 | + |
| 52 | + public function testTypeBasic(): void |
| 53 | + { |
| 54 | + $t1 = new LocalObjectType(); |
| 55 | + $t2 = new LocalObjectType(); |
| 56 | + $platform = $this->getDatabasePlatform(); |
| 57 | + |
| 58 | + $obj1 = new \stdClass(); |
| 59 | + $obj2 = new \stdClass(); |
| 60 | + |
| 61 | + $v1 = $t1->convertToDatabaseValue($obj1, $platform); |
| 62 | + $v2 = $t1->convertToDatabaseValue($obj2, $platform); |
| 63 | + $v3 = $t2->convertToDatabaseValue($obj1, $platform); |
| 64 | + static::assertMatchesRegularExpression('~^\w+-\w+$~', $v1); |
| 65 | + static::assertNotSame($v1, $v2); |
| 66 | + static::assertNotSame($v1, $v3); |
| 67 | + |
| 68 | + static::assertSame($obj1, $t1->convertToPHPValue($v1, $platform)); |
| 69 | + static::assertSame($obj2, $t1->convertToPHPValue($v2, $platform)); |
| 70 | + static::assertSame($obj1, $t2->convertToPHPValue($v3, $platform)); |
| 71 | + |
| 72 | + static::assertSame($v1, $t1->convertToDatabaseValue($obj1, $platform)); |
| 73 | + static::assertSame($obj1, $t1->convertToPHPValue($v1, $platform)); |
| 74 | + |
| 75 | + static::assertCount(2, $this->getLocalObjectHandles($t1)); |
| 76 | + static::assertCount(1, $this->getLocalObjectHandles($t2)); |
| 77 | + $obj1WeakRef = \WeakReference::create($obj1); |
| 78 | + static::assertSame($obj1, $obj1WeakRef->get()); |
| 79 | + unset($obj1); |
| 80 | + static::assertCount(1, $this->getLocalObjectHandles($t1)); |
| 81 | + static::assertCount(0, $this->getLocalObjectHandles($t2)); |
| 82 | + static::assertNull($obj1WeakRef->get()); |
| 83 | + unset($obj2); |
| 84 | + static::assertCount(0, $this->getLocalObjectHandles($t1)); |
| 85 | + } |
| 86 | + |
| 87 | + public function testTypeCloneException(): void |
| 88 | + { |
| 89 | + $t = new LocalObjectType(); |
| 90 | + |
| 91 | + $this->expectException(\Error::class); |
| 92 | + clone $t; |
| 93 | + } |
| 94 | + |
| 95 | + public function testTypeDifferentInstanceException(): void |
| 96 | + { |
| 97 | + $t1 = new LocalObjectType(); |
| 98 | + $t2 = new LocalObjectType(); |
| 99 | + $platform = $this->getDatabasePlatform(); |
| 100 | + |
| 101 | + $obj = new \stdClass(); |
| 102 | + $v = $t1->convertToDatabaseValue($obj, $platform); |
| 103 | + |
| 104 | + $t1->convertToPHPValue($v, $platform); |
| 105 | + |
| 106 | + $this->expectException(Exception::class); |
| 107 | + $t2->convertToPHPValue($v, $platform); |
| 108 | + } |
| 109 | + |
| 110 | + public function testTypeReleasedException(): void |
| 111 | + { |
| 112 | + $t = new LocalObjectType(); |
| 113 | + $platform = $this->getDatabasePlatform(); |
| 114 | + |
| 115 | + $obj = new \stdClass(); |
| 116 | + $v = $t->convertToDatabaseValue($obj, $platform); |
| 117 | + |
| 118 | + $t->convertToPHPValue($v, $platform); |
| 119 | + |
| 120 | + unset($obj); |
| 121 | + if (\PHP_MAJOR_VERSION < 8) { // force WeakMap polyfill housekeeping |
| 122 | + $this->getLocalObjectHandles($t); |
| 123 | + } |
| 124 | + |
| 125 | + $this->expectException(Exception::class); |
| 126 | + $t->convertToPHPValue($v, $platform); |
| 127 | + } |
| 128 | + |
| 129 | + public function testEntityKeepsReference(): void |
| 130 | + { |
| 131 | + $model = new Model($this->db, ['table' => 't']); |
| 132 | + $model->addField('v', ['type' => 'atk4_local_object']); |
| 133 | + $this->createMigrator($model)->create(); |
| 134 | + |
| 135 | + $entity = $model->createEntity(); |
| 136 | + $obj = new \stdClass(); |
| 137 | + $objWeakRef = \WeakReference::create($obj); |
| 138 | + $entity->set('v', $obj); |
| 139 | + unset($obj); |
| 140 | + static::assertNotNull($objWeakRef->get()); |
| 141 | + static::assertSame($objWeakRef->get(), $entity->get('v')); |
| 142 | + |
| 143 | + $entity->save(); |
| 144 | + static::assertNotNull($objWeakRef->get()); |
| 145 | + static::assertSame($objWeakRef->get(), $entity->get('v')); |
| 146 | + |
| 147 | + $entity->reload(); |
| 148 | + static::assertNotNull($objWeakRef->get()); |
| 149 | + static::assertSame($objWeakRef->get(), $entity->get('v')); |
| 150 | + |
| 151 | + $entity2 = $model->load($entity->getId()); |
| 152 | + $entity->unload(); |
| 153 | + static::assertNotNull($objWeakRef->get()); |
| 154 | + static::assertNull($entity->get('v')); |
| 155 | + static::assertSame($objWeakRef->get(), $entity2->get('v')); |
| 156 | + |
| 157 | + $entity2->unload(); |
| 158 | + static::assertNull($objWeakRef->get()); |
| 159 | + static::assertNull($entity2->get('v')); |
| 160 | + } |
| 161 | +} |
0 commit comments