ILIAS  release_5-4 Revision v5.4.26-12-gabc799a52e6
IdentificationFactoryTest.php
Go to the documentation of this file.
1 <?php
2 
4 
13 use ilPlugin;
15 use Mockery;
18 use ReflectionClass;
20 use Serializable;
21 
22 require_once('./libs/composer/vendor/autoload.php');
23 
34 class IdentificationFactoryTest extends TestCase
35 {
37  const MOCKED_PROVIDER_CLASSNAME = 'Mockery_1_ILIAS_GlobalScreen_Provider_Provider';
41  protected $provider_factory;
45  protected $provider_mock;
49  protected $plugin_mock;
53  protected $identification;
54 
55 
59  protected function setUp()
60  {
61  parent::setUp();
62 
63  $this->plugin_mock = Mockery::mock(ilPlugin::class);
64 
65  $this->provider_mock = Mockery::mock(Provider::class);
66  $this->provider_mock->shouldReceive('getProviderNameForPresentation')->andReturn('Provider')->byDefault();
67 
68  $this->provider_factory = Mockery::mock(ProviderFactoryInterface::class);
69  $this->provider_factory->shouldReceive('getProviderByClassName')->with(self::MOCKED_PROVIDER_CLASSNAME)->andReturn($this->provider_mock);
70  $this->provider_factory->shouldReceive('isInstanceCreationPossible')->andReturn(true);
71  $this->provider_factory->shouldReceive('isRegistered')->andReturn(true);
72 
73  $this->identification = new IdentificationFactory($this->provider_factory);
74  }
75 
76 
77  public function testAvailableMethods()
78  {
79  $r = new ReflectionClass($this->identification);
80 
81  $methods = [];
82  foreach ($r->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
83  $methods[] = $method->getName();
84  }
85  sort($methods);
86  $this->assertEquals(
87  $methods,
88  [
89  0 => '__construct',
90  1 => 'core',
91  2 => 'fromSerializedIdentification',
92  3 => 'plugin',
93  ]
94  );
95  }
96 
97 
98  public function testCore()
99  {
100  $this->assertInstanceOf(IdentificationProviderInterface::class, $this->identification->core($this->provider_mock));
101  $this->assertInstanceOf(IdentificationInterface::class, $this->identification->core($this->provider_mock)->identifier('dummy'));
102  }
103 
104 
105  public function testPlugin()
106  {
107  $this->plugin_mock->shouldReceive('getId')->once()->andReturn('xdemo');
108  $identification_provider = $this->identification->plugin($this->plugin_mock, $this->provider_mock);
109  $this->assertInstanceOf(IdentificationProviderInterface::class, $identification_provider);
110  $identification = $identification_provider->identifier('dummy');
111  $this->assertInstanceOf(IdentificationInterface::class, $identification);
112  }
113 
114 
115  public function testSerializingCore()
116  {
117  $identification = $this->identification->core($this->provider_mock)->identifier('dummy');
118  $this->assertInstanceOf(Serializable::class, $identification);
119  $this->assertEquals($identification->serialize(), get_class($this->provider_mock) . "|dummy");
120  }
121 
122 
123  public function testUnserializingCore()
124  {
125  $identification = $this->identification->core($this->provider_mock)->identifier('dummy');
126  $serialized_identification = $identification->serialize();
127 
128  $new_identification = $this->identification->fromSerializedIdentification($serialized_identification);
129  $this->assertEquals($identification, $new_identification);
130  }
131 
132 
133  public function testUnserializingPlugin()
134  {
135  $this->plugin_mock->shouldReceive('getId')->once()->andReturn('xdemo');
136  $identification = $this->identification->plugin($this->plugin_mock, $this->provider_mock)->identifier('dummy');
137  $serialized_identification = $identification->serialize();
138  $this->provider_mock->shouldReceive('getProviderNameForPresentation')->andReturn('Provider');
139  $new_identification = $this->identification->fromSerializedIdentification($serialized_identification);
140  $this->assertEquals($identification, $new_identification);
141  }
142 
143 
145  {
146  $this->expectException(InvalidArgumentException::class);
147  $this->expectExceptionMessage("Nobody can handle serialized identification 'ThisStringWillNobodyHandle'.");
148  $this->identification->fromSerializedIdentification("ThisStringWillNobodyHandle");
149  }
150 
151 
153  {
154  $class_name = self::MOCKED_PROVIDER_CLASSNAME;
155  $internal_identifier = "internal_identifier";
156 
157  $string_core = "{$class_name}|{$internal_identifier}";
158  $identification = $this->identification->fromSerializedIdentification($string_core);
159 
160  $this->assertInstanceOf(CoreIdentification::class, $identification);
161  $this->assertEquals($identification->getClassName(), $class_name);
162  $this->assertEquals($identification->getInternalIdentifier(), $internal_identifier);
163  }
164 
165 
167  {
168  $class_name = self::MOCKED_PROVIDER_CLASSNAME;
169  $internal_identifier = "internal_identifier";
170 
171  // $this->markTestSkipped('I currently have absolutely no idea why this test does not work since this seems to be identical zo the test testUnserializingCore :(');
172  $string_plugin = "xdemo|{$class_name}|{$internal_identifier}";
173  $identification = $this->identification->fromSerializedIdentification($string_plugin);
174 
175  $this->assertInstanceOf(PluginIdentification::class, $identification);
176  $this->assertEquals($identification->getClassName(), $class_name);
177  $this->assertEquals($identification->getInternalIdentifier(), $internal_identifier);
178  }
179 }
$r
Definition: example_031.php:79