Skip to content

Commit

Permalink
use di -> remove legacy test code
Browse files Browse the repository at this point in the history
  • Loading branch information
Glutamat42 committed Oct 26, 2024
1 parent 1fcaf43 commit a3bb7da
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 92 deletions.
8 changes: 4 additions & 4 deletions classes/condition.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use base_logger;
use coding_exception;
use core\di;
use core_availability\condition as availability_condition;
use core_availability\info;
use core_plugin_manager;
Expand All @@ -17,7 +18,6 @@


class condition extends availability_condition {
use static_call_trait;
protected logger $logger;

protected string $condition;
Expand Down Expand Up @@ -124,7 +124,7 @@ public function evaluate_section_requirements($statement, $userid, bool $validat
*/
protected function evaluate_section($section_id, $userid): bool {
try {
return plugin_interface::is_section_completed($section_id, $userid);
return di::get(plugin_interface::class)::is_section_completed($section_id, $userid);
} catch (moodle_exception $e) {
if ($e->errorcode == 'user_not_enrolled') {
return false;
Expand Down Expand Up @@ -188,7 +188,7 @@ protected function make_condition_user_readable(string $statement): string {
} else {
$updated_statement .= '<span style="color: red;">';
}
$section_name = plugin_interface::get_section_name($part);
$section_name = di::get(plugin_interface::class)::get_section_name($part);
$updated_statement .= htmlspecialchars($section_name, ENT_QUOTES, 'UTF-8') . '</span>';
} else {
switch ($part) {
Expand Down Expand Up @@ -250,7 +250,7 @@ public function update_after_restore($restoreid, $courseid, base_logger $logger,
$i = $j - 1;

// add updated id to new string
$updated_id = $this->callStatic(restore_dbops::class, 'get_backup_ids_record', $restoreid, 'course_section', $number);
$updated_id = di::get(restore_dbops::class)::get_backup_ids_record($restoreid, 'course_section', $number);
if ($updated_id == false) {
throw new moodle_exception('unknown_section', 'availability_adler', '', NULL, 'section: ' . $number);
}
Expand Down
28 changes: 0 additions & 28 deletions classes/static_call_trait.php

This file was deleted.

49 changes: 26 additions & 23 deletions tests/condition_test.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@

use availability_adler\lib\adler_testcase;
use base_logger;
use core\di;
use core_availability\info;
use core_plugin_manager;
use local_adler\plugin_interface;
Expand Down Expand Up @@ -83,24 +84,23 @@ public function provide_test_evaluate_section_data() {

/**
* @dataProvider provide_test_evaluate_section_data
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* # ANF-ID: [MVP12]
*/
public function test_evaluate_section($exception) {
$plugin_interface_mock = Mockery::mock('overload:'. plugin_interface::class);
$plugin_interface_mock = Mockery::mock(plugin_interface::class);
if ($exception == null) {
$plugin_interface_mock->shouldReceive('is_section_completed')
->once()
->with(1,1)
->with(1, 1)
->andReturn(true);
} else {
$plugin_interface_mock->shouldReceive('is_section_completed')
->once()
->with(1,1)
->with(1, 1)
->andThrow(new moodle_exception($exception));
}
di::set(plugin_interface::class, $plugin_interface_mock);
if ($exception != null && $exception != 'user_not_enrolled') {
$this->expectException(moodle_exception::class);
$this->expectExceptionMessage($exception);
Expand All @@ -112,7 +112,7 @@ public function test_evaluate_section($exception) {


$condition = Mockery::mock(condition::class);
$result = $method->invoke($condition,1,1);
$result = $method->invoke($condition, 1, 1);


if ($exception == null) {
Expand Down Expand Up @@ -305,8 +305,6 @@ public function provide_test_make_condition_user_readable_data() {

/**
* @dataProvider provide_test_make_condition_user_readable_data
* @runInSeparateProcess
* @preserveGlobalState disabled
*
* # ANF-ID: [MVP13]
*/
Expand All @@ -318,10 +316,11 @@ public function test_make_condition_user_readable($condition, $section_states, $
$condition_mock->shouldReceive('evaluate_section')->with($section_id, 0)->andReturn($section_state);
}

$plugin_interface_mock = Mockery::mock('overload:' . plugin_interface::class);
$plugin_interface_mock = Mockery::mock(plugin_interface::class);
$plugin_interface_mock->shouldReceive('get_section_name')->andReturnUsing(function ($section_id) use ($section_names) {
return $section_names[$section_id];
});
di::set(plugin_interface::class, $plugin_interface_mock);

$this->assertEquals($expected, $condition_mock->make_condition_user_readable($condition, 0));
}
Expand Down Expand Up @@ -502,36 +501,40 @@ public function test_update_after_restore($condition, $backup_id_mappings, $expe
$name = 'test';

// create get_backup_ids_record return map
$restore_dbops_mock = Mockery::mock(restore_dbops::class);
$return_map = [];
foreach ($backup_id_mappings as $mapping) {
$return_map[] = [restore_dbops::class, 'get_backup_ids_record', $restoreid, 'course_section', (string)$mapping[0], $mapping[1]];
$return_map[] = [$restoreid, 'course_section', (string)$mapping[0], $mapping[1]];
}

// mock condition
$condition_mock = $this->getMockBuilder(condition::class)
->disableOriginalConstructor()
->onlyMethods(['callStatic'])
->getMock();
$condition_mock->method('callStatic')
->will($this->returnValueMap($return_map));

$restore_dbops_mock->shouldReceive('get_backup_ids_record')
->andReturnUsing(function ($restoreid, $type, $itemid) use ($return_map) {
foreach ($return_map as $map) {
if ($map[0] == $restoreid && $map[1] == $type && $map[2] == $itemid) {
return $map[3];
}
}
return false;
});
di::set(restore_dbops::class, $restore_dbops_mock);

$condition_instance = Mockery::mock(condition::class)->makePartial();
// set condition
$reflection = new ReflectionClass($condition_mock);
$reflection = new ReflectionClass($condition_instance);
$property = $reflection->getProperty('condition');
$property->setAccessible(true);
$property->setValue($condition_mock, $condition);
$property->setValue($condition_instance, $condition);

// setup exception
if ($expect_exception) {
$this->expectException($expect_exception);
}

// call update_after_restore
$result = $condition_mock->update_after_restore($restoreid, $courseid, $base_logger_mock, $name);
$result = $condition_instance->update_after_restore($restoreid, $courseid, $base_logger_mock, $name);

// verify result
$this->assertEquals(true, $result);
$updated_condition = $property->getValue($condition_mock);
$updated_condition = $property->getValue($condition_instance);
$this->assertEquals($expected_updated_condition, $updated_condition);
}
}
37 changes: 0 additions & 37 deletions tests/static_call_trait_test.php

This file was deleted.

0 comments on commit a3bb7da

Please sign in to comment.