<commit_before><?php

/** @testCase */

namespace NextrasTests\Dbal;

use Mockery\MockInterface;
use Nextras\Dbal\Drivers\IDriver;
use Nextras\Dbal\SqlProcessor;
use Tester\Assert;

require_once __DIR__ . '/../../bootstrap.php';


class SqlProcessorWhereTest extends TestCase
{
	/** @var MockInterface */
	private $driver;

	/** @var SqlProcessor */
	private $parser;


	protected function setUp()
	{
		parent::setUp();
		$this->driver = \Mockery::mock('Nextras\Dbal\Drivers\IDriver');
		$this->parser = new SqlProcessor($this->driver);
	}


	public function testWhereAnd()
	{
		$this->driver->shouldReceive('convertToSql')->once()->with('id', IDriver::TYPE_IDENTIFIER)->andReturn('id');
		$this->driver->shouldReceive('convertToSql')->once()->with('foo', IDriver::TYPE_IDENTIFIER)->andReturn('foo');
		$this->driver->shouldReceive('convertToSql')->once()->with('bar', IDriver::TYPE_IDENTIFIER)->andReturn('bar');
		$this->driver->shouldReceive('convertToSql')->once()->with('baz', IDriver::TYPE_IDENTIFIER)->andReturn('baz');
		$this->driver->shouldReceive('convertToSql')->once()->with('bax', IDriver::TYPE_IDENTIFIER)->andReturn('bax');

		$this->driver->shouldReceive('convertToSql')->once()->with(1, IDriver::TYPE_STRING)->andReturn("'1'");
		$this->driver->shouldReceive('convertToSql')->twice()->with('a', IDriver::TYPE_STRING)->andReturn("'a'");

		Assert::same(
			"SELECT 1 FROM foo WHERE id = 1 AND foo = 2 AND bar IS NULL AND baz IN ('1', 'a') AND bax IN (1, 'a')",
			$this->convert('SELECT 1 FROM foo WHERE %and', [
				'id%i' => '1',
				'foo' => 2,
				'bar%s?' => NULL,
				'baz%s[]' => ['1', 'a'],
				'bax%any[]' => [1, 'a'],
			])
		);
	}


	public function testWhereOrNested()
	{
		$this->driver->shouldReceive('convertToSql')->twice()->with('a', IDriver::TYPE_IDENTIFIER)->andReturn('a');
		$this->driver->shouldReceive('convertToSql')->twice()->with('b', IDriver::TYPE_IDENTIFIER)->andReturn('b');

		Assert::same(
			"SELECT 1 FROM foo WHERE (a = 1 AND b IS NULL) OR (a IS NULL AND b = 1)",
			$this->convert('SELECT 1 FROM foo WHERE %or', [
				['%and', ['a%i?' => 1, 'b%i?' => NULL]],
				['%and', ['a%i?' => NULL, 'b%i?' => 1]],
			])
		);
	}


	public function testEmptyConds()
	{
		Assert::same(
			'SELECT 1 FROM foo WHERE 1=1',
			$this->convert('SELECT 1 FROM foo WHERE %and', [])
		);
	}


	private function convert($sql)
	{
		return $this->parser->process(func_get_args());
	}

}

$test = new SqlProcessorWhereTest();
$test->run();
<commit_msg>tests: improved test for %and / %or<commit_after><?php

/** @testCase */

namespace NextrasTests\Dbal;

use Mockery;
use Nextras\Dbal\Drivers\IDriver;
use Nextras\Dbal\SqlProcessor;
use Tester\Assert;

require_once __DIR__ . '/../../bootstrap.php';


class SqlProcessorWhereTest extends TestCase
{
	/** @var IDriver|Mockery\MockInterface */
	private $driver;

	/** @var SqlProcessor */
	private $parser;


	protected function setUp()
	{
		parent::setUp();
		$this->driver = Mockery::mock('Nextras\Dbal\Drivers\IDriver');
		$this->parser = new SqlProcessor($this->driver);
	}


	public function testAssoc()
	{
		$this->driver->shouldReceive('convertToSql')->once()->with('a', IDriver::TYPE_IDENTIFIER)->andReturn('A');
		$this->driver->shouldReceive('convertToSql')->once()->with('b.c', IDriver::TYPE_IDENTIFIER)->andReturn('BC');
		$this->driver->shouldReceive('convertToSql')->once()->with('d', IDriver::TYPE_IDENTIFIER)->andReturn('D');
		$this->driver->shouldReceive('convertToSql')->once()->with('e', IDriver::TYPE_IDENTIFIER)->andReturn('E');
		$this->driver->shouldReceive('convertToSql')->once()->with('f', IDriver::TYPE_IDENTIFIER)->andReturn('F');

		$this->driver->shouldReceive('convertToSql')->once()->with(1, IDriver::TYPE_STRING)->andReturn("'1'");
		$this->driver->shouldReceive('convertToSql')->twice()->with('a', IDriver::TYPE_STRING)->andReturn("'a'");

		Assert::same(
			'A = 1 AND BC = 2 AND D IS NULL AND E IN (\'1\', \'a\') AND F IN (1, \'a\')',
			$this->parser->processModifier('and', [
				'a%i' => '1',
				'b.c' => 2,
				'd%s?' => NULL,
				'e%s[]' => ['1', 'a'],
				'f%any[]' => [1, 'a'],
			])
		);
	}


	public function testComplex()
	{
		$this->driver->shouldReceive('convertToSql')->times(3)->with('a', IDriver::TYPE_IDENTIFIER)->andReturn('a');
		$this->driver->shouldReceive('convertToSql')->times(3)->with('b', IDriver::TYPE_IDENTIFIER)->andReturn('b');

		Assert::same(
			'(a = 1 AND b IS NULL) OR a = 2 OR (a IS NULL AND b = 1) OR b = 3',
			$this->parser->processModifier('or', [
				['%and', ['a%i?' => 1, 'b%i?' => NULL]],
				'a' => 2,
				['%and', ['a%i?' => NULL, 'b%i?' => 1]],
				'b' => 3,
			])
		);
	}


	public function testEmptyConds()
	{
		Assert::same(
			'1=1',
			$this->parser->processModifier('and', [])
		);

		Assert::same(
			'1=1',
			$this->parser->processModifier('or', [])
		);
	}

}

$test = new SqlProcessorWhereTest();
$test->run();

{'fraction_non_alphanumeric': 0.23581233353820938, 'fraction_numerical': 0.009424298299528785, 'mean_word_length': 4.757075471698113, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 134, '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': '3860916', 'n_tokens_mistral': 1934, 'n_tokens_neox': 1726, 'n_words': 309}