<commit_before><?php
declare(strict_types=1);

namespace Fc2blog\Tests\App\Core\Cookie;

use Fc2blog\Web\Cookie;
use Fc2blog\Web\Request;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use TypeError;

class SetTest extends TestCase
{
  public function testSetString(): void
  {
    // Cookieの正しいテストはUnitTestでは困難です
    // echo '<?php setcookie("1","b");' | php-cgi
    // 等でテストができますが、php-cgiがない環境でテストが実行できません。
    $request = new Request();
    @Cookie::set($request, "k", "v");
    $this->assertTrue(true);
  }

  public function testSetInvalidType(): void
  {
    $request = new Request('GET', '/', null, null, null, null, ['HTTPS' => "on"]);
    try {
      /** @noinspection PhpStrictTypeCheckingInspection */
      Cookie::set($request, true, "v");
      $this->fail();
    } catch (TypeError $e) {
      $this->assertInstanceOf(TypeError::class, $e);
    }

    try {
      /** @noinspection PhpStrictTypeCheckingInspection */
      Cookie::set($request, 1, "v");
      $this->fail();
    } catch (TypeError $e) {
      $this->assertInstanceOf(TypeError::class, $e);
    }

    try {
      /** @noinspection PhpStrictTypeCheckingInspection */
      Cookie::set($request, "k", true);
      $this->fail();
    } catch (TypeError $e) {
      $this->assertInstanceOf(TypeError::class, $e);
    }

    try {
      /** @noinspection PhpStrictTypeCheckingInspection */
      Cookie::set($request, "k", 1);
      $this->fail();
    } catch (TypeError $e) {
      $this->assertInstanceOf(TypeError::class, $e);
    }
  }

  public function testDenyInvalidSamesite(): void
  {
    try {
      $request = new Request('GET', '/', null, null, null, null, ['HTTPS' => "on"]);
      @Cookie::set($request, "k", "v", time(), "", "", false, false, "Lax");
      $this->assertTrue(true);
    } catch (InvalidArgumentException $e) {
      $this->fail($e->getMessage());
    }

    try {
      $request = new Request('GET', '/', null, null, null, null, ['HTTPS' => "on"]);
      @Cookie::set($request, "k", "v", time(), "", "", false, false, "Strict");
      $this->assertTrue(true);
    } catch (InvalidArgumentException $e) {
      $this->fail($e->getMessage());
    }

    try {
      $request = new Request('GET', '/', null, null, null, null, ['HTTPS' => "on"]);
      @Cookie::set($request, "k", "v", time(), "", "", true, false, "None");
      $this->assertTrue(true);
    } catch (InvalidArgumentException $e) {
      $this->fail($e->getMessage());
    }

    try {
      $request = new Request();
      @Cookie::set($request, "k", "v", time(), "", "", false, false, "Wrong");
      $this->fail();
    } catch (InvalidArgumentException $e) {
      $this->assertTrue(true);
    }
  }

