activitypub-php/test/Entities/EntityTest.php

122 lines
4.3 KiB
PHP
Raw Normal View History

2019-02-14 03:27:47 +00:00
<?php /** @noinspection PhpUnhandledExceptionInspection */
2019-01-08 15:14:42 +00:00
namespace ActivityPub\Test\Entities;
use ActivityPub\Database\PrefixNamingStrategy;
2019-02-16 17:51:24 +00:00
use ActivityPub\Entities\ActivityPubObject;
use ActivityPub\Test\TestConfig\ArrayDataSet;
use ActivityPub\Test\TestConfig\SQLiteTestCase;
use ActivityPub\Test\TestUtils\TestDateTimeProvider;
2019-02-16 17:51:24 +00:00
use ActivityPub\Utils\DateTimeProvider;
use DateTime;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Setup;
class EntityTest extends SQLiteTestCase
{
2019-02-14 03:27:47 +00:00
/**
* @var EntityManager
*/
protected $entityManager;
2019-02-14 03:27:47 +00:00
/**
* @var DateTimeProvider
*/
protected $dateTimeProvider;
public function testItCreatesAnObjectWithAPrivateKey()
{
$object = new ActivityPubObject( $this->dateTimeProvider->getTime( 'objects-service.create' ) );
$privateKey = 'a private key';
$object->setPrivateKey( $privateKey );
$this->entityManager->persist( $object );
$this->entityManager->flush();
$now = $this->getTime( 'objects-service.create' );
$expected = new ArrayDataSet( array(
'objects' => array(
array( 'id' => 1, 'created' => $now, 'lastUpdated' => $now ),
),
'keys' => array(
array( 'id' => 1, 'object_id' => 1, 'key' => $privateKey )
),
) );
$expectedObjectsTable = $expected->getTable( 'objects' );
$expectedKeysTable = $expected->getTable( 'keys' );
$objectsQueryTable = $this->getConnection()->createQueryTable(
'objects', 'SELECT * FROM objects'
);
$keysQueryTable = $this->getConnection()->createQueryTable(
'keys', 'SELECT * FROM keys'
);
$this->assertTablesEqual( $expectedObjectsTable, $objectsQueryTable );
$this->assertTablesEqual( $expectedKeysTable, $keysQueryTable );
}
2019-02-16 17:51:24 +00:00
private function getTime( $context )
{
return $this->dateTimeProvider
->getTime( $context )
->format( "Y-m-d H:i:s" );
}
public function itUpdatesAPrivateKey()
{
$object = new ActivityPubObject( $this->dateTimeProvider->getTime( 'objects-service.create' ) );
$privateKey = 'a private key';
$object->setPrivateKey( $privateKey );
$this->entityManager->persist( $object );
$this->entityManager->flush();
$newPrivateKey = 'a new private key';
$object->setPrivateKey( $newPrivateKey );
2019-02-14 03:27:47 +00:00
$this->entityManager->persist( $object );
$this->entityManager->flush();
$now = $this->getTime( 'objects-service.create' );
$expected = new ArrayDataSet( array(
'objects' => array(
array( 'id' => 1, 'created' => $now, 'lastUpdated' => $now ),
),
'keys' => array(
array( 'id' => 1, 'object_id' => 1, 'key' => $newPrivateKey )
),
) );
$expectedObjectsTable = $expected->getTable( 'objects' );
$expectedKeysTable = $expected->getTable( 'keys' );
$objectsQueryTable = $this->getConnection()->createQueryTable(
'objects', 'SELECT * FROM objects'
);
$keysQueryTable = $this->getConnection()->createQueryTable(
'keys', 'SELECT * FROM keys'
);
$this->assertTablesEqual( $expectedObjectsTable, $objectsQueryTable );
$this->assertTablesEqual( $expectedKeysTable, $keysQueryTable );
}
2019-02-16 17:51:24 +00:00
protected function getDataSet()
{
return new ArrayDataSet( array(
'objects' => array(),
'fields' => array(),
'keys' => array(),
) );
}
protected function setUp()
{
parent::setUp();
$dbConfig = Setup::createAnnotationMetadataConfiguration(
array( __DIR__ . '/../../src/Entities' ), true
);
$namingStrategy = new PrefixNamingStrategy( '' );
$dbConfig->setNamingStrategy( $namingStrategy );
$dbParams = array(
'driver' => 'pdo_sqlite',
'path' => $this->getDbPath(),
);
$this->entityManager = EntityManager::create( $dbParams, $dbConfig );
$this->dateTimeProvider = new TestDateTimeProvider( array(
'objects-service.create' => new DateTime( "12:00" ),
'objects-service.update' => new DateTime( "12:01" ),
) );
}
}