ILIAS  trunk Revision v11.0_alpha-3011-gc6b235a2e85
IdentificationFactoryTestTBD.php
Go to the documentation of this file.
1<?php
2
20
21use PHPUnit\Framework\Attributes\BackupGlobals;
22use PHPUnit\Framework\Attributes\BackupStaticProperties;
23use PHPUnit\Framework\Attributes\PreserveGlobalState;
24use PHPUnit\Framework\Attributes\RunTestsInSeparateProcesses;
32use ilPlugin;
33use InvalidArgumentException;
34use Mockery;
35use PHPUnit\Framework\TestCase;
36use ReflectionClass;
37use ReflectionMethod;
38use Serializable;
39
40require_once('./vendor/composer/vendor/autoload.php');
41
47#[BackupGlobals(false)]
48#[BackupStaticProperties(false)]
49#[PreserveGlobalState(false)]
50#[RunTestsInSeparateProcesses]
51class IdentificationFactoryTest extends TestCase
52{
53 // use MockeryPHPUnitIntegration;
54 public const MOCKED_PROVIDER_CLASSNAME = 'Mockery_1_ILIAS_GlobalScreen_Provider_Provider';
62 protected $provider_mock;
66 protected $plugin_mock;
68
69
73 protected function setUp(): void
74 {
75 parent::setUp();
76
77 $this->plugin_mock = Mockery::mock(ilPlugin::class);
78
79 $this->provider_mock = Mockery::mock(Provider::class);
80 $this->provider_mock->shouldReceive('getProviderNameForPresentation')->andReturn('Provider')->byDefault();
81
82 $this->provider_factory = Mockery::mock(ProviderFactory::class);
83 $this->provider_factory->shouldReceive('getProviderByClassName')->with(self::MOCKED_PROVIDER_CLASSNAME)->andReturn($this->provider_mock);
84 $this->provider_factory->shouldReceive('isInstanceCreationPossible')->andReturn(true);
85 $this->provider_factory->shouldReceive('isRegistered')->andReturn(true);
86
87 $this->identification = new IdentificationFactory($this->provider_factory);
88 }
89
90
91 public function testAvailableMethods(): void
92 {
93 $r = new ReflectionClass($this->identification);
94
95 $methods = [];
96 foreach ($r->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
97 $methods[] = $method->getName();
98 }
99 sort($methods);
100 $this->assertSame(
101 [
102 0 => '__construct',
103 1 => 'core',
104 2 => 'fromSerializedIdentification',
105 3 => 'plugin',
106 4 => 'tool'
107 ],
108 $methods
109 );
110 }
111
112
113 public function testCore(): void
114 {
115 $this->assertInstanceOf(IdentificationProviderInterface::class, $this->identification->core($this->provider_mock));
116 $this->assertInstanceOf(IdentificationInterface::class, $this->identification->core($this->provider_mock)->identifier('dummy'));
117 }
118
119
120 public function testPlugin(): void
121 {
122 $this->plugin_mock->shouldReceive('getId')->once()->andReturn('xdemo');
123 $identification_provider = $this->identification->plugin($this->plugin_mock->getId(), $this->provider_mock);
124 $this->assertInstanceOf(IdentificationProviderInterface::class, $identification_provider);
125 $identification = $identification_provider->identifier('dummy');
126 $this->assertInstanceOf(IdentificationInterface::class, $identification);
127 }
128
129
130 public function testSerializingCore(): void
131 {
132 $identification = $this->identification->core($this->provider_mock)->identifier('dummy');
133 $this->assertInstanceOf(Serializable::class, $identification);
134 $this->assertEquals($identification->serialize(), $this->provider_mock::class . "|dummy");
135 }
136
137
138 public function testUnserializingCore(): void
139 {
140 $identification = $this->identification->core($this->provider_mock)->identifier('dummy');
141 $serialized_identification = $identification->serialize();
142
143 $new_identification = $this->identification->fromSerializedIdentification($serialized_identification);
144 $this->assertEquals($identification, $new_identification);
145 }
146
147
148 public function testUnserializingPlugin(): void
149 {
150 $this->plugin_mock->shouldReceive('getId')->once()->andReturn('xdemo');
151 $identification = $this->identification->plugin($this->plugin_mock->getId(), $this->provider_mock)->identifier('dummy');
152 $serialized_identification = $identification->serialize();
153 $this->provider_mock->shouldReceive('getProviderNameForPresentation')->andReturn('Provider');
154 $new_identification = $this->identification->fromSerializedIdentification($serialized_identification);
155 $this->assertEquals($identification, $new_identification);
156 }
157
158
160 {
161 $this->expectException(InvalidArgumentException::class);
162 $this->expectExceptionMessage("Nobody can handle serialized identification 'ThisStringWillNobodyHandle'.");
163 $this->identification->fromSerializedIdentification("ThisStringWillNobodyHandle");
164 }
165
166
168 {
170 $internal_identifier = "internal_identifier";
171
172 $string_core = "$class_name|$internal_identifier";
173 $identification = $this->identification->fromSerializedIdentification($string_core);
174
175 $this->assertInstanceOf(CoreIdentification::class, $identification);
176 $this->assertSame($identification->getClassName(), $class_name);
177 $this->assertSame($identification->getInternalIdentifier(), $internal_identifier);
178 }
179
180
182 {
184 $internal_identifier = "internal_identifier";
185
186 // $this->markTestSkipped('I currently have absolutely no idea why this test does not work since this seems to be identical zo the test testUnserializingCore :(');
187 $string_plugin = "xdemo|$class_name|$internal_identifier";
188 $identification = $this->identification->fromSerializedIdentification($string_plugin);
189
190 $this->assertInstanceOf(PluginIdentification::class, $identification);
191 $this->assertSame($identification->getClassName(), $class_name);
192 $this->assertSame($identification->getInternalIdentifier(), $internal_identifier);
193 }
194}
Class IdentificationFactory All elements in the GlobalScreen service must be identifiable for the sup...
plugin(string $plugin_id, Provider $provider)
Returns a IdentificationProvider for ILIAS-Plugins which takes care of the plugin_id for further iden...
core(Provider $provider)
Returns a IdentificationProvider for core components, only a Provider is needed.
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...