Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
DeleteExpiredImagesTest.php
Go to the documentation of this file.
1 <?php
7 
8 class DeleteExpiredImagesTest extends \PHPUnit\Framework\TestCase
9 {
15  protected $_helper;
16 
22  protected $_adminHelper;
23 
27  protected $_filesystem;
28 
32  protected $_storeManager;
33 
38 
42  protected $_directory;
43 
47  public static $currentTime;
48 
52  protected function setUp()
53  {
54  $this->_helper = $this->createMock(\Magento\Captcha\Helper\Data::class);
55  $this->_adminHelper = $this->createMock(\Magento\Captcha\Helper\Adminhtml\Data::class);
56  $this->_filesystem = $this->createMock(\Magento\Framework\Filesystem::class);
57  $this->_directory = $this->createMock(\Magento\Framework\Filesystem\Directory\Write::class);
58  $this->_storeManager = $this->createMock(\Magento\Store\Model\StoreManager::class);
59 
60  $this->_filesystem->expects(
61  $this->once()
62  )->method(
63  'getDirectoryWrite'
64  )->will(
65  $this->returnValue($this->_directory)
66  );
67 
68  $this->_deleteExpiredImages = new \Magento\Captcha\Cron\DeleteExpiredImages(
69  $this->_helper,
70  $this->_adminHelper,
71  $this->_filesystem,
72  $this->_storeManager
73  );
74  }
75 
79  public function testDeleteExpiredImages($website, $isFile, $filename, $mTime, $timeout)
80  {
81  $this->_storeManager->expects(
82  $this->once()
83  )->method(
84  'getWebsites'
85  )->will(
86  $this->returnValue(isset($website) ? [$website] : [])
87  );
88  if (isset($website)) {
89  $this->_helper->expects(
90  $this->once()
91  )->method(
92  'getConfig'
93  )->with(
94  $this->equalTo('timeout'),
95  new \PHPUnit\Framework\Constraint\IsIdentical($website->getDefaultStore())
96  )->will(
97  $this->returnValue($timeout)
98  );
99  } else {
100  $this->_helper->expects($this->never())->method('getConfig');
101  }
102  $this->_adminHelper->expects(
103  $this->once()
104  )->method(
105  'getConfig'
106  )->with(
107  $this->equalTo('timeout'),
108  new \PHPUnit\Framework\Constraint\IsNull()
109  )->will(
110  $this->returnValue($timeout)
111  );
112 
113  $timesToCall = isset($website) ? 2 : 1;
114  $this->_directory->expects(
115  $this->exactly($timesToCall)
116  )->method(
117  'read'
118  )->will(
119  $this->returnValue([$filename])
120  );
121  $this->_directory->expects($this->exactly($timesToCall))->method('isFile')->will($this->returnValue($isFile));
122  $this->_directory->expects($this->any())->method('stat')->will($this->returnValue(['mtime' => $mTime]));
123 
124  $this->_deleteExpiredImages->execute();
125  }
126 
130  public function getExpiredImages()
131  {
132  $website = $this->createPartialMock(\Magento\Store\Model\Website::class, ['__wakeup', 'getDefaultStore']);
133  $store = $this->createPartialMock(\Magento\Store\Model\Store::class, ['__wakeup']);
134  $website->expects($this->any())->method('getDefaultStore')->will($this->returnValue($store));
135  $time = time();
136  return [
137  [null, true, 'test.png', 50, ($time - 60) / 60, true],
138  [$website, false, 'test.png', 50, ($time - 60) / 60, false],
139  [$website, true, 'test.jpg', 50, ($time - 60) / 60, false],
140  [$website, true, 'test.png', 50, ($time - 20) / 60, false]
141  ];
142  }
143 }
144 
150 function time()
151 {
154  }
156 }
testDeleteExpiredImages($website, $isFile, $filename, $mTime, $timeout)