diff options
author | Valentin Popov <info@valentineus.link> | 2019-05-10 21:43:32 +0300 |
---|---|---|
committer | Valentin Popov <info@valentineus.link> | 2019-05-10 21:43:32 +0300 |
commit | 0cbc05d52b18d06193b703c4eaced40b6f497d2d (patch) | |
tree | 48b629c7d05ceaa721dc390d8e0ece0a4e780367 /tests | |
parent | ae2f31261d8aa6019adcd618dc214c90d7b818bb (diff) | |
download | local_webhooks-0cbc05d52b18d06193b703c4eaced40b6f497d2d.tar.xz local_webhooks-0cbc05d52b18d06193b703c4eaced40b6f497d2d.zip |
Added exact other tests
Signed-off-by: Valentin Popov <info@valentineus.link>
Diffstat (limited to 'tests')
-rw-r--r-- | tests/external_test.php | 47 | ||||
-rw-r--r-- | tests/task_test.php | 84 |
2 files changed, 74 insertions, 57 deletions
diff --git a/tests/external_test.php b/tests/external_test.php index 2a9be98..5f646a9 100644 --- a/tests/external_test.php +++ b/tests/external_test.php @@ -32,28 +32,56 @@ use local_webhooks\local\record; */ final class local_webhooks_external_testcase extends externallib_advanced_testcase { /** + * Generate random an event's list. + * + * @return array + * + * @throws \ReflectionException + */ + private static function get_random_events(): array { + $result = array_rand(api::get_events(), random_int(2, 10)); + + return is_array($result) ? $result : []; + } + + /** + * Generate a random record. + * + * @return \local_webhooks\local\record + * + * @throws \ReflectionException + */ + private static function get_random_record(): record { + $record = new record(); + + $record->events = self::get_random_events(); + $record->header = 'application/json'; + $record->name = uniqid('', false); + $record->point = 'http://example.org/' . urlencode($record->name); + $record->status = true; + $record->token = generate_uuid(); + + return $record; + } + + /** * Testing external get record's data. * * @throws \ReflectionException * @throws \coding_exception * @throws \dml_exception + * @throws \invalid_parameter_exception * @throws \invalid_response_exception + * @throws \restricted_context_exception */ public function test_get_service() { self::setAdminUser(); $this->resetAfterTest(); - $record = new record(); - $record->events = array_rand(api::get_events(), random_int(2, 10)); - $record->header = 'application/json'; - $record->name = 'Example name'; - $record->point = 'http://example.org/'; - $record->status = true; - $record->token = generate_uuid(); - // Creating a new record. - $record->id = api::create_service($record); + $record = self::get_random_record(); + $record->id = api::add_service($record); $return = local_webhooks_external::get_service($record->id); $return = external_api::clean_returnvalue(local_webhooks_external::get_service_returns(), $return); @@ -70,7 +98,6 @@ final class local_webhooks_external_testcase extends externallib_advanced_testca // Testing an event's list. self::assertInternalType('array', $return['events']); self::assertNotCount(0, $return['events']); - foreach ($return['events'] as $event) { self::assertContains($event, $record->events); } diff --git a/tests/task_test.php b/tests/task_test.php index 7779261..236c1b6 100644 --- a/tests/task_test.php +++ b/tests/task_test.php @@ -20,6 +20,7 @@ global $CFG; require_once($CFG->dirroot . '/local/webhooks/classes/local/api.php'); +use core\event\course_created; use core\task\manager; use local_webhooks\local\api; use local_webhooks\local\record; @@ -32,6 +33,26 @@ use local_webhooks\task\notify; * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later */ final class local_webhooks_task_testcase extends advanced_testcase { + const EVENTNAME = '\\' . course_created::class; + + /** + * Generate a random record. + * + * @return \local_webhooks\local\record + */ + private static function get_random_record(): record { + $record = new record(); + + $record->events = [self::EVENTNAME]; + $record->header = 'application/json'; + $record->name = uniqid('', false); + $record->point = 'http://example.org/' . urlencode($record->name); + $record->status = true; + $record->token = generate_uuid(); + + return $record; + } + /** * Testing add a task to the queue. */ @@ -43,10 +64,10 @@ final class local_webhooks_task_testcase extends advanced_testcase { manager::queue_adhoc_task(new notify()); - $tasks = manager::get_adhoc_tasks('\local_webhooks\task\notify'); + $tasks = manager::get_adhoc_tasks(notify::class); self::assertCount(1, $tasks); - self::assertInstanceOf('\local_webhooks\task\notify', array_shift($tasks)); + self::assertInstanceOf(notify::class, array_shift($tasks)); } /** @@ -62,18 +83,12 @@ final class local_webhooks_task_testcase extends advanced_testcase { curl::mock_response('{}'); $this->resetAfterTest(); - $record = new record(); - $record->events = ['\core\event\course_viewed']; - $record->header = 'application/json'; - $record->name = 'Example name'; - $record->point = 'http://example.org/'; + $record = self::get_random_record(); $record->status = false; - $record->token = '967b2286-0874-4938-b088-efdbcf8a79bc'; - - api::create_service($record); + $record->id = api::add_service($record); $task = new notify(); - $task->set_custom_data(['eventname' => '\core\event\course_viewed']); + $task->set_custom_data(['eventname' => self::EVENTNAME]); $task->execute(); self::assertNull($task->debug); @@ -92,18 +107,11 @@ final class local_webhooks_task_testcase extends advanced_testcase { curl::mock_response('{}'); $this->resetAfterTest(); - $record = new record(); - $record->events = ['\core\event\course_viewed']; - $record->header = 'application/json'; - $record->name = 'Example name'; - $record->point = 'http://example.org/'; - $record->status = true; - $record->token = '967b2286-0874-4938-b088-efdbcf8a79bc'; - - $record->id = api::create_service($record); + $record = self::get_random_record(); + $record->id = api::add_service($record); $task = new notify(); - $task->set_custom_data(['eventname' => '\core\event\course_viewed']); + $task->set_custom_data(['eventname' => self::EVENTNAME]); $task->execute(); self::assertCount(1, $task->debug); @@ -133,15 +141,8 @@ final class local_webhooks_task_testcase extends advanced_testcase { $generator = self::getDataGenerator(); - $record = new record(); - $record->events = ['\core\event\course_created']; - $record->header = 'application/json'; - $record->name = 'Example name'; - $record->point = 'http://example.org/'; - $record->status = true; - $record->token = '967b2286-0874-4938-b088-efdbcf8a79bc'; - - api::create_service($record); + $record = self::get_random_record(); + $record->id = api::add_service($record); $total = random_int(5, 20); for ($i = 0; $i < $total; $i++) { @@ -150,11 +151,9 @@ final class local_webhooks_task_testcase extends advanced_testcase { $debug = []; foreach (manager::get_adhoc_tasks(notify::class) as $event) { - if (!is_object($event)) { - continue; - } - + self::assertInstanceOf(notify::class, $event); /** @var \local_webhooks\task\notify $event */ + $event->execute(); if (isset($event->debug) && is_array($event->debug)) { @@ -180,25 +179,16 @@ final class local_webhooks_task_testcase extends advanced_testcase { $generator = self::getDataGenerator(); - $record = new record(); - $record->events = ['\core\event\course_created']; - $record->header = 'application/json'; - $record->name = 'Example name'; - $record->point = 'http://example.org/'; - $record->status = true; - $record->token = '967b2286-0874-4938-b088-efdbcf8a79bc'; - - api::create_service($record); + $record = self::get_random_record(); + $record->id = api::add_service($record); $course = $generator->create_course(); $debug = []; foreach (manager::get_adhoc_tasks(notify::class) as $event) { - if (!is_object($event)) { - continue; - } - + self::assertInstanceOf(notify::class, $event); /** @var \local_webhooks\task\notify $event */ + $event->execute(); if (isset($event->debug) && is_array($event->debug)) { |