From d50475e64f0f8b49665f502ff405cc2d351414b6 Mon Sep 17 00:00:00 2001 From: nicolasconnault Date: Tue, 19 Aug 2008 09:21:30 +0000 Subject: [PATCH] MDL-15666 Adding test file with stub methods. --- lib/simpletest/testportfoliolib.php | 40 +++++++++++++++--- .../simpletest/testportfoliopluginboxnet.php | 41 +++++++++++++++++++ 2 files changed, 75 insertions(+), 6 deletions(-) create mode 100644 portfolio/type/boxnet/simpletest/testportfoliopluginboxnet.php diff --git a/lib/simpletest/testportfoliolib.php b/lib/simpletest/testportfoliolib.php index afeed65fee..bb66ff0d21 100755 --- a/lib/simpletest/testportfoliolib.php +++ b/lib/simpletest/testportfoliolib.php @@ -38,11 +38,21 @@ if (!defined('MOODLE_INTERNAL')) { require_once($CFG->libdir . '/portfoliolib.php'); class portfolio_plugin_test extends portfolio_plugin_push_base { - public function expected_time($callertime){} - public function is_push(){} - public function prepare_package(){} - public function send_package(){} - public function get_continue_url(){} + public function expected_time($callertime){ + return $callertime; + } + + public function prepare_package() { + return true; + } + + public function send_package() { + return true; + } + + public function get_continue_url() { + return ''; + } } class portfolio_caller_test extends portfolio_caller_base { @@ -82,9 +92,19 @@ class portfolio_caller_test extends portfolio_caller_base { } } +/** + * The following two classes are full mocks: none of their methods do anything, including their constructor. + * They can be instantiated directly with no params (new portfolio_caller_test()) + */ Mock::generate('portfolio_caller_test', 'mock_caller'); Mock::generate('portfolio_plugin_test', 'mock_plugin'); +/** + * Partial mocks work as normal except the methods listed in the 3rd param, which are mocked. + * They are instantiated by passing $this to the constructor within the test case class. + */ +Mock::generatePartial('portfolio_plugin_test', 'partialmock_plugin', array('send_package')); + class portfoliolib_test extends UnitTestCase { public $caller; public $plugin; @@ -94,10 +114,18 @@ class portfoliolib_test extends UnitTestCase { $this->plugin = new mock_plugin(); $this->caller = new mock_caller(); $this->exporter = new portfolio_exporter(&$this->plugin, &$this->caller, '', array()); + $partialplugin = &new partialmock_plugin($this); + + // Write a new text file + $this->exporter->save(); + $this->exporter->write_new_file('Test text', 'test.txt'); } function tearDown() { - + global $DB; + $DB->delete_records('portfolio_tempdata', array('id' => $this->exporter->get('id'))); + $fs = get_file_storage(); + $fs->delete_area_files(SYSCONTEXTID, 'portfolio_exporter', $this->exporter->get('id')); } function test_construct() { diff --git a/portfolio/type/boxnet/simpletest/testportfoliopluginboxnet.php b/portfolio/type/boxnet/simpletest/testportfoliopluginboxnet.php new file mode 100644 index 0000000000..7b79bc1dc6 --- /dev/null +++ b/portfolio/type/boxnet/simpletest/testportfoliopluginboxnet.php @@ -0,0 +1,41 @@ +libdir.'/simpletest/testportfoliolib.php'); +require_once($CFG->dirroot.'/portfolio/type/boxnet/lib.php'); + +Mock::generate('boxclient', 'mock_boxclient'); +Mock::generatePartial('portfolio_plugin_boxnet', 'mock_boxnetplugin', array('ensure_ticket', 'ensure_account_tree')); + +class testPortfolioPluginBoxnet extends portfoliolib_test { + public function setUp() { + parent::setUp(); + $this->plugin = &new mock_boxnetplugin($this); + $this->plugin->boxclient = new mock_boxclient(); + } + + public function tearDown() { + parent::tearDown(); + } + + public function test_something() { + $ticket = md5(rand(0,873907)); + $authtoken = 'ezfoeompplpug3ofii4nud0d8tvg96e0'; + + $this->plugin->setReturnValue('ensure_account_tree', true); + $this->plugin->setReturnValue('ensure_ticket', $ticket); + + $this->plugin->boxclient->setReturnValue('renameFile', true); + $this->plugin->boxclient->setReturnValue('uploadFile', array('status' => 'upload_ok', 'id' => array(1))); + $this->plugin->boxclient->setReturnValue('createFolder', array(1 => 'folder 1', 2 => 'folder 2')); + $this->plugin->boxclient->setReturnValue('isError', false); + $this->plugin->boxclient->authtoken = $authtoken; + + $this->assertTrue($this->plugin->set('exporter', $this->exporter)); + $this->assertTrue($this->plugin->set('ticket', $ticket)); + $this->assertTrue($this->plugin->set('authtoken', $authtoken)); + $this->plugin->set_export_config(array('folder' => 1)); + + $this->assertTrue($this->plugin->prepare_package()); + $this->assertTrue($this->plugin->send_package()); + } +} +?> -- 2.39.5