BaseTest.php 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. <?php
  2. /**
  3. * Copyright © Magento, Inc. All rights reserved.
  4. * See COPYING.txt for license details.
  5. */
  6. namespace Magento\Framework\Logger\Test\Unit\Handler;
  7. class BaseTest extends \PHPUnit\Framework\TestCase
  8. {
  9. /**
  10. * @var \Magento\Framework\Logger\Handler\Base|\PHPUnit_Framework_MockObject_MockObject
  11. */
  12. private $model;
  13. /**
  14. * @var \ReflectionMethod
  15. */
  16. private $sanitizeMethod;
  17. protected function setUp()
  18. {
  19. $driverMock = $this->getMockBuilder(\Magento\Framework\Filesystem\DriverInterface::class)
  20. ->disableOriginalConstructor()
  21. ->getMock();
  22. $this->model = new \Magento\Framework\Logger\Handler\Base($driverMock);
  23. $class = new \ReflectionClass($this->model);
  24. $this->sanitizeMethod = $class->getMethod('sanitizeFileName');
  25. $this->sanitizeMethod->setAccessible(true);
  26. }
  27. public function testSanitizeEmpty()
  28. {
  29. $this->assertEquals('', $this->sanitizeMethod->invokeArgs($this->model, ['']));
  30. }
  31. public function testSanitizeSimpleFilename()
  32. {
  33. $this->assertEquals('custom.log', $this->sanitizeMethod->invokeArgs($this->model, ['custom.log']));
  34. }
  35. public function testSanitizeLeadingSlashFilename()
  36. {
  37. $this->assertEquals(
  38. 'customfolder/custom.log',
  39. $this->sanitizeMethod->invokeArgs($this->model, ['/customfolder/custom.log'])
  40. );
  41. }
  42. public function testSanitizeParentLevelFolder()
  43. {
  44. $this->assertEquals(
  45. 'var/hack/custom.log',
  46. $this->sanitizeMethod->invokeArgs($this->model, ['../../../var/hack/custom.log'])
  47. );
  48. }
  49. /**
  50. * @expectedException \InvalidArgumentException
  51. * @expectedExceptionMessage Filename expected to be a string
  52. */
  53. public function testSanitizeFileException()
  54. {
  55. $this->sanitizeMethod->invokeArgs($this->model, [['filename' => 'notValid']]);
  56. }
  57. }