Implement and test base entities
This commit is contained in:
parent
c0587ba04c
commit
0ed936dff5
@ -18,7 +18,7 @@ class ActivityPub
|
|||||||
* @param array $opts Array of options. Valid keys are
|
* @param array $opts Array of options. Valid keys are
|
||||||
* 'dbOptions', 'dbprefix', and 'isDevMode'.
|
* 'dbOptions', 'dbprefix', and 'isDevMode'.
|
||||||
*/
|
*/
|
||||||
public function __construct( $opts )
|
public function __construct( array $opts )
|
||||||
{
|
{
|
||||||
$defaults = array(
|
$defaults = array(
|
||||||
'isDevMode' => false,
|
'isDevMode' => false,
|
||||||
|
@ -14,9 +14,19 @@ namespace ActivityPub\Entities;
|
|||||||
* field is another JSON-LD object, like { "inReplyTo": { <another object } }.
|
* 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
|
* A subject can have multiple values for the same predicate - this represents a JSON-LD
|
||||||
* array.
|
* array.
|
||||||
|
*
|
||||||
|
* @Entity @Table(name="indices")
|
||||||
*/
|
*/
|
||||||
class IndexEntity
|
class IndexEntity
|
||||||
{
|
{
|
||||||
|
/**
|
||||||
|
* @Id
|
||||||
|
* @Column(type="integer")
|
||||||
|
* @GeneratedValue
|
||||||
|
* @var int
|
||||||
|
*/
|
||||||
|
protected $id;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @ManyToOne(targetEntity="ObjectEntity", inversedBy="subjectIndices")
|
* @ManyToOne(targetEntity="ObjectEntity", inversedBy="subjectIndices")
|
||||||
* @var ObjectEntity The subject of the index row
|
* @var ObjectEntity The subject of the index row
|
||||||
|
31
src/Objects/ObjectsService.php
Normal file
31
src/Objects/ObjectsService.php
Normal 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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
81
test/ObjectsServiceTest.php
Normal file
81
test/ObjectsServiceTest.php
Normal 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 );
|
||||||
|
}
|
||||||
|
}
|
||||||
|
?>
|
@ -12,6 +12,11 @@ abstract class APTestCase extends TestCase
|
|||||||
private $conn = null;
|
private $conn = null;
|
||||||
private $dbPath = '';
|
private $dbPath = '';
|
||||||
|
|
||||||
|
protected function setUp()
|
||||||
|
{
|
||||||
|
parent::setUp();
|
||||||
|
}
|
||||||
|
|
||||||
final public function getConnection() {
|
final public function getConnection() {
|
||||||
if ( $this->conn === null ) {
|
if ( $this->conn === null ) {
|
||||||
if ( self::$pdo === null ) {
|
if ( self::$pdo === null ) {
|
||||||
|
@ -2,7 +2,7 @@
|
|||||||
namespace ActivityPub\Config;
|
namespace ActivityPub\Config;
|
||||||
|
|
||||||
use PHPUnit\DbUnit\DataSet\AbstractDataSet;
|
use PHPUnit\DbUnit\DataSet\AbstractDataSet;
|
||||||
use PHPUnit\DbUnit\DataSet\DefaultTableMetaData;
|
use PHPUnit\DbUnit\DataSet\DefaultTableMetadata;
|
||||||
use PHPUnit\DbUnit\DataSet\DefaultTable;
|
use PHPUnit\DbUnit\DataSet\DefaultTable;
|
||||||
use PHPUnit\DbUnit\DataSet\DefaultTableIterator;
|
use PHPUnit\DbUnit\DataSet\DefaultTableIterator;
|
||||||
|
|
||||||
@ -24,7 +24,7 @@ class ArrayDataSet extends AbstractDataSet
|
|||||||
$columns = array_keys($rows[0]);
|
$columns = array_keys($rows[0]);
|
||||||
}
|
}
|
||||||
|
|
||||||
$metaData = new DefaultTableMetaData($tableName, $columns);
|
$metaData = new DefaultTableMetadata($tableName, $columns);
|
||||||
$table = new DefaultTable($metaData);
|
$table = new DefaultTable($metaData);
|
||||||
|
|
||||||
foreach ($rows as $row) {
|
foreach ($rows as $row) {
|
||||||
|
Loading…
Reference in New Issue
Block a user