Implement and test base entities

This commit is contained in:
Jeremy Dormitzer 2018-11-20 22:34:39 -05:00
parent c0587ba04c
commit 0ed936dff5
6 changed files with 130 additions and 3 deletions

View File

@ -18,7 +18,7 @@ class ActivityPub
* @param array $opts Array of options. Valid keys are
* 'dbOptions', 'dbprefix', and 'isDevMode'.
*/
public function __construct( $opts )
public function __construct( array $opts )
{
$defaults = array(
'isDevMode' => false,

View File

@ -14,9 +14,19 @@ namespace ActivityPub\Entities;
* field is another JSON-LD object, like { "inReplyTo": { <another object } }.
* A subject can have multiple values for the same predicate - this represents a JSON-LD
* array.
*
* @Entity @Table(name="indices")
*/
class IndexEntity
{
/**
* @Id
* @Column(type="integer")
* @GeneratedValue
* @var int
*/
protected $id;
/**
* @ManyToOne(targetEntity="ObjectEntity", inversedBy="subjectIndices")
* @var ObjectEntity The subject of the index row

View File

@ -0,0 +1,31 @@
<?php
namespace ActivityPub\Objects;
use ActivityPub\Entities\ObjectEntity;
use ActivityPub\Entities\IndexEntity;
use Doctrine\ORM\EntityManager;
class ObjectsService
{
protected $entityManager;
public function __construct( EntityManager $entityManager)
{
$this->entityManager = $entityManager;
}
public function createObject( array $fields )
{
// TODO validate object fields?
$object = new ObjectEntity();
$this->entityManager->persist( $object );
// TODO handle objects as well as values
foreach ( $fields as $field => $value ) {
$index = IndexEntity::withValue( $object, $field, $value );
$this->entityManager->persist( $index );
}
$this->entityManager->flush();
return $object;
}
}
?>

View File

@ -0,0 +1,81 @@
<?php
require_once dirname( __FILE__ ) . '/config/APTestCase.php';
require_once dirname( __FILE__ ) . '/config/ArrayDataSet.php';
require_once dirname( __FILE__ ) . '/../src/Objects/ObjectsService.php';
use ActivityPub\Config\APTestCase;
use ActivityPub\Config\ArrayDataSet;
use Doctrine\ORM\EntityManager;
use Doctrine\ORM\Tools\Setup;
use ActivityPub\Objects\ObjectsService;
use ActivityPub\Database\PrefixNamingStrategy;
use PHPUnit\DbUnit\TestCaseTrait;
class ObjectsServiceTest extends APTestCase
{
use TestCaseTrait;
protected $entityManager;
protected $objectsService;
protected function getDataSet()
{
return new ArrayDataSet( array(
'objects' => array(
array( 'id' => 1 )
),
'indices' => array(
array(
'subject' => 1,
'predicate' => 'id',
'value' => 'https://example.com/notes/1',
'object' => null,
),
array(
'subject' => 1,
'predicate' => 'type',
'value' => 'Note',
'object' => null,
),
array(
'subject' => 1,
'predicate' => 'content',
'value' => 'This is a note',
'object' => null,
),
),
) );
}
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' => __DIR__ . '/db.sqlite',
);
$this->entityManager = EntityManager::create( $dbParams, $dbConfig );
$this->objectsService = new ObjectsService( $this->entityManager );
}
public function testItCreatesObject()
{
$fields = array(
'id' => 'https://example.com/notes/1',
'type' => 'Note',
'content' => 'This is a note',
);
$objectEntity = $this->objectsService->createObject( $fields );
$queryTable = $this->getConnection()->createQueryTable(
'objects', 'SELECT * FROM objects'
);
$expectedTable = $this->getDataSet()->getTable('objects');
$this->assertTablesEqual( $expectedTable, $queryTable );
}
}
?>

View File

@ -12,6 +12,11 @@ abstract class APTestCase extends TestCase
private $conn = null;
private $dbPath = '';
protected function setUp()
{
parent::setUp();
}
final public function getConnection() {
if ( $this->conn === null ) {
if ( self::$pdo === null ) {

View File

@ -2,7 +2,7 @@
namespace ActivityPub\Config;
use PHPUnit\DbUnit\DataSet\AbstractDataSet;
use PHPUnit\DbUnit\DataSet\DefaultTableMetaData;
use PHPUnit\DbUnit\DataSet\DefaultTableMetadata;
use PHPUnit\DbUnit\DataSet\DefaultTable;
use PHPUnit\DbUnit\DataSet\DefaultTableIterator;
@ -24,7 +24,7 @@ class ArrayDataSet extends AbstractDataSet
$columns = array_keys($rows[0]);
}
$metaData = new DefaultTableMetaData($tableName, $columns);
$metaData = new DefaultTableMetadata($tableName, $columns);
$table = new DefaultTable($metaData);
foreach ($rows as $row) {