<?php

namespace Test\Michel\FileSystem;

use Michel\FileSystem\Tools\FileExplorer;
use Michel\UniTester\TestCase;

class FileExplorerTest extends TestCase
{

    protected function setUp(): void
    {
        // TODO: Implement setUp() method.
    }

    protected function tearDown(): void
    {
        // TODO: Implement tearDown() method.
    }

    protected function execute(): void
    {
        $this->testListAll();
        $this->testSearchByPattern();
        $this->testSearchByExtension();
    }

    public function testListAll()
    {
        $explorer = new FileExplorer(__DIR__ . '/resources/syncsource');

        $files = $explorer->listAll(true);
        $this->assertCount(3, $files);

        $foldersCount = 0;
        $filesCount = 0;
        foreach ($files as $file) {
            if ($file['is_directory']) {
                $foldersCount++;
                $this->assertTrue(is_array($file['files']));
                $this->assertCount(2, $file['files']);
            }else{
                $filesCount++;
            }
            $this->assertArrayHasKey('path', $file);
            $this->assertArrayHasKey('name', $file);
            $this->assertArrayHasKey('is_directory', $file);
            $this->assertArrayHasKey('size', $file);
            $this->assertArrayHasKey('modified_time', $file);
        }

        $this->assertEquals(1, $foldersCount);
        $this->assertEquals(2, $filesCount);

    }

    public function testSearchByPattern()
    {
        $explorer = new FileExplorer(__DIR__ . '/resources');
        $files = $explorer->searchByPattern('*.html', true);

        $this->assertCount(2, $files);

        $files = $explorer->searchByPattern('*.txt', true);

        $this->assertCount(4, $files);

        $files = $explorer->searchByPattern('*.txt');

        $this->assertCount(2, $files);

    }

    public function testSearchByExtension()
    {
        $explorer = new FileExplorer(__DIR__ . '/resources');
        $files = $explorer->searchByExtension('html', true);
        $this->assertCount(2, $files);
    }

}
