GuestAvatarControllerTest.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. namespace Core\Controller;
  3. use OC\Core\Controller\GuestAvatarController;
  4. use OCP\AppFramework\Http\FileDisplayResponse;
  5. use OCP\Files\SimpleFS\ISimpleFile;
  6. use OCP\IAvatar;
  7. use OCP\IAvatarManager;
  8. use OCP\IRequest;
  9. use Psr\Log\LoggerInterface;
  10. /**
  11. * This class provides tests for the guest avatar controller.
  12. */
  13. class GuestAvatarControllerTest extends \Test\TestCase {
  14. /**
  15. * @var GuestAvatarController
  16. */
  17. private $guestAvatarController;
  18. /**
  19. * @var IRequest|\PHPUnit\Framework\MockObject\MockObject
  20. */
  21. private $request;
  22. /**
  23. * @var IAvatarManager|\PHPUnit\Framework\MockObject\MockObject
  24. */
  25. private $avatarManager;
  26. /**
  27. * @var IAvatar|\PHPUnit\Framework\MockObject\MockObject
  28. */
  29. private $avatar;
  30. /**
  31. * @var \OCP\Files\File|\PHPUnit\Framework\MockObject\MockObject
  32. */
  33. private $file;
  34. /**
  35. * @var LoggerInterface|\PHPUnit\Framework\MockObject\MockObject
  36. */
  37. private $logger;
  38. /**
  39. * Sets up the test environment.
  40. */
  41. protected function setUp(): void {
  42. parent::setUp();
  43. $this->logger = $this->getMockBuilder(LoggerInterface::class)->getMock();
  44. $this->request = $this->getMockBuilder(IRequest::class)->getMock();
  45. $this->avatar = $this->getMockBuilder(IAvatar::class)->getMock();
  46. $this->avatarManager = $this->getMockBuilder(IAvatarManager::class)->getMock();
  47. $this->file = $this->getMockBuilder(ISimpleFile::class)->getMock();
  48. $this->file->method('getName')->willReturn('my name');
  49. $this->file->method('getMTime')->willReturn(42);
  50. $this->guestAvatarController = new GuestAvatarController(
  51. 'core',
  52. $this->request,
  53. $this->avatarManager,
  54. $this->logger
  55. );
  56. }
  57. /**
  58. * Tests getAvatar returns the guest avatar.
  59. */
  60. public function testGetAvatar() {
  61. $this->avatarManager->expects($this->once())
  62. ->method('getGuestAvatar')
  63. ->with('Peter')
  64. ->willReturn($this->avatar);
  65. $this->avatar->expects($this->once())
  66. ->method('getFile')
  67. ->with(512)
  68. ->willReturn($this->file);
  69. $this->file->method('getMimeType')
  70. ->willReturn('image/svg+xml');
  71. $response = $this->guestAvatarController->getAvatar('Peter', 128);
  72. $this->assertGreaterThanOrEqual(201, $response->getStatus());
  73. $this->assertInstanceOf(FileDisplayResponse::class, $response);
  74. }
  75. }