GuestAvatarControllerTest.php 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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. }