|
| 1 | +<?php |
| 2 | + |
| 3 | +use PHPUnit\Framework\TestCase; |
| 4 | + |
| 5 | +require_once __DIR__ . '/TestHelper.php'; |
| 6 | +require_once RELATIVE_PATH . 'framework/Model.php'; |
| 7 | +require_once RELATIVE_PATH . 'framework/Bean.php'; |
| 8 | +require_once RELATIVE_PATH . 'framework/BeanAdapter.php'; |
| 9 | + |
| 10 | +class FakeBeanImplementation |
| 11 | +{ |
| 12 | + public $calls = []; |
| 13 | + |
| 14 | + public function select(...$args) |
| 15 | + { |
| 16 | + $this->calls[] = ['select', $args]; |
| 17 | + return 'select:' . implode('-', $args); |
| 18 | + } |
| 19 | + |
| 20 | + public function insert() |
| 21 | + { |
| 22 | + $this->calls[] = ['insert', []]; |
| 23 | + return 'inserted'; |
| 24 | + } |
| 25 | + |
| 26 | + public function delete(...$args) |
| 27 | + { |
| 28 | + $this->calls[] = ['delete', $args]; |
| 29 | + return 'delete:' . implode('-', $args); |
| 30 | + } |
| 31 | + |
| 32 | + public function update(...$args) |
| 33 | + { |
| 34 | + $this->calls[] = ['update', $args]; |
| 35 | + return 'update:' . implode('-', $args); |
| 36 | + } |
| 37 | + |
| 38 | + public function updateCurrent() |
| 39 | + { |
| 40 | + $this->calls[] = ['updateCurrent', []]; |
| 41 | + return 'update-current'; |
| 42 | + } |
| 43 | + |
| 44 | + public function isSqlError() |
| 45 | + { |
| 46 | + $this->calls[] = ['isSqlError', []]; |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + public function lastSqlError() |
| 51 | + { |
| 52 | + $this->calls[] = ['lastSqlError', []]; |
| 53 | + return ''; |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +final class BeanAdapterTest extends TestCase |
| 58 | +{ |
| 59 | + private function createAdapter(FakeBeanImplementation $bean): \framework\BeanAdapter |
| 60 | + { |
| 61 | + $reflection = new ReflectionClass(\framework\BeanAdapter::class); |
| 62 | + /** @var \framework\BeanAdapter $adapter */ |
| 63 | + $adapter = $reflection->newInstanceWithoutConstructor(); |
| 64 | + $property = $reflection->getProperty('bean'); |
| 65 | + $property->setAccessible(true); |
| 66 | + $property->setValue($adapter, $bean); |
| 67 | + |
| 68 | + return $adapter; |
| 69 | + } |
| 70 | + |
| 71 | + public function testAdapterDelegatesCallsToUnderlyingBean(): void |
| 72 | + { |
| 73 | + $bean = new FakeBeanImplementation(); |
| 74 | + $adapter = $this->createAdapter($bean); |
| 75 | + |
| 76 | + $this->assertSame('select:10', $adapter->select(10)); |
| 77 | + $this->assertSame('select:10-20', $adapter->select([10, 20])); |
| 78 | + $this->assertSame('inserted', $adapter->insert()); |
| 79 | + $this->assertSame('delete:5', $adapter->delete(5)); |
| 80 | + $this->assertSame('delete:7-8', $adapter->delete([7, 8])); |
| 81 | + $this->assertSame('update:3', $adapter->update(3)); |
| 82 | + $this->assertSame('update:4-5', $adapter->update([4, 5])); |
| 83 | + $this->assertSame('update-current', $adapter->updateCurrent()); |
| 84 | + $this->assertFalse($adapter->isSqlError()); |
| 85 | + $this->assertSame('', $adapter->lastSqlError()); |
| 86 | + |
| 87 | + $recorded = array_column($bean->calls, 0); |
| 88 | + $this->assertContains('select', $recorded); |
| 89 | + $this->assertContains('insert', $recorded); |
| 90 | + $this->assertContains('delete', $recorded); |
| 91 | + $this->assertContains('update', $recorded); |
| 92 | + $this->assertContains('updateCurrent', $recorded); |
| 93 | + $this->assertContains('isSqlError', $recorded); |
| 94 | + $this->assertContains('lastSqlError', $recorded); |
| 95 | + } |
| 96 | +} |
0 commit comments