|
| 1 | +<?php |
| 2 | +/** |
| 3 | + * Copyright © Magento, Inc. All rights reserved. |
| 4 | + * See COPYING.txt for license details. |
| 5 | + */ |
| 6 | +declare(strict_types=1); |
| 7 | + |
| 8 | +namespace Magento\Analytics\Cron; |
| 9 | + |
| 10 | +use Magento\Framework\App\Filesystem\DirectoryList; |
| 11 | +use Magento\Framework\Filesystem; |
| 12 | +use Magento\Framework\Filesystem\Directory\WriteInterface; |
| 13 | +use Magento\Framework\ObjectManagerInterface; |
| 14 | +use Magento\TestFramework\Helper\Bootstrap; |
| 15 | +use PHPUnit\Framework\TestCase; |
| 16 | + |
| 17 | +/** |
| 18 | + * Checks data collection process behaviour |
| 19 | + * |
| 20 | + * @see \Magento\Analytics\Cron\CollectData |
| 21 | + * |
| 22 | + * @magentoAppArea adminhtml |
| 23 | + */ |
| 24 | +class CollectDataTest extends TestCase |
| 25 | +{ |
| 26 | + /** @var ObjectManagerInterface */ |
| 27 | + private $objectManager; |
| 28 | + |
| 29 | + /** @var CollectData */ |
| 30 | + private $collectDataService; |
| 31 | + |
| 32 | + /** @var WriteInterface */ |
| 33 | + private $mediaDirectory; |
| 34 | + |
| 35 | + /** |
| 36 | + * @inheritdoc |
| 37 | + */ |
| 38 | + protected function setUp(): void |
| 39 | + { |
| 40 | + parent::setUp(); |
| 41 | + |
| 42 | + $this->objectManager = Bootstrap::getObjectManager(); |
| 43 | + $this->collectDataService = $this->objectManager->get(CollectData::class); |
| 44 | + $this->mediaDirectory = $this->objectManager->get(Filesystem::class)->getDirectoryWrite(DirectoryList::MEDIA); |
| 45 | + $this->removeAnalyticsDirectory(); |
| 46 | + } |
| 47 | + |
| 48 | + /** |
| 49 | + * @inheritdoc |
| 50 | + */ |
| 51 | + protected function tearDown(): void |
| 52 | + { |
| 53 | + $this->removeAnalyticsDirectory(); |
| 54 | + |
| 55 | + parent::tearDown(); |
| 56 | + } |
| 57 | + |
| 58 | + /** |
| 59 | + * @magentoConfigFixture current_store analytics/subscription/enabled 1 |
| 60 | + * @magentoConfigFixture default/analytics/general/token 123 |
| 61 | + * |
| 62 | + * @return void |
| 63 | + */ |
| 64 | + public function testExecute(): void |
| 65 | + { |
| 66 | + $this->collectDataService->execute(); |
| 67 | + $this->assertTrue( |
| 68 | + $this->mediaDirectory->isDirectory('analytics'), |
| 69 | + 'Analytics was not created' |
| 70 | + ); |
| 71 | + $files = $this->mediaDirectory->getDriver() |
| 72 | + ->readDirectoryRecursively($this->mediaDirectory->getAbsolutePath('analytics')); |
| 73 | + $file = array_filter($files, function ($element) { |
| 74 | + return substr($element, -8) === 'data.tgz'; |
| 75 | + }); |
| 76 | + $this->assertNotEmpty($file, 'File was not created'); |
| 77 | + } |
| 78 | + |
| 79 | + /** |
| 80 | + * Remove Analytics directory |
| 81 | + * |
| 82 | + * @return void |
| 83 | + */ |
| 84 | + private function removeAnalyticsDirectory(): void |
| 85 | + { |
| 86 | + $directoryToRemove = $this->mediaDirectory->getAbsolutePath('analytics'); |
| 87 | + if ($this->mediaDirectory->isDirectory($directoryToRemove)) { |
| 88 | + $this->mediaDirectory->delete($directoryToRemove); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments