<?php

namespace AlphaSnow\LaravelFilesystem\Aliyun\Tests;

use AlphaSnow\Flysystem\Aliyun\AliyunFactory;
use Illuminate\Support\Facades\Storage;
use OSS\OssClient;

class MacroTest extends TestCase
{
    protected $ossClient;

    protected function setUp(): void
    {
        parent::setUp();

        $client = \Mockery::mock(OssClient::class, ["access_id","access_secret","endpoint.com"]);
        $this->ossClient = $client;

        $factory = \Mockery::mock(AliyunFactory::class);
        $factory->makePartial()
            ->shouldReceive("createClient")
            ->andReturn($client);
        $this->app->instance(AliyunFactory::class, $factory);
    }

    /**
     * @test
     */
    public function append_file()
    {
        $file = __DIR__."/stubs/file.txt";

        $this->ossClient->shouldReceive("appendFile")
            ->with("bucket", "tests/stubs/file.txt", $file, 0, ["headers" => ["Content-Disposition" => "attachment;filename=file.txt"]])
            ->once()
            ->andReturn(7);
        $position = Storage::disk("oss")->appendFile("stubs/file.txt", $file, 0, ["headers" => ["Content-Disposition" => "attachment;filename=file.txt"]]);
        $this->assertSame($position, 7);
    }

    /**
     * @test
     */
    public function append_object()
    {
        $this->ossClient->shouldReceive("appendObject")
            ->with("bucket", "tests/stubs/file.txt", "contents", 0, ["checkmd5" => false])
            ->once()
            ->andReturn(8);
        $position = Storage::disk("oss")->appendObject("stubs/file.txt", "contents", 0, ["options" => ["checkmd5" => false]]);
        $this->assertSame($position, 8);
    }

    /**
     * @test
     */
    public function process_object()
    {
        $this->ossClient->shouldReceive("processObject")
            ->with("bucket", "tests/stubs/file.jpg", "image/resize,l_1000|sys/saveas,o_dGVzdHMvc3R1YnMvZmlsZS5qcGc,b_YnVja2V0", [])
            ->once()
            ->andReturn('{"bucket":"bucket","fileSize":1024,"object":"tests/stubs/file.jpg","status":"OK"}');
        $result = Storage::disk("oss")->processObject("stubs/file.jpg", "image/resize,l_1000");
        $this->assertSame($result, ["bucket"=>"bucket","fileSize"=>1024,"object"=>"tests/stubs/file.jpg","status"=>"OK"]);
    }
}
