Skip to content

Commit 704b68b

Browse files
committed
Add Operations/Find
1 parent 610536a commit 704b68b

File tree

5 files changed

+139
-0
lines changed

5 files changed

+139
-0
lines changed

composer.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
"psr/log": "^1.0",
2929
"spatie/image-optimizer": "^1.1",
3030
"symfony/filesystem": "^4.1",
31+
"symfony/finder": "^4.1",
3132
"wp-cli/wp-cli": "^2.0"
3233
},
3334
"require-dev": {

src/Operations/Find.php

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypistTech\ImageOptimizeCommand\Operations;
6+
7+
use Symfony\Component\Finder\Finder;
8+
use Symfony\Component\Finder\SplFileInfo;
9+
use TypistTech\ImageOptimizeCommand\LoggerInterface;
10+
use function WP_CLI\Utils\normalize_path;
11+
12+
class Find
13+
{
14+
/**
15+
* The finder.
16+
*
17+
* @var Finder
18+
*/
19+
protected $finder;
20+
21+
/**
22+
* The logger.
23+
*
24+
* @var LoggerInterface
25+
*/
26+
protected $logger;
27+
28+
/**
29+
* Find constructor.
30+
*
31+
* @param Finder $finder The finder.
32+
* @param LoggerInterface $logger The logger.
33+
*/
34+
public function __construct(Finder $finder, LoggerInterface $logger)
35+
{
36+
$this->finder = $finder;
37+
$this->logger = $logger;
38+
}
39+
40+
/**
41+
* Find files under a directory with specific extensions.
42+
*
43+
* @param string $directory Path to search.
44+
* @param string ...$extensions File extensions to search.
45+
*
46+
* @return string[]
47+
*/
48+
public function execute(string $directory, string ...$extensions): array
49+
{
50+
$directory = normalize_path($directory);
51+
$pattern = sprintf(
52+
'/\.(%1$s)$/',
53+
implode('|', $extensions)
54+
);
55+
56+
$files = $this->finder->files()
57+
->in($directory)
58+
->name($pattern);
59+
60+
return array_map(function (SplFileInfo $file): string {
61+
return $file->getRealPath();
62+
}, iterator_to_array($files));
63+
}
64+
}

tests/_data/images/find/bot.png

8.55 KB
Loading

tests/_data/images/find/deep/bot.png

8.55 KB
Loading

tests/unit/Operations/FindTest.php

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
<?php
2+
3+
declare(strict_types=1);
4+
5+
namespace TypistTech\ImageOptimizeCommand\Operations;
6+
7+
use Codeception\Module\Filesystem as FilesystemModule;
8+
use Codeception\Test\Unit;
9+
use Mockery;
10+
use Symfony\Component\Finder\Finder;
11+
use TypistTech\ImageOptimizeCommand\LoggerInterface;
12+
use WP_Mock;
13+
14+
class FindTest extends Unit
15+
{
16+
/**
17+
* @var \TypistTech\ImageOptimizeCommand\UnitTester
18+
*/
19+
protected $tester;
20+
21+
/**
22+
* @var FilesystemModule
23+
*/
24+
protected $filesystem;
25+
26+
/**
27+
* @var string
28+
*/
29+
protected $testDir;
30+
31+
public function testFind()
32+
{
33+
$finder = new Finder();
34+
$logger = Mockery::spy(LoggerInterface::class);
35+
$find = new Find($finder, $logger);
36+
37+
$actual = $find->execute($this->testDir, 'png', 'txt', 'original', 'xyz');
38+
39+
$expected = [
40+
$this->testDir . '/bot.png',
41+
$this->testDir . '/bot.png.original',
42+
$this->testDir . '/find/bot.png',
43+
$this->testDir . '/find/deep/bot.png',
44+
$this->testDir . '/restore-me.txt',
45+
$this->testDir . '/restore-me.txt.original',
46+
];
47+
sort($actual);
48+
49+
$this->assertSame($expected, $actual);
50+
}
51+
52+
protected function _before()
53+
{
54+
$this->filesystem = $this->getModule('Filesystem');
55+
$this->testDir = codecept_data_dir('tmp');
56+
57+
$this->filesystem->copyDir(
58+
codecept_data_dir('images'),
59+
$this->testDir
60+
);
61+
62+
WP_Mock::userFunction('WP_CLI\Utils\normalize_path')
63+
->with(Mockery::type('string'))
64+
->andReturnUsing(function ($arg) {
65+
return $arg;
66+
})
67+
->zeroOrMoreTimes();
68+
}
69+
70+
protected function _after()
71+
{
72+
$this->filesystem->deleteDir($this->testDir);
73+
}
74+
}

0 commit comments

Comments
 (0)