Skip to content

Commit

Permalink
use Schema\TestCase for DB setup in tests
Browse files Browse the repository at this point in the history
  • Loading branch information
PhilippGrashoff committed Sep 8, 2023
1 parent 11c0740 commit a7c1c3e
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 165 deletions.
3 changes: 1 addition & 2 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,7 @@
},
"require-dev": {
"phpunit/phpunit": "^9.5.25",
"phpstan/phpstan": "1.*",
"philippgrashoff/atkextendedtestcase": "4.*"
"phpstan/phpstan": "1.*"
},
"autoload": {
"psr-4": {
Expand Down
109 changes: 50 additions & 59 deletions tests/IntermediateModelTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

use Atk4\Data\Exception;
use Atk4\Data\Model;
use atkextendedtestcase\TestCase;
use Atk4\Data\Persistence\Sql;
use Atk4\Data\Schema\TestCase;
use PhilippR\Atk4\MToM\JunctionModel;
use PhilippR\Atk4\MToM\Tests\Testmodels\Lesson;
use PhilippR\Atk4\MToM\Tests\Testmodels\Student;
Expand All @@ -15,16 +16,20 @@

class IntermediateModelTest extends TestCase
{
protected array $sqlitePersistenceModels = [
Student::class,
StudentToLesson::class,
Lesson::class
];

protected function setUp(): void
{
parent::setUp();
$this->db = new Sql('sqlite::memory:');
$this->createMigrator(new Student($this->db))->create();
$this->createMigrator(new StudentToLesson($this->db))->create();
$this->createMigrator(new Lesson($this->db))->create();
$this->createMigrator(new Teacher($this->db))->create();
}

public function testInit(): void
{
$persistence = $this->getSqliteTestPersistence();
$studentToLesson = new StudentToLesson($persistence);
$studentToLesson = new StudentToLesson($this->db);
self::assertTrue($studentToLesson->hasField('student_id'));
self::assertTrue($studentToLesson->hasField('lesson_id'));
self::assertTrue($studentToLesson->hasReference('student_id'));
Expand All @@ -33,47 +38,46 @@ public function testInit(): void

public function testExceptionMoreThanTwoElementsInFieldNamesForReferencedClasses(): void
{
$persistence = $this->getSqliteTestPersistence();
$someClassWith3Elements = new class() extends JunctionModel {
protected array $relationFieldNames = [
'field1' => 'Blabla',
'field2' => 'DaDa',
'field3' => 'Gaga'
];
};
//TODO: assertExceptionmessage
self::expectException(Exception::class);
new $someClassWith3Elements($persistence);
new $someClassWith3Elements($this->db);
}

public function testExceptionLessThanTwoElementsInFieldNamesForReferencedClasses(): void
{
$persistence = $this->getSqliteTestPersistence();
$someClassWith1Element = new class() extends JunctionModel {
protected array $relationFieldNames = [
'field1' => 'Blabla'
];
};
//TODO: assertExceptionmessage
self::expectException(Exception::class);
$instance = new $someClassWith1Element($persistence);
$instance = new $someClassWith1Element($this->db);
}

public function testExceptionInvalidClassInFieldNamesForReferencedClasses(): void
{
$persistence = $this->getSqliteTestPersistence();
$someClassWithInvalidClassDefinition = new class() extends JunctionModel {
protected array $relationFieldNames = [
'field1' => Student::class,
'field2' => 'SomeNonExistantModel'
];
};
//TODO: assertExceptionmessage
self::expectException(Exception::class);
$instance = new $someClassWithInvalidClassDefinition($persistence);
$instance = new $someClassWithInvalidClassDefinition($this->db);
}

public function testReferencedEntitiesKeysCreatedInArray(): void
{
$persistence = $this->getSqliteTestPersistence();
$studentToLesson = new StudentToLesson($persistence);
$studentToLesson = new StudentToLesson($this->db);
$referencedEntities = (new ReflectionClass($studentToLesson))->getProperty('referencedEntities');
$referencedEntities->setAccessible(true);
$value = $referencedEntities->getValue($studentToLesson);
Expand All @@ -84,14 +88,12 @@ public function testReferencedEntitiesKeysCreatedInArray(): void

public function testAddLoadedEntity(): void
{
$persistence = $this->getSqliteTestPersistence();
$student = (new Student($persistence))->createEntity();
$student = (new Student($this->db))->createEntity();
$student->save();
$studentToLesson = new StudentToLesson($persistence);
$studentToLesson = new StudentToLesson($this->db);
$studentToLesson->addReferencedEntity($student);
$props = (new ReflectionClass($studentToLesson))->getProperty(
'referencedEntities'
);//getProperties(\ReflectionProperty::IS_PROTECTED);
$props = (new ReflectionClass($studentToLesson))->getProperty('referencedEntities');
//getProperties(\ReflectionProperty::IS_PROTECTED);
$props->setAccessible(true);
$value = $props->getValue($studentToLesson);
self::assertSame(
Expand All @@ -102,24 +104,22 @@ public function testAddLoadedEntity(): void

public function testAddReferencedEntityExceptionWrongClassPassed(): void
{
$persistence = $this->getSqliteTestPersistence();
$otherClass = new class() extends Model {
public $table = 'sometable';
};
$model = new $otherClass($persistence);
$studentToLesson = new StudentToLesson($persistence);
$model = new $otherClass($this->db);
$studentToLesson = new StudentToLesson($this->db);
self::expectException(Exception::class);
$studentToLesson->addReferencedEntity($model);
}

public function testGetReferenceEntity(): void
{
$persistence = $this->getSqliteTestPersistence();
$student = (new Student($persistence))->createEntity();
$lesson = (new Lesson($persistence))->createEntity();
$student = (new Student($this->db))->createEntity();
$lesson = (new Lesson($this->db))->createEntity();
$student->save();
$lesson->save();
$studentToLesson = (new StudentToLesson($persistence))->createEntity();
$studentToLesson = (new StudentToLesson($this->db))->createEntity();

//gets loaded from DB
$studentToLesson->set('student_id', $student->getId());
Expand All @@ -136,48 +136,42 @@ public function testGetReferenceEntity(): void

public function testGetReferencedEntityExceptionInvalidClass(): void
{
$persistence = $this->getSqliteTestPersistence();
$studentToLesson = new StudentToLesson($persistence);
$studentToLesson = new StudentToLesson($this->db);
self::expectException(Exception::class);
$resA = $studentToLesson->getReferencedEntity('SomeNonSetClass');
}

public function testgetFieldNameForModel(): void
public function testGetFieldNameForModel(): void
{
$persistence = $this->getSqliteTestPersistence();
$studentToLesson = new StudentToLesson($persistence);
self::assertSame('student_id', $studentToLesson->getFieldNameForModel(new Student($persistence)));
self::assertSame('lesson_id', $studentToLesson->getFieldNameForModel(new Lesson($persistence)));
$studentToLesson = new StudentToLesson($this->db);
self::assertSame('student_id', $studentToLesson->getFieldNameForModel(new Student($this->db)));
self::assertSame('lesson_id', $studentToLesson->getFieldNameForModel(new Lesson($this->db)));
}

public function testgetFieldNameForModelExceptionWrongClass(): void
public function testGetFieldNameForModelExceptionWrongClass(): void
{
$persistence = $this->getSqliteTestPersistence();
$studentToLesson = new StudentToLesson($persistence);
$studentToLesson = new StudentToLesson($this->db);
self::expectException(Exception::class);
$studentToLesson->getFieldNameForModel(new StudentToLesson($persistence));
$studentToLesson->getFieldNameForModel(new StudentToLesson($this->db));
}

public function testGetOtherModelClass(): void
{
$persistence = $this->getSqliteTestPersistence();
$studentToLesson = new StudentToLesson($persistence);
self::assertSame(Lesson::class, $studentToLesson->getOtherModelClass(new Student($persistence)));
self::assertSame(Student::class, $studentToLesson->getOtherModelClass(new Lesson($persistence)));
$studentToLesson = new StudentToLesson($this->db);
self::assertSame(Lesson::class, $studentToLesson->getOtherModelClass(new Student($this->db)));
self::assertSame(Student::class, $studentToLesson->getOtherModelClass(new Lesson($this->db)));
}

public function testGetOtherModelClassExceptionWrongClass(): void
{
$persistence = $this->getSqliteTestPersistence();
$studentToLesson = new StudentToLesson($persistence);
$studentToLesson = new StudentToLesson($this->db);
self::expectException(Exception::class);
$studentToLesson->getOtherModelClass(new StudentToLesson($persistence));
$studentToLesson->getOtherModelClass(new StudentToLesson($this->db));
}

public function testGetReferencedEntityExceptionInvalidArrayKeyGiven(): void
{
$persistence = $this->getSqliteTestPersistence();
$studentToLesson = (new StudentToLesson($persistence))->createEntity();
$studentToLesson = (new StudentToLesson($this->db))->createEntity();
$studentToLesson->set('student_id', 1);
$studentToLesson->set('lesson_id', 1);
$studentToLesson->save();
Expand All @@ -187,20 +181,18 @@ public function testGetReferencedEntityExceptionInvalidArrayKeyGiven(): void

public function testSavingWithoutIDsOfEntitiesSetFails(): void
{
$persistence = $this->getSqliteTestPersistence();
$studentToLesson = (new StudentToLesson($persistence))->createEntity();
$studentToLesson = (new StudentToLesson($this->db))->createEntity();
self::expectExceptionMessage('Must not be null');
$studentToLesson->save();
}

public function testAddReferencedEntityExceptionInvalidModelClassGiven(): void
{
$persistence = $this->getSqliteTestPersistence([Teacher::class]);
$studentToLesson = (new StudentToLesson($persistence))->createEntity();
$studentToLesson = (new StudentToLesson($this->db))->createEntity();
$studentToLesson->set('student_id', 1);
$studentToLesson->set('lesson_id', 1);
$studentToLesson->save();
$teacher = (new Teacher($persistence))->createEntity();
$teacher = (new Teacher($this->db))->createEntity();
$teacher->save();
self::expectExceptionMessage(
'This PhilippR\Atk4\MToM\JunctionModel does not have a reference to PhilippR\Atk4\MToM\Tests\Testmodels\Teacher'
Expand All @@ -210,17 +202,16 @@ public function testAddReferencedEntityExceptionInvalidModelClassGiven(): void

public function testAddConditionForModel(): void
{
$persistence = $this->getSqliteTestPersistence();
$lesson = (new Lesson($persistence))->createEntity();
$lesson = (new Lesson($this->db))->createEntity();
$lesson->set('id', 234);
$lesson->save();
$student = (new Student($persistence))->createEntity();
$student = (new Student($this->db))->createEntity();
$student->set('id', 456);
$student->save();

$student->addMToMRelation((new StudentToLesson($persistence)), $lesson);
$student->addMToMRelation((new StudentToLesson($this->db)), $lesson);

$studentToLesson = new StudentToLesson($persistence);
$studentToLesson = new StudentToLesson($this->db);
$studentToLesson->addConditionForModel($lesson);
$studentToLesson->addConditionForModel($student);
$studentToLesson = $studentToLesson->loadAny();
Expand Down
Loading

0 comments on commit a7c1c3e

Please sign in to comment.