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,76 @@
<?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 XApi\Repository\Api\Test\Functional;
use PHPUnit\Framework\TestCase;
use Xabbuh\XApi\Model\Activity;
use Xabbuh\XApi\Model\IRI;
use XApi\Repository\Api\ActivityRepositoryInterface;
/**
* @author Jérôme Parmentier <jerome.parmentier@acensi.fr>
*/
abstract class ActivityRepositoryTest extends TestCase
{
/**
* @var ActivityRepositoryInterface
*/
private $activityRepository;
protected function setUp()
{
$this->activityRepository = $this->createActivityRepository();
$this->cleanDatabase();
}
protected function tearDown()
{
$this->cleanDatabase();
}
/**
* @expectedException \Xabbuh\XApi\Common\Exception\NotFoundException
*/
public function testFetchingNonExistingActivityThrowsException()
{
$this->activityRepository->findActivityById(IRI::fromString('not-existing'));
}
/**
* @dataProvider getStatementsWithId
*/
public function testActivitiesCanBeRetrievedById(Activity $activity)
{
$fetchedActivity = $this->activityRepository->findActivityById($activity->getId());
$this->assertTrue($activity->equals($fetchedActivity));
}
public function getActivitiesWithId()
{
$fixtures = array();
foreach (get_class_methods('Xabbuh\XApi\DataFixtures\ActivityFixtures') as $method) {
$activity = call_user_func(array('Xabbuh\XApi\DataFixtures\ActivityFixtures', $method));
if ($activity instanceof Activity) {
$fixtures[$method] = array($activity);
}
}
return $fixtures;
}
abstract protected function createActivityRepository();
abstract protected function cleanDatabase();
}

View File

