<commit_before><?php

namespace Keboola\Test\Backend\FileWorkspace;

use Keboola\StorageApi\Workspaces;
use Keboola\Test\Backend\FileWorkspace\Backend\Abs;
use Keboola\Test\Backend\Workspaces\WorkspacesTestCase;
use MicrosoftAzure\Storage\Common\Exceptions\ServiceException;

class WorkspacesTest extends WorkspacesTestCase
{
    public function testWorkspaceCreate()
    {
        $workspaces = new Workspaces($this->_client);

        $runId = $this->_client->generateRunId();
        $this->_client->setRunId($runId);

        $backend = $this->resolveFileWorkspaceBackend();

        $workspace = $workspaces->createWorkspace([
            'backend' => $backend,
        ]);

        $this->assertEquals($backend, $workspace['connection']['backend']);

        $connection = $workspace['connection'];
        $backend = new Abs($workspace['connection']);
        $this->assertCount(0, $backend->listFiles());

        $fileName = $backend->uploadTestingFile();

        $files = $backend->listFiles();
        $this->assertCount(1, $files);
        $this->assertEquals($files[0]->getName(), $fileName);

        // get workspace
        $workspace = $workspaces->getWorkspace($workspace['id']);
        $this->assertArrayNotHasKey('password', $workspace['connection']);

        // list workspaces
        $workspacesIds = array_map(function ($workspace) {
            return $workspace['id'];
        }, $workspaces->listWorkspaces());

        $this->assertArrayHasKey($workspace['id'], array_flip($workspacesIds));

        $workspaces->deleteWorkspace($workspace['id']);

        // block until async events are processed, processing in order is not guaranteed but it should work most of time
        $this->createAndWaitForEvent((new \Keboola\StorageApi\Event())->setComponent('dummy')->setMessage('dummy'));

        $events = $this->_client->listEvents([
            'runId' => $runId,
        ]);

        $workspaceCreatedEvent = array_pop($events);
        $this->assertSame($runId, $workspaceCreatedEvent['runId']);
        $this->assertSame('storage.workspaceCreated', $workspaceCreatedEvent['event']);
        $this->assertSame('storage', $workspaceCreatedEvent['component']);

        $workspaceDeletedEvent = array_pop($events);
        $this->assertSame($runId, $workspaceDeletedEvent['runId']);
        $this->assertSame('storage.workspaceDeleted', $workspaceDeletedEvent['event']);
        $this->assertSame('storage', $workspaceDeletedEvent['component']);
        $backend = new Abs($connection);
        $this->expectException(ServiceException::class);
        $this->expectExceptionMessage('The specified container does not exist');
        $backend->listFiles();
    }

    public function testWorkspacePasswordReset()
    {
        $this->markTestIncomplete('TODO add test');
    }

    /**
     * @dataProvider  dropOptions
     * @param $dropOptions
     */
    public function testDropWorkspace($dropOptions)
    {
        $workspaces = new Workspaces($this->_client);

        $backend = $this->resolveFileWorkspaceBackend();
        $workspace = $workspaces->createWorkspace([
            'backend' => $backend,
        ]);
        $backend = new Abs($workspace['connection']);
        // sync delete
        $workspaces->deleteWorkspace($workspace['id'], $dropOptions);
        try {
            $backend->listFiles();
        } catch (ServiceException $e) {
            $this->assertEquals(404, $e->getCode(), $e->getMessage());
        }

        if (!empty($dropOptions['async'])) {
            $job = $this->_client->listJobs()[0];
            $this->assertEquals('workspaceDrop', $job['operationName']);
            $this->assertEquals($workspace['id'], $job['operationParams']['workspaceId']);
        }
    }

    /**
     * @dataProvider dropOptions
     * @param $dropOptions
     */
    public function testDropNonExistingWorkspace($dropOptions)
    {
        $this->markTestIncomplete('TODO add test');
    }

    public function dropOptions()
    {
        return [
            [
                [],
            ],
            [
                [
                    'async' => true,
                ],
            ],
        ];
    }

