<?php
/*
 * Copyright 2024 Cloud Creativity Limited
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

declare(strict_types=1);

namespace CloudCreativity\LaravelJsonApi\Tests\Unit\Http\Headers;

use CloudCreativity\LaravelJsonApi\Http\Headers\MediaTypeParser;
use Neomerx\JsonApi\Factories\Factory;
use Neomerx\JsonApi\Http\Headers\HeaderParametersParser;
use Neomerx\JsonApi\Http\Headers\MediaType;
use PHPUnit\Framework\TestCase;

class MediaTypeTest extends TestCase
{

    /**
     * @return void
     * @see https://github.com/neomerx/json-api/issues/221
     */
    public function testIssue221(): void
    {
        $type1 = new MediaType('multipart', 'form-data', ['boundary' => '*']);
        $type2 = new MediaType('multipart', 'form-data', ['boundary' => '----WebKitFormBoundaryAAA']);

        $this->assertFalse($type1->matchesTo($type2));
        $this->assertTrue($type2->matchesTo($type1));
    }

    public function testIssue221ViaParser(): void
    {
        $parser = new MediaTypeParser(new HeaderParametersParser(new Factory()));

        $type1 = $parser->parse('multipart/form-data; boundary=*');
        $type2 = $parser->parse('multipart/form-data; boundary=----WebKitFormBoundary7MA4YWxkTrZu0gW');

        $this->assertFalse($type1->matchesTo($type2));
        $this->assertTrue($type2->matchesTo($type1));
    }
}
