ILIAS  trunk Revision v12.0_alpha-377-g3641b37b9db
JavaScriptBindableTest.php
Go to the documentation of this file.
1<?php
2
19declare(strict_types=1);
20
21use PHPUnit\Framework\TestCase;
23use ILIAS\UI\Implementation\Component\ComponentHelper;
24
25require_once("vendor/composer/vendor/autoload.php");
26
28{
30 use ComponentHelper;
31}
32
36class JavaScriptBindableTest extends TestCase
37{
39
40 public function setUp(): void
41 {
42 $this->mock = new JSComponentMock();
43 }
44
45 public function testWithOnLoadCode(): void
46 {
47 $m = $this->mock->withOnLoadCode(function ($id) {
48 return "Its me, $id!";
49 });
50
51 $binder = $m->getOnLoadCode();
52 $this->assertInstanceOf(Closure::class, $binder);
53 $this->assertEquals("Its me, Mario!", $binder("Mario"));
54 }
55
56 public function testWithOnLoadCodeFalseClosure1(): void
57 {
58 try {
59 $this->mock->withOnLoadCode(function (): void {
60 });
61 $this->assertFalse("This should not happen...");
62 } catch (InvalidArgumentException $exception) {
63 $this->assertTrue(true);
64 }
65 }
66
67 public function testWithOnLoadCodeFalseClosure2(): void
68 {
69 try {
70 $this->mock->withOnLoadCode(function ($id, $some_arg): void {
71 });
72 $this->assertFalse("This should not happen...");
73 } catch (InvalidArgumentException $exception) {
74 $this->assertTrue(true);
75 }
76 }
77
78 public function testWithAdditionalOnLoadCode(): void
79 {
80 $java_script_id = 'js-id-1';
81 $java_script_code_1 = 'first piece of JavaScript code with id: ';
82 $java_script_code_2 = 'second piece of JavaScript code with id: ';
83
84 $java_script_bindable = $this
85 ->mock
86 ->withOnLoadCode(static fn($id) => $java_script_code_1 . $id)
87 ->withAdditionalOnLoadCode(static fn($id) => $java_script_code_2 . $id);
88
89 $java_script_binder = $java_script_bindable->getOnLoadCode();
90 $this->assertInstanceOf(Closure::class, $java_script_binder);
91 $this->assertEquals(
92 $java_script_code_2 . $java_script_id . "\n" .
93 $java_script_code_1 . $java_script_id,
94 $java_script_binder($java_script_id)
95 );
96 }
97
99 {
100 $m = $this->mock
101 ->withAdditionalOnLoadCode(function ($id) {
102 return "And again, me: $id.";
103 });
104
105 $binder = $m->getOnLoadCode();
106 $this->assertInstanceOf(Closure::class, $binder);
107 $this->assertEquals("And again, me: Mario.", $binder("Mario"));
108 }
109}
$id
plugin.php for ilComponentBuildPluginInfoObjectiveTest::testAddPlugins
Definition: plugin.php:23
Interface to be extended by components that have the possibility to bind to Javascript.
trait JavaScriptBindable
Trait for components implementing JavaScriptBindable providing standard implementation.