ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
DefinitionTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
22
23use PHPUnit\Framework\TestCase;
28use InvalidArgumentException;
29
30class DefinitionTest extends TestCase
31{
32 public function testConstruct(): void
33 {
34 $association = $this->getMockBuilder(Association::class)->disableOriginalConstructor()->getMock();
35 $instance = new Definition([$association]);
36 $this->assertInstanceOf(Definition::class, $instance);
37 }
38
39 public function testZeroAssociationsWillFail(): void
40 {
41 $this->expectException(InvalidArgumentException::class);
42 new Definition([]);
43 }
44
45
46 public function testTableName(): void
47 {
48 $tableName = 'hej';
49 $field = $this->getMockBuilder(Field::class)->disableOriginalConstructor()->getMock();
50 $field->method('tableName')->willReturn($tableName);
51
52 $association = $this->getMockBuilder(Association::class)->disableOriginalConstructor()->getMock();
53 $association->method('field')->willReturn($field);
54
55 $instance = new Definition([$association]);
56
57 $this->assertSame($tableName, $instance->tableName());
58 }
59
60 public function testAssociations(): void
61 {
62 $association = $this->getMockBuilder(Association::class)->disableOriginalConstructor()->getMock();
63 $instance = new Definition([$association]);
64
65 $this->assertEquals([$association], $instance->associations());
66 }
67
68 public function testDefaultIgnoreValues(): void
69 {
70 $association = $this->getMockBuilder(Association::class)->disableOriginalConstructor()->getMock();
71 $instance = new Definition([$association]);
72
73 $this->assertSame([], $instance->ignoreValues());
74 }
75
76 public function testIgnoreValuesWithValues(): void
77 {
78 $ignoredValues = ['lorem', 'ipsum'];
79
80 $association = $this->getMockBuilder(Association::class)->disableOriginalConstructor()->getMock();
81
82 $ignore = $this->getMockBuilder(Ignore::class)->disableOriginalConstructor()->getMock();
83 $ignore->method('values')->willReturn($ignoredValues);
84
85 $instance = new Definition([$association], $ignore);
86
87 $this->assertSame($ignoredValues, $instance->ignoreValues());
88 }
89
90 public function testReferenceTableName(): void
91 {
92 $tableName = 'hej';
93 $field = $this->getMockBuilder(Field::class)->disableOriginalConstructor()->getMock();
94 $field->method('tableName')->willReturn($tableName);
95
96 $association = $this->getMockBuilder(Association::class)->disableOriginalConstructor()->getMock();
97 $association->method('referenceField')->willReturn($field);
98
99 $instance = new Definition([$association]);
100
101 $this->assertSame($tableName, $instance->referenceTableName());
102 }
103
104 public function testInvalidName(): void
105 {
106 $association_one = new Association(
107 new Field('table_one', 'field_one'),
108 new Field('table_two', 'field_two')
109 );
110
111 $association_two = new Association(
112 new Field('table_three', 'field_one'),
113 new Field('table_four', 'field_two')
114 );
115
116 $this->expectException(InvalidArgumentException::class);
117 $this->expectExceptionMessage('All fields must have the same table');
118 $instance = new Definition([$association_one, $association_two]);
119 }
120
121
122 public function testInvalidParameter(): void
123 {
124 $association_one = new Association(
125 new Field('table_one', 'field_one'),
126 new Field('table_two', 'field_two')
127 );
128
129 $association_two = 'funny string';
130
131 $this->expectException(InvalidArgumentException::class);
132 $this->expectExceptionMessage('Associations must be of type ILIAS\components\Database\Integrity\Association.');
133 $instance = new Definition([$association_one, $association_two]);
134 }
135}