ILIAS  release_7 Revision v7.30-3-g800a261c036
All Data Structures Namespaces Files Functions Variables Modules Pages
IdentificationFactoryTest.php
Go to the documentation of this file.
1 <?php
2 
20 
28 use ilPlugin;
30 use Mockery;
33 use ReflectionClass;
35 use Serializable;
36 
37 require_once('./libs/composer/vendor/autoload.php');
38 
50 {
52  public const MOCKED_PROVIDER_CLASSNAME = 'Mockery_1_ILIAS_GlobalScreen_Provider_Provider';
56  protected $provider_factory;
60  protected $provider_mock;
64  protected $plugin_mock;
68  protected $identification;
69 
70 
74  protected function setUp() : void
75  {
76  parent::setUp();
77 
78  $this->plugin_mock = Mockery::mock(ilPlugin::class);
79 
80  $this->provider_mock = Mockery::mock(Provider::class);
81  $this->provider_mock->shouldReceive('getProviderNameForPresentation')->andReturn('Provider')->byDefault();
82 
83  $this->provider_factory = Mockery::mock(ProviderFactory::class);
84  $this->provider_factory->shouldReceive('getProviderByClassName')->with(self::MOCKED_PROVIDER_CLASSNAME)->andReturn($this->provider_mock);
85  $this->provider_factory->shouldReceive('isInstanceCreationPossible')->andReturn(true);
86  $this->provider_factory->shouldReceive('isRegistered')->andReturn(true);
87 
88  $this->identification = new IdentificationFactory($this->provider_factory);
89  }
90 
91 
92  public function testAvailableMethods() : void
93  {
94  $r = new ReflectionClass($this->identification);
95 
96  $methods = [];
97  foreach ($r->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
98  $methods[] = $method->getName();
99  }
100  sort($methods);
101  $this->assertEquals(
102  [
103  0 => '__construct',
104  1 => 'core',
105  2 => 'fromSerializedIdentification',
106  3 => 'plugin',
107  4 => 'tool'
108  ],
109  $methods
110  );
111  }
112 
113 
114  public function testCore() : void
115  {
116  $this->assertInstanceOf(IdentificationProviderInterface::class, $this->identification->core($this->provider_mock));
117  $this->assertInstanceOf(IdentificationInterface::class, $this->identification->core($this->provider_mock)->identifier('dummy'));
118  }
119 
120 
121  public function testPlugin() : void
122  {
123  $this->plugin_mock->shouldReceive('getId')->once()->andReturn('xdemo');
124  $identification_provider = $this->identification->plugin($this->plugin_mock->getId(), $this->provider_mock);
125  $this->assertInstanceOf(IdentificationProviderInterface::class, $identification_provider);
126  $identification = $identification_provider->identifier('dummy');
127  $this->assertInstanceOf(IdentificationInterface::class, $identification);
128  }
129 
130 
131  public function testSerializingCore() : void
132  {
133  $identification = $this->identification->core($this->provider_mock)->identifier('dummy');
134  $this->assertInstanceOf(Serializable::class, $identification);
135  $this->assertEquals($identification->serialize(), get_class($this->provider_mock) . "|dummy");
136  }
137 
138 
139  public function testUnserializingCore() : void
140  {
141  $identification = $this->identification->core($this->provider_mock)->identifier('dummy');
142  $serialized_identification = $identification->serialize();
143 
144  $new_identification = $this->identification->fromSerializedIdentification($serialized_identification);
145  $this->assertEquals($identification, $new_identification);
146  }
147 
148 
149  public function testUnserializingPlugin() : void
150  {
151  $this->plugin_mock->shouldReceive('getId')->once()->andReturn('xdemo');
152  $identification = $this->identification->plugin($this->plugin_mock->getId(), $this->provider_mock)->identifier('dummy');
153  $serialized_identification = $identification->serialize();
154  $this->provider_mock->shouldReceive('getProviderNameForPresentation')->andReturn('Provider');
155  $new_identification = $this->identification->fromSerializedIdentification($serialized_identification);
156  $this->assertEquals($identification, $new_identification);
157  }
158 
159 
161  {
162  $this->expectException(InvalidArgumentException::class);
163  $this->expectExceptionMessage("Nobody can handle serialized identification 'ThisStringWillNobodyHandle'.");
164  $this->identification->fromSerializedIdentification("ThisStringWillNobodyHandle");
165  }
166 
167 
168  public function testFactoryMustReturnCorrectTypeCore() : void
169  {
170  $class_name = self::MOCKED_PROVIDER_CLASSNAME;
171  $internal_identifier = "internal_identifier";
172 
173  $string_core = "$class_name|$internal_identifier";
174  $identification = $this->identification->fromSerializedIdentification($string_core);
175 
176  $this->assertInstanceOf(CoreIdentification::class, $identification);
177  $this->assertEquals($identification->getClassName(), $class_name);
178  $this->assertEquals($identification->getInternalIdentifier(), $internal_identifier);
179  }
180 
181 
182  public function testFactoryMustReturnCorrectTypePlugin() : void
183  {
184  $class_name = self::MOCKED_PROVIDER_CLASSNAME;
185  $internal_identifier = "internal_identifier";
186 
187  // $this->markTestSkipped('I currently have absolutely no idea why this test does not work since this seems to be identical zo the test testUnserializingCore :(');
188  $string_plugin = "xdemo|$class_name|$internal_identifier";
189  $identification = $this->identification->fromSerializedIdentification($string_plugin);
190 
191  $this->assertInstanceOf(PluginIdentification::class, $identification);
192  $this->assertEquals($identification->getClassName(), $class_name);
193  $this->assertEquals($identification->getInternalIdentifier(), $internal_identifier);
194  }
195 }
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...