szurubooru/tests/Services/HistoryServiceTest.php

235 lines
5.7 KiB
PHP
Raw Normal View History

2014-09-26 20:41:28 +02:00
<?php
namespace Szurubooru\Tests\Services;
2014-11-18 21:22:46 +01:00
use Szurubooru\Entities\Snapshot;
use Szurubooru\Dao\SnapshotDao;
use Szurubooru\Dao\TransactionManager;
use Szurubooru\Services\AuthService;
use Szurubooru\Services\HistoryService;
use Szurubooru\Services\TimeService;
use Szurubooru\Tests\AbstractTestCase;
final class HistoryServiceTest extends AbstractTestCase
2014-09-26 20:41:28 +02:00
{
private $snapshotDaoMock;
private $timeServiceMock;
private $authServiceMock;
private $transactionManagerMock;
2014-11-18 21:22:46 +01:00
public static function dataDifferenceProvider()
2014-09-26 20:41:28 +02:00
{
yield
[
2014-11-18 21:22:46 +01:00
[],
[],
['+' => [], '-' => []]
2014-09-26 20:41:28 +02:00
];
yield
[
2014-11-18 21:22:46 +01:00
['key' => 'unchangedValue'],
['key' => 'unchangedValue'],
['+' => [], '-' => []]
2014-09-26 20:41:28 +02:00
];
yield
[
['key' => 'newValue'],
[],
[
2014-11-19 10:22:59 +01:00
'+' => ['key' => 'newValue'],
2014-09-26 20:41:28 +02:00
'-' => []
]
];
yield
[
[],
['key' => 'deletedValue'],
[
'+' => [],
2014-11-19 10:22:59 +01:00
'-' => ['key' => 'deletedValue']
2014-09-26 20:41:28 +02:00
]
];
yield
[
['key' => 'changedValue'],
['key' => 'oldValue'],
[
2014-11-19 10:22:59 +01:00
'+' => ['key' => 'changedValue'],
'-' => ['key' => 'oldValue']
2014-09-26 20:41:28 +02:00
]
];
yield
[
['key' => []],
['key' => []],
[
'+' => [],
'-' => []
]
];
yield
[
['key' => ['newArrayElement']],
['key' => []],
[
2014-11-19 10:22:59 +01:00
'+' => ['key' => ['newArrayElement']],
2014-09-26 20:41:28 +02:00
'-' => []
]
];
yield
[
['key' => []],
['key' => ['removedArrayElement']],
[
'+' => [],
2014-11-19 10:22:59 +01:00
'-' => ['key' => ['removedArrayElement']]
2014-09-26 20:41:28 +02:00
]
];
yield
[
2014-11-19 10:22:59 +01:00
['key' => ['unchangedArrayElement', 'newArrayElement']],
['key' => ['unchangedArrayElement', 'oldArrayElement']],
2014-09-26 20:41:28 +02:00
[
2014-11-19 10:22:59 +01:00
'+' => ['key' => ['newArrayElement']],
'-' => ['key' => ['oldArrayElement']]
2014-09-26 20:41:28 +02:00
]
];
}
public function setUp()
{
parent::setUp();
$this->snapshotDaoMock = $this->mock(SnapshotDao::class);
2014-11-18 21:22:46 +01:00
$this->transactionManagerMock = $this->mockTransactionManager();
$this->timeServiceMock = $this->mock(TimeService::class);
$this->authServiceMock = $this->mock(AuthService::class);
2014-09-26 20:41:28 +02:00
}
/**
2014-11-18 21:22:46 +01:00
* @dataProvider dataDifferenceProvider
2014-09-26 20:41:28 +02:00
*/
2014-11-18 21:22:46 +01:00
public function testDataDifference($newData, $oldData, $expectedResult)
{
$historyService = $this->getHistoryService();
$this->assertEquals($expectedResult, $historyService->getDataDifference($newData, $oldData));
}
public static function mergingProvider()
2014-09-26 20:41:28 +02:00
{
2014-11-18 21:22:46 +01:00
{
$oldSnapshot = new Snapshot(1);
$oldSnapshot->setTime(date('c', 1));
$oldSnapshot->setOperation(Snapshot::OPERATION_CREATION);
$oldSnapshot->setData(['old' => '1']);
$newSnapshot = new Snapshot(2);
$newSnapshot->setTime(date('c', 2));
$newSnapshot->setOperation(Snapshot::OPERATION_CHANGE);
$newSnapshot->setData(['new' => '2']);
$expectedSnapshot = new Snapshot(1);
$expectedSnapshot->setTime(date('c', 3));
$expectedSnapshot->setOperation(Snapshot::OPERATION_CREATION);
$expectedSnapshot->setData(['new' => '2']);
2014-11-19 10:22:59 +01:00
$expectedSnapshot->setDataDifference(['+' => ['new' => '2'], '-' => []]);
2014-11-18 21:22:46 +01:00
yield [$oldSnapshot, $newSnapshot, $expectedSnapshot, date('c', 3)];
}
{
$oldSnapshot = new Snapshot(1);
$oldSnapshot->setOperation(Snapshot::OPERATION_CREATION);
$oldSnapshot->setData(['old' => '1']);
$newSnapshot = new Snapshot(2);
$newSnapshot->setOperation(Snapshot::OPERATION_CHANGE);
$newSnapshot->setData(['new' => '2']);
$expectedSnapshot = new Snapshot(2);
$expectedSnapshot->setTime(date('c', 3000));
$expectedSnapshot->setOperation(Snapshot::OPERATION_CHANGE);
$expectedSnapshot->setData(['new' => '2']);
2014-11-19 10:22:59 +01:00
$expectedSnapshot->setDataDifference(['+' => ['new' => '2'], '-' => ['old' => '1']]);
2014-11-18 21:22:46 +01:00
yield [$oldSnapshot, $newSnapshot, $expectedSnapshot, date('c', 3000)];
}
{
$oldSnapshot = new Snapshot(1);
$oldSnapshot->setOperation(Snapshot::OPERATION_CREATION);
$oldSnapshot->setData(['old' => '1']);
$oldSnapshot->setUserId(1);
$newSnapshot = new Snapshot(2);
$newSnapshot->setOperation(Snapshot::OPERATION_CHANGE);
$newSnapshot->setData(['new' => '2']);
$newSnapshot->setUserId(2);
$expectedSnapshot = new Snapshot(2);
$expectedSnapshot->setOperation(Snapshot::OPERATION_CHANGE);
$expectedSnapshot->setData(['new' => '2']);
2014-11-19 10:22:59 +01:00
$expectedSnapshot->setDataDifference(['+' => ['new' => '2'], '-' => ['old' => '1']]);
2014-11-18 21:22:46 +01:00
$expectedSnapshot->setUserId(null);
yield [$oldSnapshot, $newSnapshot, $expectedSnapshot, null];
}
{
$oldSnapshot = new Snapshot(1);
$oldSnapshot->setOperation(Snapshot::OPERATION_CREATION);
$oldSnapshot->setData(['old' => '1']);
$newSnapshot = new Snapshot(2);
$newSnapshot->setOperation(Snapshot::OPERATION_DELETE);
$newSnapshot->setData(['new' => '2']);
$expectedSnapshot = new Snapshot(2);
$expectedSnapshot->setOperation(Snapshot::OPERATION_DELETE);
$expectedSnapshot->setData(['new' => '2']);
2014-11-19 10:22:59 +01:00
$expectedSnapshot->setDataDifference(['+' => ['new' => '2'], '-' => ['old' => '1']]);
2014-11-18 21:22:46 +01:00
yield [$oldSnapshot, $newSnapshot, $expectedSnapshot, null];
}
}
/**
* @dataProvider mergingProvider
*/
public function testMerging($oldSnapshot, $newSnapshot, $expectedSnapshot, $currentTime)
{
$this->timeServiceMock->method('getCurrentTime')->willReturn($currentTime);
$this->snapshotDaoMock
->method('findEarlierSnapshots')
->will(
$this->onConsecutiveCalls([$oldSnapshot], null));
$this->snapshotDaoMock
->expects($this->once())
->method('save')
->will($this->returnCallback(function($param) use (&$actualSnapshot)
{
$actualSnapshot = $param;
}));
2014-09-26 20:41:28 +02:00
$historyService = $this->getHistoryService();
2014-11-18 21:22:46 +01:00
$historyService->saveSnapshot($newSnapshot);
$this->assertEntitiesEqual($expectedSnapshot, $actualSnapshot);
2014-09-26 20:41:28 +02:00
}
private function getHistoryService()
{
return new HistoryService(
2014-09-26 20:41:28 +02:00
$this->snapshotDaoMock,
$this->transactionManagerMock,
$this->timeServiceMock,
$this->authServiceMock);
}
}