ControllerTestCase.php
1 <?php
2 /**
3  * wCMF - wemove Content Management Framework
4  * Copyright (C) 2005-2020 wemove digital solutions GmbH
5  *
6  * Licensed under the terms of the MIT License.
7  *
8  * See the LICENSE file distributed with this work for
9  * additional information.
10  */
11 namespace wcmf\test\lib;
12 
16 
17 /**
18  * ControllerTestCase is the base class for test cases used for Controllers.
19  *
20  * @author ingo herwig <ingo@wemove.com>
21  */
22 abstract class ControllerTestCase extends DatabaseTestCase {
23 
24  /**
25  * Make a request to the controller. This method makes sure that the
26  * requested action is routed to the controller to be tested.
27  * The calling method has to make sure that a session is started, if necessary
28  * (e.g. by calling TestUtil::startSession()). The transaction will be rolled
29  * back before the request is run in order to avoid side effects.
30  * @param $action The action
31  * @param $data An associative array with additional key/value pairs for the Request instance
32  * @parma $addActionKey Boolean, whether to add an action key for the given action to the configuration or not (optional, default: _true_)
33  * @return Response instance
34  */
35  protected function runRequest($action, $data, $addActionKey=true) {
36  return $this->runInternal($action, $data, $addActionKey, false);
37  }
38 
39  /**
40  * Make a request to the controller with the controller set as sender.
41  * @see ControllerTestCase::runRequest()
42  */
43  protected function runRequestFromThis($action, $data, $addActionKey=true) {
44  return $this->runInternal($action, $data, $addActionKey, true);
45  }
46 
47  /**
48  * Make a request to the controller.
49  * @param $action The action
50  * @param $data An associative array with additional key/value pairs for the Request instance
51  * @parma $addActionKey Boolean, whether to add an action key for the given action to the configuration or not (optional, default: _true_)
52  * @parma $addSender Boolean, whether to add the controller as sender or not (optional, default: _false_)
53  * @return Response instance
54  */
55  private function runInternal($action, $data, $addActionKey=true, $addSender=false) {
56  $persistenceFacade = ObjectFactory::getInstance('persistenceFacade');
57  $persistenceFacade->getTransaction()->rollback();
58 
59  // add action key
60  if ($addActionKey) {
61  TestUtil::setConfigValue('??'.$action, $this->getControllerName(), 'actionmapping');
62  }
63 
64  // make request
65  $request = ObjectFactory::getNewInstance('request');
66  $request->setAction($action);
67  if ($addSender) {
68  $request->setSender($this->getControllerName());
69  }
70  foreach ($data as $key => $value) {
71  $request->setValue($key, $value);
72  }
73  return TestUtil::simulateRequest($request);
74  }
75 
76  /**
77  * Get the fully qualified name of the controller to test
78  * @return The name of the controller
79  */
80  abstract protected function getControllerName();
81 }
82 ?>
ControllerTestCase is the base class for test cases used for Controllers.
static setConfigValue($key, $value, $section)
Set a configuration value.
Definition: TestUtil.php:200
getControllerName()
Get the fully qualified name of the controller to test.
runRequestFromThis($action, $data, $addActionKey=true)
Make a request to the controller with the controller set as sender.
Test support classes.
Definition: namespaces.php:100
runRequest($action, $data, $addActionKey=true)
Make a request to the controller.
DatabaseTestCase is the base class for test cases that need database support.
static getNewInstance($name, $dynamicConfiguration=[])
static getInstance($name, $dynamicConfiguration=[])
TestUtil provides helper methods for testing wCMF functionality.
Definition: TestUtil.php:25
static simulateRequest($request)
Process a request as if it was sent to main.php.
Definition: TestUtil.php:151
ObjectFactory implements the service locator pattern by wrapping a Factory instance and providing sta...