test_helpers-1.0.0-alpha6/tests/modules/test_helpers_example/tests/src/Unit/TestHelpersExampleControllerModernResultTest.php

tests/modules/test_helpers_example/tests/src/Unit/TestHelpersExampleControllerModernResultTest.php
<?php

declare(strict_types=1);

namespace Drupal\Tests\test_helpers_example\Unit;

use Drupal\Tests\UnitTestCase;
use Drupal\test_helpers\TestHelpers;
use Drupal\test_helpers_example\Controller\TestHelpersExampleController;
use PHPUnit\Framework\Attributes\CoversClass;
use PHPUnit\Framework\Attributes\CoversMethod;
use PHPUnit\Framework\Attributes\Group;

/**
 * Tests TestHelpersExampleController with Test Helpers API to check the result.
 */
#[CoversClass(TestHelpersExampleController::class)]
#[Group('test_helpers_example')]
#[CoversMethod(TestHelpersExampleController::class, '__construct')]
#[CoversMethod(TestHelpersExampleController::class, 'create')]
#[CoversMethod(TestHelpersExampleController::class, 'articlesList')]
class TestHelpersExampleControllerModernResultTest extends UnitTestCase {

  /**
   * Tests the articlesList() method using Test Helpers API.
   */
  public function testArticlesList() {
    TestHelpers::service('config.factory')->stubSetConfig('test_helpers_example.settings', ['articles_to_display' => 1]);
    TestHelpers::service('date.formatter')->stubSetFormat('medium', 'Medium', 'd.m.Y');
    TestHelpers::saveEntity('user', ['name' => 'Alice']);
    // Putting coding standards ignore flag to suppress warnings until the
    // https://www.drupal.org/project/coder/issues/3185082 is fixed.
    // @codingStandardsIgnoreStart
    TestHelpers::saveEntity('node', ['type' => 'article', 'title' => 'A1', 'status' => 1, 'uid' => 1, 'created' => 1672574400]);
    TestHelpers::saveEntity('node', ['type' => 'article', 'title' => 'A2', 'status' => 1, 'uid' => 1, 'created' => 1672660800]);
    TestHelpers::saveEntity('node', ['type' => 'page', 'title' => 'P1', 'status' => 1, 'uid' => 1, 'created' => 1672747200]);
    TestHelpers::saveEntity('node', ['type' => 'article', 'title' => 'A3', 'status' => 0, 'uid' => 1, 'created' => 1672833600]);
    // @codingStandardsIgnoreEnd

    $result = TestHelpers::createClass(TestHelpersExampleController::class)->articlesList();
    $this->assertCount(1, $result['#items']);
    $this->assertEquals('A2 (02.01.2023 by Alice)', $result['#items'][0]->getText());
    $this->assertContains('node_list:article', $result['#cache']['tags']);
  }

}

Главная | Обратная связь

drupal hosting | друпал хостинг | it patrol .inc