|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types = 1); |
| 4 | +/** |
| 5 | + * @copyright 2024 Reno Reckling <e-github@wthack.de> |
| 6 | + * @license GNU AGPL version 3 or any later version |
| 7 | + * |
| 8 | + * This program is free software: you can redistribute it and/or modify |
| 9 | + * it under the terms of the GNU Affero General Public License as |
| 10 | + * published by the Free Software Foundation, either version 3 of the |
| 11 | + * License, or (at your option) any later version. |
| 12 | + * |
| 13 | + * This program is distributed in the hope that it will be useful, |
| 14 | + * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | + * GNU Affero General Public License for more details. |
| 17 | + * |
| 18 | + * You should have received a copy of the GNU Affero General Public License |
| 19 | + * along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 20 | + * |
| 21 | + */ |
| 22 | + |
| 23 | +namespace Test; |
| 24 | + |
| 25 | +use OCP\ICache; |
| 26 | +use OCP\IConfig; |
| 27 | +use OCP\ICacheFactory; |
| 28 | +use OC\BinaryFinder; |
| 29 | + |
| 30 | +class BinaryFinderTest extends TestCase { |
| 31 | + private ICache $cache; |
| 32 | + private array $cacheMap; |
| 33 | + private ICacheFactory $cacheFactory; |
| 34 | + |
| 35 | + protected function setUp(): void { |
| 36 | + $this->cacheMap = []; |
| 37 | + $this->cacheFactory = $this->createMock(ICacheFactory::class); |
| 38 | + $this->cache = $this->createMock(ICache::class); |
| 39 | + $this->cache->method('get')->will($this->returnCallback(function($key) { |
| 40 | + return array_key_exists($key, $this->cacheMap) ? $this->cacheMap[$key] : null; |
| 41 | + })); |
| 42 | + $this->cache->method('set')->will($this->returnCallback(function($key, $val, $expires) { |
| 43 | + $this->cacheMap[$key] = $val; |
| 44 | + })); |
| 45 | + $this->cacheFactory->method('createLocal')->with('findBinaryPath')->willReturn($this->cache); |
| 46 | + |
| 47 | + } |
| 48 | + |
| 49 | + public function testDefaultFindsCat() { |
| 50 | + $config = $this->createMock(IConfig::class); |
| 51 | + $config |
| 52 | + ->method('getSystemValue') |
| 53 | + ->with('binary_search_paths', $this->anything()) |
| 54 | + ->will($this->returnCallback(function($key, $default) { |
| 55 | + return $default; |
| 56 | + })); |
| 57 | + $finder = new BinaryFinder($this->cacheFactory, $config); |
| 58 | + $this->assertEquals($finder->findBinaryPath('cat'), '/usr/bin/cat'); |
| 59 | + } |
| 60 | + |
| 61 | + public function testDefaultDoesNotFindCata() { |
| 62 | + $config = $this->createMock(IConfig::class); |
| 63 | + $config |
| 64 | + ->method('getSystemValue') |
| 65 | + ->with('binary_search_paths', $this->anything()) |
| 66 | + ->will($this->returnCallback(function($key, $default) { |
| 67 | + return $default; |
| 68 | + })); |
| 69 | + $finder = new BinaryFinder($this->cacheFactory, $config); |
| 70 | + $this->assertFalse($finder->findBinaryPath('cata')); |
| 71 | + } |
| 72 | + |
| 73 | + public function testCustomPathFindsCat() { |
| 74 | + $config = $this->createMock(IConfig::class); |
| 75 | + $config |
| 76 | + ->method('getSystemValue') |
| 77 | + ->with('binary_search_paths', $this->anything()) |
| 78 | + ->willReturn(['/usr/bin']); |
| 79 | + $finder = new BinaryFinder($this->cacheFactory, $config); |
| 80 | + $this->assertEquals($finder->findBinaryPath('cat'), '/usr/bin/cat'); |
| 81 | + } |
| 82 | + |
| 83 | + public function testWrongCustomPathDoesNotFindCat() { |
| 84 | + $config = $this->createMock(IConfig::class); |
| 85 | + $config |
| 86 | + ->method('getSystemValue') |
| 87 | + ->with('binary_search_paths') |
| 88 | + ->willReturn(['/wrong']); |
| 89 | + $finder = new BinaryFinder($this->cacheFactory, $config); |
| 90 | + $this->assertFalse($finder->findBinaryPath('cats')); |
| 91 | + } |
| 92 | +} |
0 commit comments