    /**
     * @return string
     */
    private function resolveFileWorkspaceBackend()
    {
        $tokenInfo = $this->_client->verifyToken();<commit_msg>remove testDropNonExistingWorkspace from FileWorkspacesTest its tested in ordinary workspace test<commit_after><?php

namespace Keboola\Test\Backend\FileWorkspace;

use Keboola\StorageApi\ClientException;
use Keboola\StorageApi\Workspaces;
use Keboola\Test\Backend\FileWorkspace\Backend\Abs;
use Keboola\Test\Backend\Workspaces\WorkspacesTestCase;
use MicrosoftAzure\Storage\Common\Exceptions\ServiceException;

class WorkspacesTest extends WorkspacesTestCase
{
    public function testWorkspaceCreate()
    {
        $workspaces = new Workspaces($this->_client);

        $runId = $this->_client->generateRunId();
        $this->_client->setRunId($runId);

        $backend = $this->resolveFileWorkspaceBackend();

        $workspace = $workspaces->createWorkspace([
            'backend' => $backend,
        ]);

        $this->assertEquals($backend, $workspace['connection']['backend']);

        $connection = $workspace['connection'];
        $backend = new Abs($workspace['connection']);
        $this->assertCount(0, $backend->listFiles());

        $fileName = $backend->uploadTestingFile();

        $files = $backend->listFiles();
        $this->assertCount(1, $files);
        $this->assertEquals($files[0]->getName(), $fileName);

        // get workspace
        $workspace = $workspaces->getWorkspace($workspace['id']);
        $this->assertArrayNotHasKey('password', $workspace['connection']);

        // list workspaces
        $workspacesIds = array_map(function ($workspace) {
            return $workspace['id'];
        }, $workspaces->listWorkspaces());

        $this->assertArrayHasKey($workspace['id'], array_flip($workspacesIds));

        $workspaces->deleteWorkspace($workspace['id']);

        // block until async events are processed, processing in order is not guaranteed but it should work most of time
        $this->createAndWaitForEvent((new \Keboola\StorageApi\Event())->setComponent('dummy')->setMessage('dummy'));

        $events = $this->_client->listEvents([
            'runId' => $runId,
        ]);

        $workspaceCreatedEvent = array_pop($events);
        $this->assertSame($runId, $workspaceCreatedEvent['runId']);
        $this->assertSame('storage.workspaceCreated', $workspaceCreatedEvent['event']);
        $this->assertSame('storage', $workspaceCreatedEvent['component']);

        $workspaceDeletedEvent = array_pop($events);
        $this->assertSame($runId, $workspaceDeletedEvent['runId']);
        $this->assertSame('storage.workspaceDeleted', $workspaceDeletedEvent['event']);
        $this->assertSame('storage', $workspaceDeletedEvent['component']);
        $backend = new Abs($connection);
        $this->expectException(ServiceException::class);
        $this->expectExceptionMessage('The specified container does not exist');
        $backend->listFiles();
    }

    public function testWorkspacePasswordReset()
    {
        $this->markTestIncomplete('TODO add test');
    }

    /**
     * @dataProvider  dropOptions
     * @param $dropOptions
     */
    public function testDropWorkspace($dropOptions)
    {
        $workspaces = new Workspaces($this->_client);

        $backend = $this->resolveFileWorkspaceBackend();
        $workspace = $workspaces->createWorkspace([
            'backend' => $backend,
        ]);
        $backend = new Abs($workspace['connection']);
        // sync delete
        $workspaces->deleteWorkspace($workspace['id'], $dropOptions);
        try {
            $backend->listFiles();
        } catch (ServiceException $e) {
            $this->assertEquals(404, $e->getCode(), $e->getMessage());
        }

        if (!empty($dropOptions['async'])) {
            $job = $this->_client->listJobs()[0];
            $this->assertEquals('workspaceDrop', $job['operationName']);
            $this->assertEquals($workspace['id'], $job['operationParams']['workspaceId']);
        }
    }

    public function dropOptions()
    {
        return [
            [
                [],
            ],
            [
                [
                    'async' => true,
                ],
            ],
        ];
    }

    /**
     * @return string
     */
    private function resolveFileWorkspaceBackend()
    {
        $tokenInfo = $this->_client->verifyToken();
{'fraction_non_alphanumeric': 0.15119795301232844, 'fraction_numerical': 0.0016282856478250756, 'mean_word_length': 2.8099246787771377, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 110, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1361905', 'n_tokens_mistral': 2318, 'n_tokens_neox': 2283, 'n_words': 368}