<commit_before><?php

declare(strict_types=1);

namespace XtreamwayzTest\HTMLFormValidator;

use Psr\Http\Message\ServerRequestInterface;
use Xtreamwayz\HTMLFormValidator\FormFactory;
use Xtreamwayz\HTMLFormValidator\ValidationResult;

class FormFactoryTest extends \PHPUnit_Framework_TestCase
{
    private $rawValues = [
        'foo' => 'bar',
        'baz' => ' qux ',
    ];

    private $values = [
        'foo' => 'bar',
        'baz' => 'qux',
    ];

    private $messages = [
        'foo' => [
            'regexNotMatch' => 'The input does not match against pattern \'/^\d+$/\'',
        ],
    ];

    public function testPsrPostRequestIsValid()
    {
        $htmlForm = '
            <form action="/" method="post">
                <input type="text" name="foo" />
                <input type="text" name="baz" data-filters="stringtrim" />
            </form>';

        $form = FormFactory::fromHtml($htmlForm);
        $request = $this->prophesize(ServerRequestInterface::class);
        $request->getMethod()->willReturn('POST');
        $request->getParsedBody()->willReturn($this->rawValues);

        $result = $form->validateRequest($request->reveal());

        self::assertInstanceOf(ValidationResult::class, $result);
        self::assertEquals($this->rawValues, $result->getRawValues());
        self::assertEquals($this->values, $result->getValues());
        self::assertEquals([], $result->getMessages());
        self::assertTrue($result->isValid());
    }

    public function testPsrGetRequestIsNotValid()
    {
        $htmlForm = '
            <form action="/" method="post">
                <input type="text" name="foo" />
                <input type="text" name="baz" data-filters="stringtrim" />
            </form>';

        $form = FormFactory::fromHtml($htmlForm);
        $request = $this->prophesize(ServerRequestInterface::class);
        $request->getMethod()->willReturn('GET');
        $request->getParsedBody()->willReturn($this->rawValues);

        $result = $form->validateRequest($request->reveal());

        self::assertInstanceOf(ValidationResult::class, $result);
        self::assertEquals([], $result->getRawValues());
        self::assertEquals([], $result->getValues());
        self::assertEquals([], $result->getMessages());
        self::assertFalse($result->isValid());
    }

    public function testPsrPostRequestHasMessages()
    {
        $htmlForm = '
            <form action="/" method="post">
                <input type="text" name="foo" pattern="^\d+$" />
                <input type="text" name="baz" data-filters="stringtrim" />
            </form>';

        $form = FormFactory::fromHtml($htmlForm);
        $request = $this->prophesize(ServerRequestInterface::class);
        $request->getMethod()->willReturn('POST');
        $request->getParsedBody()->willReturn($this->rawValues);

        $result = $form->validateRequest($request->reveal());

        self::assertInstanceOf(ValidationResult::class, $result);
        self::assertEquals($this->rawValues, $result->getRawValues());
        self::assertEquals($this->values, $result->getValues());
        self::assertEquals($this->messages, $result->getMessages());
        self::assertFalse($result->isValid());
    }

    public function testSetValuesStatically()
    {
        $htmlForm = '
            <form action="/" method="post">
                <input type="text" name="foo" data-reuse-submitted-value="true" />
                <input type="text" name="baz" data-filters="stringtrim" />
            </form>';

        $form = FormFactory::fromHtml($htmlForm, [
            'foo' => 'bar',
            'baz' => 'qux',
        ]);

        self::assertContains('<input type="text" name="foo" value="bar">', $form->asString());
        self::assertContains('<input type="text" name="baz" value="qux">', $form->asString());
    }

    public function testSetValuesWithConstructor()
    {
        $htmlForm = '
            <form action="/" method="post">
                <input type="text" name="foo" data-reuse-submitted-value="true" />
                <input type="text" name="baz" data-filters="stringtrim" />
            </form>';

        $form = new FormFactory($htmlForm, null, [
            'foo' => 'bar',
            'baz' => 'qux',
        ]);

        self::assertContains('<input type="text" name="foo" value="bar">', $form->asString());
        self::assertContains('<input type="text" name="baz" value="qux">', $form->asString());
    }
}<commit_msg>Add test for accepting objects as default data<commit_after><?php

declare(strict_types=1);

namespace XtreamwayzTest\HTMLFormValidator;

use Psr\Http\Message\ServerRequestInterface;
use Xtreamwayz\HTMLFormValidator\FormFactory;
use Xtreamwayz\HTMLFormValidator\ValidationResult;

class FormFactoryTest extends \PHPUnit_Framework_TestCase
{
    private $rawValues = [
        'foo' => 'bar',
        'baz' => ' qux ',
    ];

    private $values = [
        'foo' => 'bar',
        'baz' => 'qux',
    ];

    private $messages = [
        'foo' => [
            'regexNotMatch' => 'The input does not match against pattern \'/^\d+$/\'',
        ],
    ];

