This commit is contained in:
Xes
2025-08-14 22:41:49 +02:00
parent 2de81ccc46
commit 8ce45119b6
39774 changed files with 4309466 additions and 0 deletions

View File

@@ -0,0 +1,58 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Tests;
use Xabbuh\XApi\Model\Actor;
/**
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
abstract class ActorSerializerTest extends SerializerTest
{
private $actorSerializer;
protected function setUp()
{
$this->actorSerializer = $this->createActorSerializer();
}
/**
* @dataProvider serializeData
*/
public function testSerializeActor(Actor $actor, $expectedJson)
{
$this->assertJsonStringEqualsJsonString($expectedJson, $this->actorSerializer->serializeActor($actor));
}
public function serializeData()
{
return $this->buildSerializeTestCases('Actor');
}
/**
* @dataProvider deserializeData
*/
public function testDeserializeActor($json, Actor $expectedActor)
{
$actor = $this->actorSerializer->deserializeActor($json);
$this->assertInstanceOf('Xabbuh\XApi\Model\Actor', $actor);
$this->assertTrue($expectedActor->equals($actor), 'Deserialized actor has the expected properties');
}
public function deserializeData()
{
return $this->buildDeserializeTestCases('Actor');
}
abstract protected function createActorSerializer();
}

View File

@@ -0,0 +1,49 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Tests;
use Xabbuh\XApi\DataFixtures\DocumentFixtures;
use XApi\Fixtures\Json\DocumentJsonFixtures;
/**
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
abstract class DocumentDataSerializerTest extends SerializerTest
{
private $documentDataSerializer;
protected function setUp()
{
$this->documentDataSerializer = $this->createDocumentDataSerializer();
}
public function testDeserializeDocumentData()
{
$documentData = $this->documentDataSerializer->deserializeDocumentData(DocumentJsonFixtures::getDocument());
$this->assertInstanceOf('\Xabbuh\XApi\Model\DocumentData', $documentData);
$this->assertEquals('foo', $documentData['x']);
$this->assertEquals('bar', $documentData['y']);
}
public function testSerializeDocumentData()
{
$documentData = DocumentFixtures::getDocumentData();
$this->assertJsonStringEqualsJsonString(
DocumentJsonFixtures::getDocument(),
$this->documentDataSerializer->serializeDocumentData($documentData)
);
}
abstract protected function createDocumentDataSerializer();
}

View File

@@ -0,0 +1,68 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Tests;
use PHPUnit\Framework\TestCase;
abstract class SerializerTest extends TestCase
{
protected function buildSerializeTestCases($objectType)
{
$tests = array();
$phpFixturesClass = 'Xabbuh\XApi\DataFixtures\\'.$objectType.'Fixtures';
$jsonFixturesClass = 'XApi\Fixtures\Json\\'.$objectType.'JsonFixtures';
$jsonFixturesMethods = get_class_methods($jsonFixturesClass);
foreach (get_class_methods($phpFixturesClass) as $method) {
if (false !== strpos($method, 'ForQuery')) {
continue;
}
// serialized data will always contain type information
if (in_array($method.'WithType', $jsonFixturesMethods)) {
$jsonMethod = $method.'WithType';
} else {
$jsonMethod = $method;
}
$tests[$method] = array(
call_user_func(array($phpFixturesClass, $method)),
call_user_func(array($jsonFixturesClass, $jsonMethod)),
);
}
return $tests;
}
protected function buildDeserializeTestCases($objectType)
{
$tests = array();
$jsonFixturesClass = 'XApi\Fixtures\Json\\'.$objectType.'JsonFixtures';
$phpFixturesClass = 'Xabbuh\XApi\DataFixtures\\'.$objectType.'Fixtures';
foreach (get_class_methods($jsonFixturesClass) as $method) {
// PHP objects do not contain the type information as a dedicated property
if ('WithType' === substr($method, -8)) {
continue;
}
$tests[$method] = array(
call_user_func(array($jsonFixturesClass, $method)),
call_user_func(array($phpFixturesClass, $method)),
);
}
return $tests;
}
}

View File

@@ -0,0 +1,72 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Tests;
use Xabbuh\XApi\Model\StatementResult;
/**
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
abstract class StatementResultSerializerTest extends SerializerTest
{
private $statementResultSerializer;
protected function setUp()
{
$this->statementResultSerializer = $this->createStatementResultSerializer();
}
/**
* @dataProvider serializeData
*/
public function testSerializeStatementResult(StatementResult $statementResult, $expectedJson)
{
$this->assertJsonStringEqualsJsonString($expectedJson, $this->statementResultSerializer->serializeStatementResult($statementResult));
}
public function serializeData()
{
return $this->buildSerializeTestCases('StatementResult');
}
/**
* @dataProvider deserializeData
*/
public function testDeserializeStatementResult($json, StatementResult $expectedStatementResult)
{
$statementResult = $this->statementResultSerializer->deserializeStatementResult($json);
$this->assertInstanceOf('Xabbuh\XApi\Model\StatementResult', $statementResult);
$expectedStatements = $expectedStatementResult->getStatements();
$statements = $statementResult->getStatements();
$this->assertCount(count($expectedStatements), $statements, 'Statement result sets have the same size');
foreach ($expectedStatements as $key => $expectedStatement) {
$this->assertTrue($expectedStatement->equals($statements[$key]), 'Statements in result are the same');
}
if (null === $expectedStatementResult->getMoreUrlPath()) {
$this->assertNull($statementResult->getMoreUrlPath(), 'The more URL path is null');
} else {
$this->assertTrue($expectedStatementResult->getMoreUrlPath()->equals($statementResult->getMoreUrlPath()), 'More URL paths are equal');
}
}
public function deserializeData()
{
return $this->buildDeserializeTestCases('StatementResult');
}
abstract protected function createStatementResultSerializer();
}

