MDL-80203 courseformat: Fix some typos and PHPDoc
[moodle.git] / lib / tests / di_test.php
blobdfb79f0d006a520cffb803ad69a0f4b8d450b050
1 <?php
2 // This file is part of Moodle - http://moodle.org/
3 //
4 // Moodle is free software: you can redistribute it and/or modify
5 // it under the terms of the GNU General Public License as published by
6 // the Free Software Foundation, either version 3 of the License, or
7 // (at your option) any later version.
8 //
9 // Moodle is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 // GNU General Public License for more details.
14 // You should have received a copy of the GNU General Public License
15 // along with Moodle. If not, see <http://www.gnu.org/licenses/>.
17 namespace core;
19 use PHPUnit\Framework\MockObject\Stub;
20 use Psr\Container\ContainerInterface;
22 /**
23 * Tests for Moodle's Container.
25 * @package core
26 * @copyright 2024 Andrew Nicols <andrew@nicols.co.uk>
27 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
28 * @covers \core\di
30 class di_test extends \advanced_testcase {
31 /**
32 * Test that the get_container method returns the Container Instance and stores it statically.
34 public function test_get_container(): void {
35 $container = di::get_container();
36 $this->assertInstanceOf(ContainerInterface::class, $container);
38 $this->assertTrue($container === di::get_container());
41 /**
42 * Test that the reset_container method resets the container such that a different instance is returned.
44 public function test_reset_container(): void {
45 $instance = di::get_container();
46 $this->assertInstanceOf(ContainerInterface::class, $instance);
48 di::reset_container();
49 $this->assertFalse($instance === di::get_container());
52 /**
53 * This test just ensures that a container can return an autowired client.
55 * This is standard behaviour for a Container, but we want to actually check it.
57 public function test_autowired_client(): void {
58 $container = di::get_container();
59 $client = $container->get(http_client::class);
61 $this->assertInstanceOf(http_client::class, $client);
63 // Fetching the same again.
64 $this->assertEquals(
65 $client,
66 $container->get(http_client::class),
70 /**
71 * Test that we can mock a client and set it in the container for other consumers to get.
73 public function test_mocked_client(): void {
74 $container = di::get_container();
76 // Create a mocked http_client.
77 $mockedclient = $this->createStub(http_client::class);
79 // Set it in the container.
80 di::set(http_client::class, $mockedclient);
82 // Fetching it out will give us the same mocked client.
83 $client = $container->get(http_client::class);
84 $this->assertEquals(
85 $mockedclient,
86 $client,
89 // And the returned client will of course still be an http_client and a Stub.
90 $this->assertInstanceOf(http_client::class, $client);
91 $this->assertInstanceOf(Stub::class, $client);
93 // Even after getting a new container instance.
94 $this->assertEquals(
95 di::get_container()->get(http_client::class),
96 $client,
99 // Resetting the container will give us a new, unmocked, instance.
100 di::reset_container();
102 $client = di::get_container()->get(http_client::class);
103 $this->assertInstanceOf(http_client::class, $client);
104 $this->assertNotInstanceOf(Stub::class, $client);
105 $this->assertNotEquals(
106 $mockedclient,
107 $client,
112 * Test that a mocked client can be set in one test, but is not preserved across tests.
114 * @return Stub The mocked client to pass to the dependant test
116 public function test_mocked_client_test_one(): Stub {
117 di::set(http_client::class, $this->createStub(http_client::class));
119 $mockedclient = di::get_container()->get(http_client::class);
120 $this->assertInstanceOf(http_client::class, $mockedclient);
121 $this->assertInstanceOf(Stub::class, $mockedclient);
123 return $mockedclient;
127 * Test that a client mocked in a previous test does not bleed.
129 * @depends test_mocked_client_test_one
131 public function test_mocked_client_test_two(Stub $mockedclient): void {
132 $client = di::get_container()->get(http_client::class);
133 $this->assertInstanceOf(http_client::class, $client);
134 $this->assertNotInstanceOf(Stub::class, $client);
135 $this->assertNotEquals($mockedclient, $client);
139 * Test that the container will return the $DB global as a moodle_database instance.
141 public function test_fetch_moodle_database(): void {
142 global $DB;
144 $this->assertEquals($DB, di::get(\moodle_database::class));
148 * Test that the hook manager is in the container.
150 public function test_fetch_hook_manager(): void {
151 $manager = di::get(hook\manager::class);
152 $this->assertEquals($manager, hook\manager::get_instance());
155 public function test_fetch_string_manager(): void {
156 $stringmanager = di::get(\core_string_manager::class);
157 $this->assertEquals(
158 get_string_manager(),
159 $stringmanager,