| 13 | use PHPUnit\Framework\TestCase; |
| 14 | |
| 15 | class BundleImportServiceTest extends TestCase { |
| 16 | public function testFailureRollsBackObjectsCreatedEarlierInTheBundle(): void { |
| 17 | $db = new class implements IDBConnection { |
| 18 | public array $calls = []; |
| 19 | public function beginTransaction(): void { $this->calls[] = 'begin'; } |
| 20 | public function commit(): void { $this->calls[] = 'commit'; } |
| 21 | public function rollBack(): void { $this->calls[] = 'rollback'; } |
| 22 | }; |
| 23 | $datasets = $this->createMock(DatasetService::class); |
| 24 | $datasets->expects($this->once())->method('create')->willReturn(11); |
| 25 | $storage = $this->createMock(StorageMapper::class); |
| 26 | $storage->expects($this->once())->method('create')->with(11, 'Metric', '2026', '42'); |
| 27 | $reports = $this->createMock(ReportService::class); |
| 28 | $reports->expects($this->once())->method('create')->willReturn(21); |
| 29 | $reports->method('updateOptions')->willThrowException(new \RuntimeException('Injected write failure')); |
| 30 | $service = (new \ReflectionClass(BundleImportService::class))->newInstanceWithoutConstructor(); |
| 31 | foreach (compact('db', 'datasets', 'storage', 'reports') as $name => $dependency) { |
| 32 | $property = new \ReflectionProperty(BundleImportService::class, $name); |
| 33 | $property->setAccessible(true); |
| 34 | $property->setValue($service, $dependency); |
| 35 | } |
| 36 | try { |
| 37 | $service->importBundle(['format' => 'analytics-bundle', 'version' => 1, |
| 38 | 'datasets' => [['key' => 'data', 'name' => 'Data', 'dimension1' => 'Metric', 'dimension2' => 'Year', 'value' => 'Value', 'data' => [['Metric', '2026', '42']]]], |
| 39 | 'reports' => [['key' => 'report', 'name' => 'Report', 'dataset' => 'data', 'visualization' => 'table', 'chart' => '']], |
| 40 | ]); |
| 41 | $this->fail('Expected injected failure'); |
| 42 | } catch (\RuntimeException $e) { |
| 43 | $this->assertSame('Injected write failure', $e->getMessage()); |
| 44 | } |
| 45 | $this->assertSame(['begin', 'rollback'], $db->calls); |
| 46 | } |
| 47 | } |
nothing calls this directly
no outgoing calls
no test coverage detected