MDL-32960 refactor phpunit integration to one class per file structure
[moodle.git] / lib / phpunit / classes / block_generator.php
CommitLineData
7e7cfe7a
PS
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.
13//
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/>.
16
17/**
18 * Block generator base class.
19 *
20 * @package core
21 * @category phpunit
22 * @copyright 2012 Petr Skoda {@link http://skodak.org}
23 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
24 */
25
26
27/**
28 * Block generator base class.
29 *
30 * Extend in blocks/xxxx/tests/generator/lib.php as class block_xxxx_generator.
31 *
32 * @package core
33 * @category phpunit
34 * @copyright 2012 Petr Skoda {@link http://skodak.org}
35 * @license http://www.gnu.org/copyleft/gpl.html GNU GPL v3 or later
36 */
37abstract class phpunit_block_generator {
38 /** @var phpunit_data_generator@var */
39 protected $datagenerator;
40
41 /** @var number of created instances */
42 protected $instancecount = 0;
43
44 public function __construct(phpunit_data_generator $datagenerator) {
45 $this->datagenerator = $datagenerator;
46 }
47
48 /**
49 * To be called from data reset code only,
50 * do not use in tests.
51 * @return void
52 */
53 public function reset() {
54 $this->instancecount = 0;
55 }
56
57 /**
58 * Returns block name
59 * @return string name of block that this class describes
60 * @throws coding_exception if class invalid
61 */
62 public function get_blockname() {
63 $matches = null;
64 if (!preg_match('/^block_([a-z0-9_]+)_generator$/', get_class($this), $matches)) {
65 throw new coding_exception('Invalid block generator class name: '.get_class($this));
66 }
67
68 if (empty($matches[1])) {
69 throw new coding_exception('Invalid block generator class name: '.get_class($this));
70 }
71 return $matches[1];
72 }
73
74 /**
75 * Fill in record defaults
76 * @param stdClass $record
77 * @return stdClass
78 */
79 protected function prepare_record(stdClass $record) {
80 $record->blockname = $this->get_blockname();
81 if (!isset($record->parentcontextid)) {
82 $record->parentcontextid = context_system::instance()->id;
83 }
84 if (!isset($record->showinsubcontexts)) {
85 $record->showinsubcontexts = 1;
86 }
87 if (!isset($record->pagetypepattern)) {
88 $record->pagetypepattern = '';
89 }
90 if (!isset($record->subpagepattern)) {
91 $record->subpagepattern = null;
92 }
93 if (!isset($record->defaultregion)) {
94 $record->defaultregion = '';
95 }
96 if (!isset($record->defaultweight)) {
97 $record->defaultweight = '';
98 }
99 if (!isset($record->configdata)) {
100 $record->configdata = null;
101 }
102 return $record;
103 }
104
105 /**
106 * Create a test block
107 * @param array|stdClass $record
108 * @param array $options
109 * @return stdClass activity record
110 */
111 abstract public function create_instance($record = null, array $options = null);
112}