From a9074a26499bf77d29b5b20f0c40939e39eea249 Mon Sep 17 00:00:00 2001 From: "Eloy Lafuente (stronk7)" Date: Mon, 27 Apr 2015 18:24:11 +0200 Subject: [PATCH] MDL-49664 renderers: Verify expected render methods are called. For various renderables (core, plugin, namespaced), all them against the base and plugin renderers. --- lib/tests/fixtures/namespaced_renderable.php | 25 +++++++++++++++++ lib/tests/outputcomponents_test.php | 41 ++++++++++++++++++++++++++++ 2 files changed, 66 insertions(+) create mode 100644 lib/tests/fixtures/namespaced_renderable.php diff --git a/lib/tests/fixtures/namespaced_renderable.php b/lib/tests/fixtures/namespaced_renderable.php new file mode 100644 index 00000000000..951ef35b7c4 --- /dev/null +++ b/lib/tests/fixtures/namespaced_renderable.php @@ -0,0 +1,25 @@ +. + +namespace something\largely\namespaced; + +defined('MOODLE_INTERNAL') || die; + +/** + * Simple namespaced renderable class to verify corresponding render methods are called correctly + */ +class renderable_test implements \renderable { +} diff --git a/lib/tests/outputcomponents_test.php b/lib/tests/outputcomponents_test.php index aa0e52b832c..7bb116c8329 100644 --- a/lib/tests/outputcomponents_test.php +++ b/lib/tests/outputcomponents_test.php @@ -416,4 +416,45 @@ EOF; $this->assertEquals($expecteda, $pbara->pagelinks); $this->assertEquals($expectedb, $pbarb->pagelinks); } + + public function test_renderer_method_names() { + global $CFG; + // Note, here we don't verify autoloading at all, but only that + // the renderers call to the correct methods no matter the renderable class + // is namespaced or no. Hence we are loading the needed fixtures manually. + require_once($CFG->dirroot . '/mod/assign/renderable.php'); + require_once($CFG->libdir . '/tests/fixtures/namespaced_renderable.php'); + + // Array of renderable widgets to verify that renderers + // do call to the expected render method. Indexes are the expected + // method and values are the renderable instances. + $renderables = array( + 'render_pix_icon' => new pix_icon('test', 'test'), // A core one. + 'render_assign_course_index_summary' => new assign_course_index_summary(true, 'test'), // A plugin one. + 'render_renderable_test' => new \something\largely\namespaced\renderable_test(), // A namespaced one. + ); + + // We are going to test all the renderables agains some different renderers. + $renderers = array( + 'renderer_base', + 'plugin_renderer_base' + ); + + // Run all combinations. + foreach ($renderables as $method => $renderable) { + foreach ($renderers as $renderer) { + // Create the double with the expected method mocked. + $stub = $this->getMockBuilder($renderer) + ->disableOriginalConstructor() + ->setMethods(array($method)) + ->getMock(); + // Expect the method is called once and will return true. + $stub->expects($this->once()) + ->method($method) + ->will($this->returnValue(true)); + // Assert the return value and verify the expectation. + $this->assertTrue($stub->render($renderable)); + } + } + } } -- 2.11.4.GIT