    public function testPsrPostRequestIsValid()
    {
        $htmlForm = '
            <form action="/" method="post">
                <input type="text" name="foo" />
                <input type="text" name="baz" data-filters="stringtrim" />
            </form>';

        $form = FormFactory::fromHtml($htmlForm);
        $request = $this->prophesize(ServerRequestInterface::class);
        $request->getMethod()->willReturn('POST');
        $request->getParsedBody()->willReturn($this->rawValues);

        $result = $form->validateRequest($request->reveal());

        self::assertInstanceOf(ValidationResult::class, $result);
        self::assertEquals($this->rawValues, $result->getRawValues());
        self::assertEquals($this->values, $result->getValues());
        self::assertEquals([], $result->getMessages());
        self::assertTrue($result->isValid());
    }

    public function testPsrGetRequestIsNotValid()
    {
        $htmlForm = '
            <form action="/" method="post">
                <input type="text" name="foo" />
                <input type="text" name="baz" data-filters="stringtrim" />
            </form>';

        $form = FormFactory::fromHtml($htmlForm);
        $request = $this->prophesize(ServerRequestInterface::class);
        $request->getMethod()->willReturn('GET');
        $request->getParsedBody()->willReturn($this->rawValues);

        $result = $form->validateRequest($request->reveal());

        self::assertInstanceOf(ValidationResult::class, $result);
        self::assertEquals([], $result->getRawValues());
        self::assertEquals([], $result->getValues());
        self::assertEquals([], $result->getMessages());
        self::assertFalse($result->isValid());
    }

    public function testPsrPostRequestHasMessages()
    {
        $htmlForm = '
            <form action="/" method="post">
                <input type="text" name="foo" pattern="^\d+$" />
                <input type="text" name="baz" data-filters="stringtrim" />
            </form>';

        $form = FormFactory::fromHtml($htmlForm);
        $request = $this->prophesize(ServerRequestInterface::class);
        $request->getMethod()->willReturn('POST');
        $request->getParsedBody()->willReturn($this->rawValues);

        $result = $form->validateRequest($request->reveal());

        self::assertInstanceOf(ValidationResult::class, $result);
        self::assertEquals($this->rawValues, $result->getRawValues());
        self::assertEquals($this->values, $result->getValues());
        self::assertEquals($this->messages, $result->getMessages());
        self::assertFalse($result->isValid());
    }

    public function testSetValuesStatically()
    {
        $htmlForm = '
            <form action="/" method="post">
                <input type="text" name="foo" data-reuse-submitted-value="true" />
                <input type="text" name="baz" data-filters="stringtrim" />
            </form>';

        $form = FormFactory::fromHtml($htmlForm, [
            'foo' => 'bar',
            'baz' => 'qux',
        ]);

        self::assertContains('<input type="text" name="foo" value="bar">', $form->asString());
        self::assertContains('<input type="text" name="baz" value="qux">', $form->asString());
    }

    public function testSetValuesWithConstructor()
    {
        $htmlForm = '
            <form action="/" method="post">
                <input type="text" name="foo" data-reuse-submitted-value="true" />
                <input type="text" name="baz" data-filters="stringtrim" />
            </form>';

        $form = new FormFactory($htmlForm, null, [
            'foo' => 'bar',
            'baz' => 'qux',
        ]);

        self::assertContains('<input type="text" name="foo" value="bar">', $form->asString());
        self::assertContains('<input type="text" name="baz" value="qux">', $form->asString());
    }

    public function testSetValuesStaticallyFromObject()
    {
        $htmlForm = '
            <form action="/" method="post">
                <input type="text" name="foo" data-reuse-submitted-value="true" />
                <input type="text" name="baz" data-filters="stringtrim" />
            </form>';

        $form = FormFactory::fromHtml($htmlForm, new GenericObjectWithMethods());

        self::assertContains('<input type="text" name="foo" value="bar">', $form->asString());
        self::assertContains('<input type="text" name="baz" value="qux">', $form->asString());
    }

    public function testSetValuesWithConstructorFromObject()
    {
        $htmlForm = '
            <form action="/" method="post">
                <input type="text" name="foo" data-reuse-submitted-value="true" />
                <input type="text" name="baz" data-filters="stringtrim" />
            </form>';

        $form = new FormFactory($htmlForm, null, new GenericObjectWithMethods());

        self::assertContains('<input type="text" name="foo" value="bar">', $form->asString());
        self::assertContains('<input type="text" name="baz" value="qux">', $form->asString());
    }
}

class GenericObjectWithMethods
{
    public function getFoo()
    {
        return 'bar';
    }

    public function getBaz()
    {
        return 'qux';
    }
}
{'fraction_non_alphanumeric': 0.17958594126143476, 'fraction_numerical': 0.00019258545979778527, 'mean_word_length': 2.7467532467532467, 'pattern_counts': {'":': 0, '<': 65, '<?xml version=': 0, '>': 177, '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': '4790299', 'n_tokens_mistral': 2959, 'n_tokens_neox': 2855, 'n_words': 483}