  public function testSamesiteNoneAndNotSecure(): void
  {
    try {
      $request = new Request();
      @Cookie::set($request, "k", "v", time(), "", "", true, false, "None");
      $this->fail();
    } catch (InvalidArgumentException $e) {
      $this->assertTrue(true);
    }

    try {
      $request = new Request('GET', '/', null, null, null, null, ['HTTPS' => "on"]);
      @Cookie::set($request, "k", "v", time(), "", "", false, false, "None");
      $this->fail();
    } catch (InvalidArgumentException $e) {
      $this->assertTrue(true);
    }

    try {
      $request = new Request('GET', '/', null, null, null, null, ['HTTPS' => "on"]);
      @Cookie::set($request, "k", "v", time(), "", "", true, false, "None");
    } catch (InvalidArgumentException $e) {
      $this->fail($e->getMessage());
    }<commit_msg>fix test: support strict error catch.<commit_after><?php
declare(strict_types=1);

namespace Fc2blog\Tests\App\Core\Cookie;

use ErrorException;
use Fc2blog\Web\Cookie;
use Fc2blog\Web\Request;
use InvalidArgumentException;
use PHPUnit\Framework\TestCase;
use TypeError;

class SetTest extends TestCase
{
  public function testSetString(): void
  {
    // Cookieの正しいテストはUnitTestでは困難です
    // echo '<?php setcookie("1","b");' | php-cgi
    // 等でテストができますが、php-cgiがない環境でテストが実行できません。
    // headerが送信されている状態でCookieをセットするため、エラーが発生しますが、例外に変換してキャッチしています。
    $request = new Request();
    try {
      @Cookie::set($request, "k", "v");
    } /** @noinspection PhpRedundantCatchClauseInspection */ catch(ErrorException $e){
    }
    $this->assertTrue(true);
  }

  public function testSetInvalidType(): void
  {
    $request = new Request('GET', '/', null, null, null, null, ['HTTPS' => "on"]);
    try {
      /** @noinspection PhpStrictTypeCheckingInspection */
      Cookie::set($request, true, "v");
      $this->fail();
    } catch (TypeError $e) {
      $this->assertInstanceOf(TypeError::class, $e);
    }

    try {
      /** @noinspection PhpStrictTypeCheckingInspection */
      Cookie::set($request, 1, "v");
      $this->fail();
    } catch (TypeError $e) {
      $this->assertInstanceOf(TypeError::class, $e);
    }

    try {
      /** @noinspection PhpStrictTypeCheckingInspection */
      Cookie::set($request, "k", true);
      $this->fail();
    } catch (TypeError $e) {
      $this->assertInstanceOf(TypeError::class, $e);
    }

    try {
      /** @noinspection PhpStrictTypeCheckingInspection */
      Cookie::set($request, "k", 1);
      $this->fail();
    } catch (TypeError $e) {
      $this->assertInstanceOf(TypeError::class, $e);
    }
  }

  public function testDenyInvalidSamesite(): void
  {
    try {
      $request = new Request('GET', '/', null, null, null, null, ['HTTPS' => "on"]);
      @Cookie::set($request, "k", "v", time(), "", "", false, false, "Lax");
    } catch (InvalidArgumentException $e) {
      $this->fail($e->getMessage());
    } /** @noinspection PhpRedundantCatchClauseInspection */ catch(ErrorException $e){
      $this->assertTrue(true);
    }

    try {
      $request = new Request('GET', '/', null, null, null, null, ['HTTPS' => "on"]);
      @Cookie::set($request, "k", "v", time(), "", "", false, false, "Strict");
    } catch (InvalidArgumentException $e) {
      $this->fail($e->getMessage());
    } /** @noinspection PhpRedundantCatchClauseInspection */ catch(ErrorException $e){
      $this->assertTrue(true);
    }

    try {
      $request = new Request('GET', '/', null, null, null, null, ['HTTPS' => "on"]);
      @Cookie::set($request, "k", "v", time(), "", "", true, false, "None");$this->assertTrue(true);
    } catch (InvalidArgumentException $e) {
      $this->fail($e->getMessage());
    } /** @noinspection PhpRedundantCatchClauseInspection */ catch(ErrorException $e){
      $this->assertTrue(true);
    }

    try {
      $request = new Request();
      @Cookie::set($request, "k", "v", time(), "", "", false, false, "Wrong");
      $this->fail();
    } catch (InvalidArgumentException $e) {
      $this->assertTrue(true);
    }
  }

  public function testSamesiteNoneAndNotSecure(): void
  {
    try {
      $request = new Request();
      @Cookie::set($request, "k", "v", time(), "", "", true, false, "None");
      $this->fail();
    } catch (InvalidArgumentException $e) {
      $this->assertTrue(true);
    }

    try {
      $request = new Request('GET', '/', null, null, null, null, ['HTTPS' => "on"]);
      @Cookie::set($request, "k", "v", time(), "", "", false, false, "None");
      $this->fail();
    } catch (InvalidArgumentException $e) {
      $this->assertTrue(true);
    }

    try {
      $request = new Request('GET', '/', null, null, null, null, ['HTTPS' => "on"]);
      @Cookie::set($request, "k", "v", time(), "", "", true, false, "None");
    } catch (InvalidArgumentException $e) {
      $this->fail($e->getMessage());
    } /** @noinspection PhpRedundantCatchClauseInspection */ catch(ErrorException $e){
      $this->assertTrue(true);
    }
{'fraction_non_alphanumeric': 0.20290044421217665, 'fraction_numerical': 0.0018291089626339169, 'mean_word_length': 3.2199558985667034, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 69, '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': '3498222', 'n_tokens_mistral': 2643, 'n_tokens_neox': 2571, 'n_words': 520}