<commit_before><?php

namespace Ouzo\Db;


use Ouzo\Tests\Assert;

class PreparedStatementEmulatorTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @test
     */
    public function shouldSubstituteParametersInSimpleQuery()
    {
        //given
        $sql = 'select * from users where name = ? and surname = ?';
        $params = array('bob', 'smith');

        //when
        $result = PreparedStatementEmulator::substitute($sql, $params);

        //then
        Assert::thatString($result)->isEqualTo("select * from users where name = 'bob' and surname = 'smith'");
    }

    /**
     * @test
     */
    public function shouldSubstituteBooleanParameters()
    {
        //given
        $sql = 'select * from users where verified = ? or admin = ?';
        $params = array(true, false);

        //when
        $result = PreparedStatementEmulator::substitute($sql, $params);

        //then
        Assert::thatString($result)->isEqualTo("select * from users where verified = true or admin = false");
    }

    /**
     * @test
     */
    public function shouldConvertNumbersToStrings()
    {
        //given
        $sql = "select * from users where age = ?";
        $params = array('26');

        //when
        $result = PreparedStatementEmulator::substitute($sql, $params);

        //then
        Assert::thatString($result)->isEqualTo("select * from users where age = '26'");
    }

    /**
     * @test
     */
    public function shouldNotSubstituteQuestionMarksInStrings()
    {
        //given
        $sql = "select * from users where id = '?'";
        $params = array();

        //when
        $result = PreparedStatementEmulator::substitute($sql, $params);

        //then
        Assert::thatString($result)->isEqualTo($sql);
    }

    /**
     * @test
     */
    public function shouldSubstituteParametersWhenSqlContainsStrings()
    {
        //given
        $sql = "select * from users where name = 'bob?' and age = ? and  surname = 'smith?'";
        $params = array('26');

        //when
        $result = PreparedStatementEmulator::substitute($sql, $params);

        //then
        Assert::thatString($result)->isEqualTo("select * from users where name = 'bob?' and age = '26' and  surname = 'smith?'");
    }

    /**
     * @test
     */
    public function shouldQuoteParams()
    {
         //given
        $sql = "select * from users where surname = ?";
        $params = array("' or '1' = '1");

        //when
        $result = PreparedStatementEmulator::substitute($sql, $params);

        //then
        Assert::thatString($result)->isEqualTo("select * from users where surname = ''' or ''1'' = ''1'");
    }
}
 <commit_msg>fixing test<commit_after><?php

namespace Ouzo\Db;


use Ouzo\Db;
use Ouzo\Tests\Assert;

class PreparedStatementEmulatorTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @test
     */
    public function shouldSubstituteParametersInSimpleQuery()
    {
        //given
        $sql = 'select * from users where name = ? and surname = ?';
        $params = array('bob', 'smith');

        //when
        $result = PreparedStatementEmulator::substitute($sql, $params);

        //then
        Assert::thatString($result)->isEqualTo("select * from users where name = 'bob' and surname = 'smith'");
    }

    /**
     * @test
     */
    public function shouldSubstituteBooleanParameters()
    {
        //given
        $sql = 'select * from users where verified = ? or admin = ?';
        $params = array(true, false);

        //when
        $result = PreparedStatementEmulator::substitute($sql, $params);

        //then
        Assert::thatString($result)->isEqualTo("select * from users where verified = true or admin = false");
    }

    /**
     * @test
     */
    public function shouldConvertNumbersToStrings()
    {
        //given
        $sql = "select * from users where age = ?";
        $params = array('26');

        //when
        $result = PreparedStatementEmulator::substitute($sql, $params);

        //then
        Assert::thatString($result)->isEqualTo("select * from users where age = '26'");
    }

    /**
     * @test
     */
    public function shouldNotSubstituteQuestionMarksInStrings()
    {
        //given
        $sql = "select * from users where id = '?'";
        $params = array();

        //when
        $result = PreparedStatementEmulator::substitute($sql, $params);

        //then
        Assert::thatString($result)->isEqualTo($sql);
    }

    /**
     * @test
     */
    public function shouldSubstituteParametersWhenSqlContainsStrings()
    {
        //given
        $sql = "select * from users where name = 'bob?' and age = ? and  surname = 'smith?'";
        $params = array('26');

        //when
        $result = PreparedStatementEmulator::substitute($sql, $params);

        //then
        Assert::thatString($result)->isEqualTo("select * from users where name = 'bob?' and age = '26' and  surname = 'smith?'");
    }

    /**
     * @test
     */
    public function shouldQuoteParams()
    {
         //given
        $sql = "select * from users where surname = ?";
        $param = "' or '1' = '1";

        //when
        $result = PreparedStatementEmulator::substitute($sql, array($param));

        //then
        Assert::thatString($result)->isEqualTo("select * from users where surname = " . Db::getInstance()->_dbHandle->quote($param));
    }
}
 
{'fraction_non_alphanumeric': 0.14669881305637983, 'fraction_numerical': 0.004080118694362018, 'mean_word_length': 2.187352245862884, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 17, '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': '524175', 'n_tokens_mistral': 1596, 'n_tokens_neox': 1507, 'n_words': 354}