View File

@@ -0,0 +1,110 @@
<?php
/*
* This file is part of the xAPI package.
*
* (c) Christian Flothmann <christian.flothmann@xabbuh.de>
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Xabbuh\XApi\Serializer\Tests;
use Xabbuh\XApi\DataFixtures\StatementFixtures;
use Xabbuh\XApi\Model\Statement;
use XApi\Fixtures\Json\StatementJsonFixtures;
/**
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
abstract class StatementSerializerTest extends SerializerTest
{
private $statementSerializer;
protected function setUp()
{
$this->statementSerializer = $this->createStatementSerializer();
}
/**
* @dataProvider serializeData
*/
public function testSerializeStatement(Statement $statement, $expectedJson)
{
$this->assertJsonStringEqualsJsonString($expectedJson, $this->statementSerializer->serializeStatement($statement));
}
public function serializeData()
{
$testCases = array();
foreach ($this->buildSerializeTestCases('Statement') as $fixtures) {
if ($fixtures[0] instanceof Statement) {
if ($fixtures[0]->getVerb()->isVoidVerb()) {
$fixtures[0] = StatementFixtures::getVoidingStatement(StatementFixtures::DEFAULT_STATEMENT_ID);
}
$testCases[] = $fixtures;
}
}
return $testCases;
}
/**
* @dataProvider deserializeData
*/
public function testDeserializeStatement($json, Statement $expectedStatement)
{
$attachments = array();
if (null !== $expectedStatement->getAttachments()) {
foreach ($expectedStatement->getAttachments() as $attachment) {
$attachments[$attachment->getSha2()] = array(
'type' => $attachment->getContentType(),
'content' => $attachment->getContent(),
);
}
}
$statement = $this->statementSerializer->deserializeStatement($json, $attachments);
$this->assertInstanceOf('Xabbuh\XApi\Model\Statement', $statement);
$this->assertTrue($expectedStatement->equals($statement));
}
public function deserializeData()
{
$testCases = array();
foreach ($this->buildDeserializeTestCases('Statement') as $fixtures) {
if ($fixtures[1] instanceof Statement) {
if ($fixtures[1]->getVerb()->isVoidVerb()) {
$fixtures[1] = StatementFixtures::getVoidingStatement(StatementFixtures::DEFAULT_STATEMENT_ID);
}
$testCases[] = $fixtures;
}
}
return $testCases;
}
public function testDeserializeStatementCollection()
{
/** @var \Xabbuh\XApi\Model\Statement[] $statements */
$statements = $this->statementSerializer->deserializeStatements(
StatementJsonFixtures::getStatementCollection()
);
$expectedCollection = StatementFixtures::getStatementCollection();
$this->assertSame(count($expectedCollection), count($statements));
foreach ($expectedCollection as $index => $expectedStatement) {
$this->assertTrue($expectedStatement->equals($statements[$index]));
}
}
abstract protected function createStatementSerializer();
}