<commit_before><?php
/** @noinspection PhpParamsInspection */

namespace BasicTablePackage\Controller;

use BasicTablePackage\TableViewService;
use BasicTablePackage\Test\DIContainerFactory;
use BasicTablePackage\View\TableView\Row;
use BasicTablePackage\View\TableView\TableView;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use function DI\factory;

class BasicTableControllerTest extends TestCase
{
    const TEST_1   = "test1";
    const TEST_2   = "test2";
    const TEST_3   = "test3";
    const TEST_4   = "test4";
    const HEADER_1 = "header1";
    const HEADER_2 = "header2";


    /**
     * @var MockObject
     */
    private $renderer;
    /**
     * @var MockObject
     */

    private $tableViewService;
    private $variableSetter;
    /**
     * @var BasicTableController
     */
    private $basicTableController;


    protected function setUp ()
    {
        $this->renderer = $this->createMock(Renderer::class);
        $this->tableViewService = $this->createMock(TableViewService::class);
        $this->variableSetter = $this->createMock(VariableSetter::class);

        $this->basicTableController =
            new BasicTableController($this->renderer, $this->tableViewService, $this->variableSetter, null);
    }


    public function test_renders_table_view_when_view_called ()
    {
        $this->renderer->expects($this->once())->method('render')->with(BasicTableController::TABLE_VIEW);

        $this->basicTableController->view();
    }

    public function test_renders_form_view_when_edit_action_called ()
    {
        $this->renderer->expects($this->once())->method('render')->with(BasicTableController::FORM_VIEW);

        $this->basicTableController->openForm(null);
    }

    public function test_sets_headers_and_rows_to_TableView_retrieved_from_TableViewService ()
    {
        $row1 = new Row([ self::TEST_1, self::TEST_2 ]);
        $row2 = new Row([ self::TEST_3, self::TEST_4 ]);
        $tableView = new TableView([ self::HEADER_1, self::HEADER_2 ], [ $row1, $row2 ]);


        /** @var Cont $container */
        $container = DIContainerFactory::createContainer();
        $container->set(TableViewService::class, factory(function () use (
            $tableView
        ) {
            return new MockTableViewService($tableView);
        }));


        ob_start();
        /**
         * @var $basicTableController BasicTableController
         */
        $basicTableController = $container->get(BasicTableController::class);
        $basicTableController->view();
        $output = ob_get_clean();

        $this->assertThat($output, $this->stringContains(self::HEADER_1));
        $this->assertThat($output, $this->stringContains(self::HEADER_2));
        $this->assertThat($output, $this->stringContains(self::TEST_1));
        $this->assertThat($output, $this->stringContains(self::TEST_2));
        $this->assertThat($output, $this->stringContains(self::TEST_3));
        $this->assertThat($output, $this->stringContains(self::TEST_4));
    }
}

class MockTableViewService extends TableViewService
{
    /**
     * @var TableView
     */
    private $tableView;

    /**
     * MockTableViewService constructor.
     * @param TableView $tableView
     */
    public function __construct (TableView $tableView)
    {
        $this->tableView = $tableView;
    }

    public function getTableView (): TableView
    {
        return $this->tableView;
    }
}<commit_msg>BasicTableControllerTest: always use mock for BasicTableService<commit_after><?php
/** @noinspection PhpParamsInspection */

namespace BasicTablePackage\Controller;

use BasicTablePackage\TableViewService;
use BasicTablePackage\Test\DIContainerFactory;
use BasicTablePackage\View\TableView\Row;
use BasicTablePackage\View\TableView\TableView;
use DI\Container;
use PHPUnit\Framework\MockObject\MockObject;
use PHPUnit\Framework\TestCase;
use function DI\value;

class BasicTableControllerTest extends TestCase
{
    const TEST_1   = "test1";
    const TEST_2   = "test2";
    const TEST_3   = "test3";
    const TEST_4   = "test4";
    const HEADER_1 = "header1";
    const HEADER_2 = "header2";


    /**
     * @var MockObject
     */
    private $renderer;
    /**
     * @var MockObject
     */

    private $tableViewService;
    private $variableSetter;
    /**
     * @var BasicTableController
     */
    private $basicTableController;
    private $container;


    protected function setUp ()
    {
        $this->renderer = $this->createMock(Renderer::class);
        $this->tableViewService = $this->createMock(TableViewService::class);
        $this->variableSetter = $this->createMock(VariableSetter::class);

        $this->basicTableController =
            new BasicTableController($this->renderer, $this->tableViewService, $this->variableSetter, null);

        /** @var Container $container */
        $this->container = DIContainerFactory::createContainer();
    }


    public function test_renders_table_view_when_view_called ()
    {
        $this->renderer->expects($this->once())->method('render')->with(BasicTableController::TABLE_VIEW);

        $this->basicTableController->view();
    }

    public function test_renders_form_view_when_edit_action_called ()
    {
        $this->renderer->expects($this->once())->method('render')->with(BasicTableController::FORM_VIEW);

        $this->basicTableController->openForm(null);
    }

    public function test_sets_headers_and_rows_to_TableView_retrieved_from_TableViewService ()
    {
        $row1 = new Row([ self::TEST_1, self::TEST_2 ]);
        $row2 = new Row([ self::TEST_3, self::TEST_4 ]);
        $tableView = new TableView([ self::HEADER_1, self::HEADER_2 ], [ $row1, $row2 ]);

        $this->tableViewService->expects($this->once())->method('getTableView')->willReturn($tableView);

        $this->container->set(TableViewService::class, value($this->tableViewService));


        ob_start();
        /**
         * @var $basicTableController BasicTableController
         */
        $basicTableController = $this->container->get(BasicTableController::class);
        $basicTableController->view();

        $output = ob_get_clean();
        $this->assertThat($output, $this->stringContains(self::HEADER_1));
        $this->assertThat($output, $this->stringContains(self::HEADER_2));
        $this->assertThat($output, $this->stringContains(self::TEST_1));
        $this->assertThat($output, $this->stringContains(self::TEST_2));
        $this->assertThat($output, $this->stringContains(self::TEST_3));
        $this->assertThat($output, $this->stringContains(self::TEST_4));
    }
}

{'fraction_non_alphanumeric': 0.1390019598974823, 'fraction_numerical': 0.00844263530830695, 'mean_word_length': 3.479405806887238, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 92, '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': '3391932', 'n_tokens_mistral': 1948, 'n_tokens_neox': 1920, 'n_words': 299}