@@ -0,0 +1,206 @@
<?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 XApi\Repository\Api\Test\Functional;
use PHPUnit\Framework\TestCase;
use Xabbuh\XApi\DataFixtures\StatementFixtures;
use Xabbuh\XApi\Model\Statement;
use Xabbuh\XApi\Model\StatementId;
use XApi\Repository\Api\StatementRepository;
/**
* @author Christian Flothmann <christian.flothmann@xabbuh.de>
*/
abstract class StatementRepositoryTest extends TestCase
{
const UUID_REGEXP = '/^[a-f0-9]{8}-[a-f0-9]{4}-[1-5][a-f0-9]{3}-[89ab][a-f0-9]{3}-[a-f0-9]{12}$/i';
/**
* @var StatementRepository
*/
private $statementRepository;
protected function setUp()
{
$this->statementRepository = $this->createStatementRepository();
$this->cleanDatabase();
}
protected function tearDown()
{
$this->cleanDatabase();
}
/**
* @expectedException \Xabbuh\XApi\Common\Exception\NotFoundException
*/
public function testFetchingNonExistingStatementThrowsException()
{
$this->statementRepository->findStatementById(StatementId::fromString('12345678-1234-5678-8234-567812345678'));
}
/**
* @expectedException \Xabbuh\XApi\Common\Exception\NotFoundException
*/
public function testFetchingStatementAsVoidedStatementThrowsException()
{
$statement = StatementFixtures::getTypicalStatement()->withId(null);
$statementId = $this->statementRepository->storeStatement($statement);
$this->statementRepository->findVoidedStatementById($statementId);
}
/**
* @dataProvider getStatementsWithoutId
*/
public function testUuidIsGeneratedForNewStatementIfNotPresent(Statement $statement)
{
$statement = $statement->withId(null);
$statementId = $this->statementRepository->storeStatement($statement);
$this->assertNull($statement->getId());
$this->assertRegExp(self::UUID_REGEXP, $statementId->getValue());
}
/**
* @dataProvider getStatementsWithId
*/
public function testUuidIsNotGeneratedForNewStatementIfPresent(Statement $statement)
{
$statementId = $this->statementRepository->storeStatement($statement);
$this->assertEquals($statement->getId(), $statementId);
}
/**
* @dataProvider getStatementsWithId
*/
public function testCreatedStatementCanBeRetrievedByOriginalId(Statement $statement)
{
$this->statementRepository->storeStatement($statement);
if ($statement->getVerb()->isVoidVerb()) {
$fetchedStatement = $this->statementRepository->findVoidedStatementById($statement->getId());
} else {
$fetchedStatement = $this->statementRepository->findStatementById($statement->getId());
}
$this->assertTrue($statement->equals($fetchedStatement));
}
/**
* @dataProvider getStatementsWithoutId
*/
public function testCreatedStatementCanBeRetrievedByGeneratedId(Statement $statement)
{
$statement =$statement->withId(null);
$statementId = $this->statementRepository->storeStatement($statement);
if ($statement->getVerb()->isVoidVerb()) {
$fetchedStatement = $this->statementRepository->findVoidedStatementById($statementId);
} else {
$fetchedStatement = $this->statementRepository->findStatementById($statementId);
}
$this->assertNull($statement->getId());
$this->assertTrue($statement->equals($fetchedStatement->withId(null)));
}
public function getStatementsWithId()
{
$fixtures = array();
foreach (get_class_methods('Xabbuh\XApi\DataFixtures\StatementFixtures') as $method) {
$statement = call_user_func(array('Xabbuh\XApi\DataFixtures\StatementFixtures', $method));
if ($statement instanceof Statement) {
$fixtures[$method] = array($statement->withId(StatementId::fromString(StatementFixtures::DEFAULT_STATEMENT_ID)));
}
}
return $fixtures;
}
public function getStatementsWithoutId()
{
$fixtures = array();
foreach (get_class_methods('Xabbuh\XApi\DataFixtures\StatementFixtures') as $method) {
$statement = call_user_func(array('Xabbuh\XApi\DataFixtures\StatementFixtures', $method));
if ($statement instanceof Statement) {
$fixtures[$method] = array($statement->withId(null));
}
}
return $fixtures;
}
/**
* @expectedException \Xabbuh\XApi\Common\Exception\NotFoundException
*/
public function testFetchingNonExistingVoidStatementThrowsException()
{
$this->statementRepository->findVoidedStatementById(StatementId::fromString('12345678-1234-5678-8234-567812345678'));
}
/**
* @expectedException \Xabbuh\XApi\Common\Exception\NotFoundException
*/
public function testFetchingVoidStatementAsStatementThrowsException()
{
$statement = StatementFixtures::getVoidingStatement()->withId(null);
$statementId = $this->statementRepository->storeStatement($statement);
$this->statementRepository->findStatementById($statementId);
}
public function testUuidIsGeneratedForNewVoidStatementIfNotPresent()
{
$statement = StatementFixtures::getVoidingStatement()->withId(null);
$statementId = $this->statementRepository->storeStatement($statement);
$this->assertNull($statement->getId());
$this->assertRegExp(self::UUID_REGEXP, $statementId->getValue());
}
public function testUuidIsNotGeneratedForNewVoidStatementIfPresent()
{
$statement = StatementFixtures::getVoidingStatement();
$statementId = $this->statementRepository->storeStatement($statement);
$this->assertEquals($statement->getId(), $statementId);
}
public function testCreatedVoidStatementCanBeRetrievedByOriginalId()
{
$statement = StatementFixtures::getVoidingStatement();
$this->statementRepository->storeStatement($statement);
$fetchedStatement = $this->statementRepository->findVoidedStatementById($statement->getId());
$this->assertTrue($statement->equals($fetchedStatement));
}
public function testCreatedVoidStatementCanBeRetrievedByGeneratedId()
{
$statement = StatementFixtures::getVoidingStatement()->withId(null);
$statementId = $this->statementRepository->storeStatement($statement);
$fetchedStatement = $this->statementRepository->findVoidedStatementById($statementId);
$this->assertNull($statement->getId());
$this->assertTrue($statement->equals($fetchedStatement->withId(null)));
}
abstract protected function createStatementRepository();
abstract protected function cleanDatabase();
}