Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Feature/controller tests #19

Merged
merged 11 commits into from

2 participants

@Bukarinov

Tests for controllers

...PHPUnit/Controller/AbstractHttpControllerTestCase.php
((62 lines not shown))
+ if (array_key_exists($section, $json)) {
+ $json = $json[$section];
+ } else {
+ throw new \PHPUnit_Framework_AssertionFailedError('Path not found in JSON');
+ }
+ }
+
+ return $json;
+ }
+
+ /**
+ * Get JSON from Response
+ *
+ * @return array
+ */
+ protected function getJsonFromResponse()
@Shumkov Owner
Shumkov added a note

check content-type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...est/PHPUnit/Controller/AbstractControllerTestCase.php
((31 lines not shown))
+ * @return AbstractHttpControllerTestCase
+ */
+ public function setPluginManager(PluginManager $pluginManager)
+ {
+ $this->plugins = $pluginManager;
+ $this->plugins->setTest($this);
+
+ return $this;
+ }
+
+ /**
+ * @return PluginManager
+ */
+ public function getPluginManager()
+ {
+ if (!$this->plugins) {
@Shumkov Owner
Shumkov added a note

$this->plugins === null

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...PHPUnit/Controller/AbstractHttpControllerTestCase.php
((13 lines not shown))
+ /**
+ * Assert that JSON piece by path $path and $expected are equals
+ *
+ * @param string $path
+ * @param mixed $expected
+ * @param string $message
+ * @param int $delta
+ * @param int $maxDepth
+ * @param bool $canonicalize
+ * @param bool $ignoreCase
+ */
+ public function assertJsonEquals($path, $expected, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
+ {
+ $message = ($message) ?: sprintf('Failed asserting that JSON path "%s" value equals "%s"', $path, $expected);
+
+ $this->assertContentType('application/json');
@Shumkov Owner
Shumkov added a note

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
library/GeometriaLab/Test/Plugin/PluginListener.php
@@ -0,0 +1,119 @@
+<?php
+
+namespace GeometriaLab\Test\Plugin;
+
+use GeometriaLab\Test\TestCaseInterface;
@Shumkov Owner
Shumkov added a note

Move to PHPUnit

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
...PHPUnit/Controller/AbstractHttpControllerTestCase.php
((11 lines not shown))
+ protected $useConsoleRequest = false;
+
+ /**
+ * Assert that JSON piece by path $path and $expected are equals
+ *
+ * @param string $path
+ * @param mixed $expected
+ * @param string $message
+ * @param int $delta
+ * @param int $maxDepth
+ * @param bool $canonicalize
+ * @param bool $ignoreCase
+ */
+ public function assertJsonEquals($path, $expected, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
+ {
+ $message = ($message) ?: sprintf('Failed asserting that JSON path "%s" value equals "%s"', $path, $expected);
@Shumkov Owner
Shumkov added a note

$message === ''

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@Shumkov Shumkov merged commit dc96042 into master

1 check passed

Details default The Travis build passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
This page is out of date. Refresh to see the latest.
View
7 library/GeometriaLab/Api/Mvc/Controller/Action/Params/Listener.php
@@ -9,7 +9,8 @@
Zend\Http\Request as ZendRequest;
use GeometriaLab\Api\Exception\InvalidParamsException,
- GeometriaLab\Api\Exception\ObjectNotFoundException;
+ GeometriaLab\Api\Exception\ObjectNotFoundException,
+ GeometriaLab\Api\Authentication\Adapter\OAuth as OAuthAuthenticationAdapter;
class Listener implements ZendListenerAggregateInterface
{
@@ -93,7 +94,9 @@ protected function getParamsFromRequest(ZendRequest $request)
}
}
// @TODO It's possible to remove the access_token param only here and nowhere else
- unset($params['access_token']);
+ if (isset($params[OAuthAuthenticationAdapter::TOKEN_PARAM_NAME])) {
+ unset($params[OAuthAuthenticationAdapter::TOKEN_PARAM_NAME]);
+ }
return $params;
}
View
12 library/GeometriaLab/Test/PHPUnit/Controller/AbstractConsoleControllerTestCase.php
@@ -0,0 +1,12 @@
+<?php
+
+namespace GeometriaLab\Test\PHPUnit\Controller;
+
+/**
+ * @method \Zend\Console\Request getRequest()
+ * @method \Zend\Console\Response getResponse()
+ */
+abstract class AbstractConsoleControllerTestCase extends AbstractControllerTestCase
+{
+ protected $useConsoleRequest = true;
+}
View
84 library/GeometriaLab/Test/PHPUnit/Controller/AbstractControllerTestCase.php
@@ -0,0 +1,84 @@
+<?php
+
+namespace GeometriaLab\Test\PHPUnit\Controller;
+
+use Zend\Test\PHPUnit\Controller\AbstractControllerTestCase as ZendAbstractControllerTestCase;
+
+use GeometriaLab\Test\PHPUnit\TestCaseInterface,
+ GeometriaLab\Test\PHPUnit\Plugin\PluginManager;
+
+abstract class AbstractControllerTestCase extends ZendAbstractControllerTestCase implements TestCaseInterface
+{
+ /**
+ * @var PluginManager
+ */
+ protected $plugins;
+
+ public function setUp()
+ {
+ $this->setApplicationConfig(
+ include 'config/application.config.php'// Use relative path
+ );
+
+ // @TODO Hack
+ $this->getApplication();
+
+ parent::setUp();
+ }
+
+ /**
+ * @param PluginManager $pluginManager
+ * @return AbstractHttpControllerTestCase
+ */
+ public function setPluginManager(PluginManager $pluginManager)
+ {
+ $this->plugins = $pluginManager;
+ $this->plugins->setTest($this);
+
+ return $this;
+ }
+
+ /**
+ * @return PluginManager
+ */
+ public function getPluginManager()
+ {
+ if ($this->plugins === null) {
+ $this->setPluginManager(new PluginManager());
+ }
+
+ return $this->plugins;
+ }
+
+ /**
+ * Get plugin instance
+ *
+ * @param string $name Name of plugin to return
+ * @param null|array $options Options to pass to plugin constructor (if not already instantiated)
+ * @return mixed
+ */
+ public function plugin($name, array $options = null)
+ {
+ return $this->getPluginManager()->get($name, $options);
+ }
+
+ /**
+ * Method overloading: return/call plugins
+ *
+ * If the plugin is a functor, call it, passing the parameters provided.
+ * Otherwise, return the plugin instance.
+ *
+ * @param string $method
+ * @param array $params
+ * @return mixed
+ */
+ public function __call($method, $params)
+ {
+ $plugin = $this->plugin($method);
+ if (is_callable($plugin)) {
+ return call_user_func_array($plugin, $params);
+ }
+
+ return $plugin;
+ }
+}
View
109 library/GeometriaLab/Test/PHPUnit/Controller/AbstractHttpControllerTestCase.php
@@ -0,0 +1,109 @@
+<?php
+
+namespace GeometriaLab\Test\PHPUnit\Controller;
+
+/**
+ * @method \Zend\Http\PhpEnvironment\Request getRequest()
+ * @method \Zend\Http\PhpEnvironment\Response getResponse()
+ */
+abstract class AbstractHttpControllerTestCase extends AbstractControllerTestCase
+{
+ protected $useConsoleRequest = false;
+
+ /**
+ * Assert that JSON piece by path $path and $expected are equals
+ *
+ * @param string $path
+ * @param mixed $expected
+ * @param string $message
+ * @param int $delta
+ * @param int $maxDepth
+ * @param bool $canonicalize
+ * @param bool $ignoreCase
+ */
+ public function assertJsonEquals($path, $expected, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
+ {
+ $message = ($message === '') ?: sprintf('Failed asserting that JSON path "%s" value equals "%s"', $path, $expected);
+
+ $actual = $this->getJsonValueByPath($path);
+
+ self::assertEquals($expected, $actual, $message, $delta, $maxDepth, $canonicalize, $ignoreCase);
+ }
+
+ /**
+ * Assert that content-type header and $expected are equals
+ *
+ * @param string $expected
+ * @param string $message
+ * @param int $delta
+ * @param int $maxDepth
+ * @param bool $canonicalize
+ * @param bool $ignoreCase
+ * @throws \PHPUnit_Framework_AssertionFailedError
+ */
+ protected function assertContentType($expected, $message = '', $delta = 0, $maxDepth = 10, $canonicalize = false, $ignoreCase = false)
+ {
+ $contentType = $this->getResponse()->getHeaders()->get('content-type');
+
+ if ($contentType === false) {
+ throw new \PHPUnit_Framework_AssertionFailedError('Content-type not found in Headers');
+ }
+
+ self::assertEquals($expected, $contentType->getFieldValue(), $message, $delta, $maxDepth, $canonicalize, $ignoreCase);
+ }
+
+ /**
+ * Get JSON value by path
+ *
+ * @param string $path
+ * @throws \PHPUnit_Framework_AssertionFailedError
+ * @return mixed
+ */
+ protected function getJsonValueByPath($path = '')
+ {
+ $json = $this->getJsonFromResponse();
+
+ if (!is_array($json)) {
+ throw new \PHPUnit_Framework_AssertionFailedError('Path not found in JSON');
+ }
+
+ $path = trim($path, '/');
+
+ if ($path === '') {
+ return $json;
+ }
+
+ $sections = explode('/', $path);
+
+ foreach ($sections as $section) {
+ if (array_key_exists($section, $json)) {
+ $json = $json[$section];
+ } else {
+ throw new \PHPUnit_Framework_AssertionFailedError('Path not found in JSON');
+ }
+ }
+
+ return $json;
+ }
+
+ /**
+ * Get JSON from Response
+ *
+ * @return array
+ * @throws \PHPUnit_Framework_AssertionFailedError
+ */
+ protected function getJsonFromResponse()
+ {
+ $contentType = $this->getResponse()->getHeaders()->get('content-type');
+
+ if ($contentType === false) {
+ throw new \PHPUnit_Framework_AssertionFailedError('Content-type not found in Headers');
+ }
+
+ if ($contentType->getFieldValue() !== 'application/json') {
+ throw new \PHPUnit_Framework_AssertionFailedError('Content-type not equals "application/json"');
+ }
+
+ return json_decode($this->getResponse()->getContent(), true);
+ }
+}
View
44 library/GeometriaLab/Test/PHPUnit/Plugin/AbstractPlugin.php
@@ -0,0 +1,44 @@
+<?php
+
+namespace GeometriaLab\Test\PHPUnit\Plugin;
+
+use GeometriaLab\Test\PHPUnit\TestCaseInterface;
+
+abstract class AbstractPlugin implements PluginInterface
+{
+ /**
+ * @var TestCaseInterface
+ */
+ protected $test;
+
+ /**
+ * Set test object
+ *
+ * @param TestCaseInterface $test
+ * @return AbstractPlugin
+ */
+ public function setTest(TestCaseInterface $test)
+ {
+ $this->test = $test;
+
+ return $this;
+ }
+
+ /**
+ * Get test object
+ *
+ * @return TestCaseInterface
+ */
+ public function getTest()
+ {
+ return $this->test;
+ }
+
+ /**
+ * It will call when a test ended
+ */
+ public function tearDown()
+ {
+
+ }
+}
View
8 library/GeometriaLab/Test/PHPUnit/Plugin/Exception/InvalidPluginException.php
@@ -0,0 +1,8 @@
+<?php
+
+namespace GeometriaLab\Test\PHPUnit\Plugin\Exception;
+
+class InvalidPluginException extends \Exception
+{
+
+}
View
28 library/GeometriaLab/Test/PHPUnit/Plugin/PluginInterface.php
@@ -0,0 +1,28 @@
+<?php
+
+namespace GeometriaLab\Test\PHPUnit\Plugin;
+
+use GeometriaLab\Test\PHPUnit\TestCaseInterface;
+
+interface PluginInterface
+{
+ /**
+ * Set test case object
+ *
+ * @param TestCaseInterface $test
+ * @return PluginInterface
+ */
+ public function setTest(TestCaseInterface $test);
+
+ /**
+ * Get test case object
+ *
+ * @return TestCaseInterface
+ */
+ public function getTest();
+
+ /**
+ * It will call when a test ended
+ */
+ public function tearDown();
+}
View
131 library/GeometriaLab/Test/PHPUnit/Plugin/PluginListener.php
@@ -0,0 +1,131 @@
+<?php
+
+namespace GeometriaLab\Test\PHPUnit\Plugin;
+
+use GeometriaLab\Test\PHPUnit\TestCaseInterface;
+
+use Zend\ServiceManager\Config as ZendConfig;
+
+class PluginListener implements \PHPUnit_Framework_TestListener
+{
+ /**
+ * @var PluginManager
+ */
+ protected $pluginManager;
+
+ /**
+ * @param array $plugins Plugins map
+ */
+ public function __construct(array $plugins = array())
+ {
+ $this->pluginManager = new PluginManager(new ZendConfig(array(
+ 'invokables' => $plugins,
+ )));
+ }
+
+ /**
+ * An error occurred.
+ *
+ * @param \PHPUnit_Framework_Test $test
+ * @param \Exception $e
+ * @param float $time
+ */
+ public function addError(\PHPUnit_Framework_Test $test, \Exception $e, $time)
+ {
+
+ }
+
+ /**
+ * A failure occurred.
+ *
+ * @param \PHPUnit_Framework_Test $test
+ * @param \PHPUnit_Framework_AssertionFailedError $e
+ * @param float $time
+ */
+ public function addFailure(\PHPUnit_Framework_Test $test, \PHPUnit_Framework_AssertionFailedError $e, $time)
+ {
+
+ }
+
+ /**
+ * Incomplete test.
+ *
+ * @param \PHPUnit_Framework_Test $test
+ * @param \Exception $e
+ * @param float $time
+ */
+ public function addIncompleteTest(\PHPUnit_Framework_Test $test, \Exception $e, $time)
+ {
+
+ }
+
+ /**
+ * Skipped test.
+ *
+ * @param \PHPUnit_Framework_Test $test
+ * @param \Exception $e
+ * @param float $time
+ * @since Method available since Release 3.0.0
+ */
+ public function addSkippedTest(\PHPUnit_Framework_Test $test, \Exception $e, $time)
+ {
+
+ }
+
+ /**
+ * A test suite started.
+ *
+ * @param \PHPUnit_Framework_TestSuite $suite
+ * @since Method available since Release 2.2.0
+ */
+ public function startTestSuite(\PHPUnit_Framework_TestSuite $suite)
+ {
+
+ }
+
+ /**
+ * A test suite ended.
+ *
+ * @param \PHPUnit_Framework_TestSuite $suite
+ * @since Method available since Release 2.2.0
+ */
+ public function endTestSuite(\PHPUnit_Framework_TestSuite $suite)
+ {
+
+ }
+
+ /**
+ * A test started.
+ *
+ * @param \PHPUnit_Framework_Test $test
+ */
+ public function startTest(\PHPUnit_Framework_Test $test)
+ {
+ if ($test instanceof TestCaseInterface) {
+ $test->setPluginManager($this->pluginManager);
+ }
+ }
+
+ /**
+ * A test ended.
+ *
+ * @param \PHPUnit_Framework_Test $test
+ * @param float $time
+ */
+ public function endTest(\PHPUnit_Framework_Test $test, $time)
+ {
+ if ($test instanceof TestCaseInterface) {
+ foreach ($test->getPluginManager()->getRegisteredServices() as $serviceName => $plugins) {
+ if ($serviceName === 'instances') {
+ continue;
+ }
+ foreach ($plugins as $pluginName) {
+ $plugin = $test->getPluginManager()->get($pluginName);
+ if (method_exists($plugin, 'tearDown')) {
+ $plugin->tearDown();
+ }
+ }
+ }
+ }
+ }
+}
View
110 library/GeometriaLab/Test/PHPUnit/Plugin/PluginManager.php
@@ -0,0 +1,110 @@
+<?php
+
+namespace GeometriaLab\Test\PHPUnit\Plugin;
+
+use GeometriaLab\Test\PHPUnit\TestCaseInterface;
+
+use Zend\ServiceManager\AbstractPluginManager as ZendAbstractPluginManager,
+ Zend\ServiceManager\ConfigInterface as ZendConfigInterface;
+
+class PluginManager extends ZendAbstractPluginManager
+{
+ /**
+ * @var TestCaseInterface
+ */
+ protected $test;
+
+ /**
+ * Constructor
+ *
+ * After invoking parent constructor, add an initializer to inject the
+ * attached test, if any, to the currently requested plugin.
+ *
+ * @param null|ZendConfigInterface $configuration
+ */
+ public function __construct(ZendConfigInterface $configuration = null)
+ {
+ parent::__construct($configuration);
+
+ $this->addInitializer(array($this, 'injectTest'));
+ }
+
+ /**
+ * Retrieve a registered instance
+ *
+ * @param string $name
+ * @param mixed $options
+ * @param bool $usePeeringServiceManagers
+ * @return mixed
+ */
+ public function get($name, $options = array(), $usePeeringServiceManagers = true)
+ {
+ $plugin = parent::get($name, $options, $usePeeringServiceManagers);
+ $this->injectTest($plugin);
+
+ return $plugin;
+ }
+
+ /**
+ * Set test object
+ *
+ * @param TestCaseInterface $test
+ * @return PluginManager
+ */
+ public function setTest(TestCaseInterface $test)
+ {
+ $this->test = $test;
+
+ return $this;
+ }
+
+ /**
+ * Get test object
+ *
+ * @return TestCaseInterface
+ */
+ public function getTest()
+ {
+ return $this->test;
+ }
+
+ /**
+ * Inject a plugin instance with the registered test
+ *
+ * @param object $plugin
+ * @return void
+ */
+ public function injectTest($plugin)
+ {
+ if (!is_object($plugin)) {
+ return;
+ }
+ if (!method_exists($plugin, 'setTest')) {
+ return;
+ }
+
+ $test = $this->getTest();
+
+ $plugin->setTest($test);
+ }
+
+ /**
+ * Validate the plugin
+ *
+ * Any plugin is considered valid in this context.
+ *
+ * @param mixed $plugin
+ * @return void
+ * @throws Exception\InvalidPluginException
+ */
+ public function validatePlugin($plugin)
+ {
+ if (!$plugin instanceof PluginInterface) {
+ throw new Exception\InvalidPluginException(sprintf(
+ 'Plugin of type %s is invalid; must implement %s\Plugin\PluginInterface',
+ (is_object($plugin) ? get_class($plugin) : gettype($plugin)),
+ __NAMESPACE__
+ ));
+ }
+ }
+}
View
40 library/GeometriaLab/Test/PHPUnit/TestCaseInterface.php
@@ -0,0 +1,40 @@
+<?php
+
+namespace GeometriaLab\Test\PHPUnit;
+
+use GeometriaLab\Test\PHPUnit\Plugin\PluginManager;
+
+interface TestCaseInterface
+{
+ /**
+ * @param PluginManager $pluginManager
+ * @return TestCaseInterface
+ */
+ public function setPluginManager(PluginManager $pluginManager);
+
+ /**
+ * @return \GeometriaLab\Test\PHPUnit\Plugin\PluginManager
+ */
+ public function getPluginManager();
+
+ /**
+ * Get plugin instance
+ *
+ * @param string $name Name of plugin to return
+ * @param null|array $options Options to pass to plugin constructor (if not already instantiated)
+ * @return mixed
+ */
+ public function plugin($name, array $options = null);
+
+ /**
+ * Method overloading: return/call plugins
+ *
+ * If the plugin is a functor, call it, passing the parameters provided.
+ * Otherwise, return the plugin instance.
+ *
+ * @param string $method
+ * @param array $params
+ * @return mixed
+ */
+ public function __call($method, $params);
+}
Something went wrong with that request. Please try again.