ILIAS  release_8 Revision v8.19
All Data Structures Namespaces Files Functions Variables Modules Pages
IdentificationFactoryTest.php
Go to the documentation of this file.
1 <?php
2 
4 
12 use ilPlugin;
14 use Mockery;
17 use ReflectionClass;
19 use Serializable;
20 
21 require_once('./libs/composer/vendor/autoload.php');
22 
33 class IdentificationFactoryTest extends TestCase
34 {
36  public const MOCKED_PROVIDER_CLASSNAME = 'Mockery_1_ILIAS_GlobalScreen_Provider_Provider';
40  protected $provider_factory;
44  protected $provider_mock;
48  protected $plugin_mock;
50 
51 
55  protected function setUp(): void
56  {
57  parent::setUp();
58 
59  $this->plugin_mock = Mockery::mock(ilPlugin::class);
60 
61  $this->provider_mock = Mockery::mock(Provider::class);
62  $this->provider_mock->shouldReceive('getProviderNameForPresentation')->andReturn('Provider')->byDefault();
63 
64  $this->provider_factory = Mockery::mock(ProviderFactory::class);
65  $this->provider_factory->shouldReceive('getProviderByClassName')->with(self::MOCKED_PROVIDER_CLASSNAME)->andReturn($this->provider_mock);
66  $this->provider_factory->shouldReceive('isInstanceCreationPossible')->andReturn(true);
67  $this->provider_factory->shouldReceive('isRegistered')->andReturn(true);
68 
69  $this->identification = new IdentificationFactory($this->provider_factory);
70  }
71 
72 
73  public function testAvailableMethods(): void
74  {
75  $r = new ReflectionClass($this->identification);
76 
77  $methods = [];
78  foreach ($r->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
79  $methods[] = $method->getName();
80  }
81  sort($methods);
82  $this->assertEquals(
83  [
84  0 => '__construct',
85  1 => 'core',
86  2 => 'fromSerializedIdentification',
87  3 => 'plugin',
88  4 => 'tool'
89  ],
90  $methods
91  );
92  }
93 
94 
95  public function testCore(): void
96  {
97  $this->assertInstanceOf(IdentificationProviderInterface::class, $this->identification->core($this->provider_mock));
98  $this->assertInstanceOf(IdentificationInterface::class, $this->identification->core($this->provider_mock)->identifier('dummy'));
99  }
100 
101 
102  public function testPlugin(): void
103  {
104  $this->plugin_mock->shouldReceive('getId')->once()->andReturn('xdemo');
105  $identification_provider = $this->identification->plugin($this->plugin_mock->getId(), $this->provider_mock);
106  $this->assertInstanceOf(IdentificationProviderInterface::class, $identification_provider);
107  $identification = $identification_provider->identifier('dummy');
108  $this->assertInstanceOf(IdentificationInterface::class, $identification);
109  }
110 
111 
112  public function testSerializingCore(): void
113  {
114  $identification = $this->identification->core($this->provider_mock)->identifier('dummy');
115  $this->assertInstanceOf(Serializable::class, $identification);
116  $this->assertEquals($identification->serialize(), get_class($this->provider_mock) . "|dummy");
117  }
118 
119 
120  public function testUnserializingCore(): void
121  {
122  $identification = $this->identification->core($this->provider_mock)->identifier('dummy');
123  $serialized_identification = $identification->serialize();
124 
125  $new_identification = $this->identification->fromSerializedIdentification($serialized_identification);
126  $this->assertEquals($identification, $new_identification);
127  }
128 
129 
130  public function testUnserializingPlugin(): void
131  {
132  $this->plugin_mock->shouldReceive('getId')->once()->andReturn('xdemo');
133  $identification = $this->identification->plugin($this->plugin_mock->getId(), $this->provider_mock)->identifier('dummy');
134  $serialized_identification = $identification->serialize();
135  $this->provider_mock->shouldReceive('getProviderNameForPresentation')->andReturn('Provider');
136  $new_identification = $this->identification->fromSerializedIdentification($serialized_identification);
137  $this->assertEquals($identification, $new_identification);
138  }
139 
140 
142  {
143  $this->expectException(InvalidArgumentException::class);
144  $this->expectExceptionMessage("Nobody can handle serialized identification 'ThisStringWillNobodyHandle'.");
145  $this->identification->fromSerializedIdentification("ThisStringWillNobodyHandle");
146  }
147 
148 
149  public function testFactoryMustReturnCorrectTypeCore(): void
150  {
151  $class_name = self::MOCKED_PROVIDER_CLASSNAME;
152  $internal_identifier = "internal_identifier";
153 
154  $string_core = "$class_name|$internal_identifier";
155  $identification = $this->identification->fromSerializedIdentification($string_core);
156 
157  $this->assertInstanceOf(CoreIdentification::class, $identification);
158  $this->assertEquals($identification->getClassName(), $class_name);
159  $this->assertEquals($identification->getInternalIdentifier(), $internal_identifier);
160  }
161 
162 
164  {
165  $class_name = self::MOCKED_PROVIDER_CLASSNAME;
166  $internal_identifier = "internal_identifier";
167 
168  // $this->markTestSkipped('I currently have absolutely no idea why this test does not work since this seems to be identical zo the test testUnserializingCore :(');
169  $string_plugin = "xdemo|$class_name|$internal_identifier";
170  $identification = $this->identification->fromSerializedIdentification($string_plugin);
171 
172  $this->assertInstanceOf(PluginIdentification::class, $identification);
173  $this->assertEquals($identification->getClassName(), $class_name);
174  $this->assertEquals($identification->getInternalIdentifier(), $internal_identifier);
175  }
176 }
plugin(string $plugin_id, Provider $provider)
Returns a IdentificationProvider for ILIAS-Plugins which takes care of the plugin_id for further iden...
This file is part of ILIAS, a powerful learning management system published by ILIAS open source e-Le...
Class IdentificationFactory All elements in the GlobalScreen service must be identifiable for the sup...
core(Provider $provider)
Returns a IdentificationProvider for core components, only a Provider is needed.