From a8ddf748a7a9060d52202e2af5d77a6c5c6a2cd5 Mon Sep 17 00:00:00 2001 From: Bernhard Posselt Date: Sun, 20 Sep 2015 18:18:36 +0200 Subject: fix import for integration tests --- tests/bootstrap.php | 13 +++ tests/classloader.php | 48 -------- tests/integration/bootstrap.php | 2 +- tests/unit/controller/UserApiControllerTest.php | 141 ++++++++++++++++++++++++ 4 files changed, 155 insertions(+), 49 deletions(-) create mode 100644 tests/bootstrap.php delete mode 100644 tests/classloader.php create mode 100644 tests/unit/controller/UserApiControllerTest.php (limited to 'tests') diff --git a/tests/bootstrap.php b/tests/bootstrap.php new file mode 100644 index 000000000..9149dd874 --- /dev/null +++ b/tests/bootstrap.php @@ -0,0 +1,13 @@ + + * @copyright Bernhard Posselt 2012, 2014 + */ + +require_once __DIR__ . '/../../../tests/bootstrap.php'; +require_once __DIR__ . '/../vendor/autoload.php'; diff --git a/tests/classloader.php b/tests/classloader.php deleted file mode 100644 index 122bc47fd..000000000 --- a/tests/classloader.php +++ /dev/null @@ -1,48 +0,0 @@ - - * @author Bernhard Posselt - * @copyright Alessandro Cosentino 2012 - * @copyright Bernhard Posselt 2012, 2014 - */ - -require_once __DIR__ . '/../vendor/autoload.php'; - -// to execute without owncloud, we need to create our own classloader -spl_autoload_register(function ($className){ - if (strpos($className, 'OCA\\') === 0) { - - $path = strtolower( - str_replace('\\', '/', substr($className, 3)) . '.php' - ); - $relPath = __DIR__ . '/../..' . $path; - - if(file_exists($relPath)){ - require_once $relPath; - } - } else if(strpos($className, 'OCP\\') === 0) { - $path = strtolower( - str_replace('\\', '/', substr($className, 3)) . '.php' - ); - $relPath = __DIR__ . '/../../../lib/public' . $path; - - if(file_exists($relPath)){ - require_once $relPath; - } - } else if(strpos($className, 'Test\\') === 0) { - $path = strtolower( - str_replace('\\', '/', substr($className, 4)) . '.php' - ); - echo $path; - $relPath = __DIR__ . '/../../../tests/lib' . $path; - - if(file_exists($relPath)){ - require_once $relPath; - } - } -}); \ No newline at end of file diff --git a/tests/integration/bootstrap.php b/tests/integration/bootstrap.php index fd41a0383..19a169a7e 100644 --- a/tests/integration/bootstrap.php +++ b/tests/integration/bootstrap.php @@ -11,7 +11,7 @@ namespace OCA\News\Tests\Integration; -require_once __DIR__ . '/../../../../tests/bootstrap.php'; +require_once __DIR__ . '/../bootstrap.php'; use PHPUnit_Framework_TestCase; use OCP\IDb; diff --git a/tests/unit/controller/UserApiControllerTest.php b/tests/unit/controller/UserApiControllerTest.php new file mode 100644 index 000000000..6c7ce7f04 --- /dev/null +++ b/tests/unit/controller/UserApiControllerTest.php @@ -0,0 +1,141 @@ + + * @author Bernhard Posselt + * @copyright Alessandro Cosentino 2012 + * @copyright Bernhard Posselt 2012, 2014 + */ + +namespace OCA\News\Controller; + + +class UserApiControllerTest extends \PHPUnit_Framework_TestCase { + + private $request; + private $appName; + private $rootFolder; + private $userSession; + private $controller; + private $user; + private $file; + + protected function setUp() { + $this->appName = 'news'; + $this->request = $this->getMockBuilder( + '\OCP\IRequest') + ->disableOriginalConstructor() + ->getMock(); + $this->rootFolder = $this->getMockBuilder( + '\OCP\Files\IRootFolder') + ->disableOriginalConstructor() + ->getMock(); + $this->file = $this->getMockBuilder( + '\OCP\Files\File') + ->disableOriginalConstructor() + ->getMock(); + $this->userSession = $this->getMockBuilder( + '\OCP\IUserSession') + ->disableOriginalConstructor() + ->getMock(); + $this->user = $this->getMockBuilder( + '\OCP\IUser') + ->disableOriginalConstructor() + ->getMock(); + $this->controller = new UserApiController( + $this->appName, $this->request, $this->userSession, + $this->rootFolder + ); + + + } + + private function expectUser($uid, $displayName, $lastLogin) { + $this->userSession->expects($this->any()) + ->method('getUser') + ->will($this->returnValue($this->user)); + $this->user->expects($this->any()) + ->method('getUID') + ->will($this->returnValue($uid)); + $this->user->expects($this->any()) + ->method('getLastLogin') + ->will($this->returnValue($lastLogin)); + $this->user->expects($this->any()) + ->method('getDisplayName') + ->will($this->returnValue($displayName)); + } + + private function expectImg($isJpg, $isPng, $user, $exists, $data) { + $jpg = '/' . $user . '/' . 'avatar.jpg'; + $png = '/' . $user . '/' . 'avatar.png'; + + $this->rootFolder->expects($this->any()) + ->method('nodeExists') + ->will($this->returnValueMap([ + [$jpg, $isJpg], + [$png, $isPng] + ])); + $this->rootFolder->expects($this->any()) + ->method('get') + ->will($this->returnValue($this->file)); + $this->file->expects($this->any()) + ->method('getContent') + ->will($this->returnValue($data)); + } + + public function testGetJpeg() { + $this->expectUser('john', 'John', 123); + $this->expectImg(true, false, 'john', true, 'hi'); + + $result = $this->controller->index(); + $expected = [ + 'userId' => 'john', + 'displayName' => 'John', + 'lastLoginTimestamp' => 123, + 'avatar' => [ + 'data' => base64_encode('hi'), + 'mime' => 'image/jpeg' + ] + ]; + + $this->assertEquals($expected, $result); + } + + public function testGetPng() { + $this->expectUser('john', 'John', 123); + $this->expectImg(false, true, 'john', false, 'hi'); + + $result = $this->controller->index(); + $expected = [ + 'userId' => 'john', + 'displayName' => 'John', + 'lastLoginTimestamp' => 123, + 'avatar' => [ + 'data' => base64_encode('hi'), + 'mime' => 'image/png' + ] + ]; + + $this->assertEquals($expected, $result); + } + + public function testNoAvatar() { + $this->expectUser('john', 'John', 123); + $this->expectImg(false, false, 'john', false, 'hi'); + + $result = $this->controller->index(); + $expected = [ + 'userId' => 'john', + 'displayName' => 'John', + 'lastLoginTimestamp' => 123, + 'avatar' => null + ]; + + $this->assertEquals($expected, $result); + } + +} -- cgit v1.2.3