<commit_before><?php
/**
 * magic-method-and-trait-example
 *
 * @copyright 2013 k-holy <<EMAIL>>
 * @license The MIT License (MIT)
 */

namespace Acme\Test;

use Acme\PDOStatement;
use Acme\Test\PDOTestDataImmutable;
use Acme\Test\PDOTestDataMutable;
use Acme\Domain\Data\User;
use Acme\JsonSerializer;

/**
 * Test for PDOStatement
 *
 * @author <EMAIL>
 */
class PDOStatementTest extends \PHPUnit_Framework_TestCase
{

	private function createRecord(\DateTimeImmutable $now)
	{
		$pdo = new \PDO('sqlite::memory:', null, null, [
			\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
		]);

		$pdo->exec('DROP TABLE IF EXISTS users;');
		$pdo->exec(<<<'SQL'
CREATE TABLE users(
  user_id    INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT 
, user_name  TEXT    NOT NULL
, created_at INTEGER NOT NULL
);
SQL
		);

		$pdo->beginTransaction();
		$statement = new PDOStatement($pdo->prepare("INSERT INTO users (user_name, created_at) VALUES (:user_name, :created_at)"));
		$statement->execute([':user_name' => 'test1', ':created_at' => $now->getTimestamp()]);
		$statement->execute([':user_name' => 'test2', ':created_at' => $now->getTimestamp()]);
		$pdo->commit();

		return $pdo;
	}

	public function testCallPdoStatementMethod()
	{
		$timezone = new \DateTimeZone('Asia/Tokyo');
		$now = new \DateTimeImmutable('now', $timezone);

		$pdo = $this->createRecord($now);

		$statement = new PDOStatement($pdo->prepare("SELECT user_id, user_name, created_at FROM users ORDER BY user_id"));

		$statement->execute();

		$user = $statement->fetchObject();

		$this->assertEquals('1', $user->user_id);
		$this->assertEquals('test1', $user->user_name);
		$this->assertEquals($now->getTimestamp(), $user->created_at);
	}

	public function testCallPdoStatementMethodWithArguments()
	{
		$timezone = new \DateTimeZone('Asia/Tokyo');
		$now = new \DateTimeImmutable('now', $timezone);

		$pdo = $this->createRecord($now);

		$statement = new PDOStatement($pdo->prepare("SELECT user_id AS userId, user_name AS userName, created_at AS createdAt FROM users ORDER BY user_id"));

		$statement->execute();

		$user = $statement->fetchObject('\Acme\Test\PDOTestDataImmutable', [['timezone' => $timezone, 'dateFormat' => 'Y-m-d H:i:s']]);

		$this->assertNull($user->userId); // プロパティがセットされた後でコンストラクタが呼ばれるため、コンストラクタで指定したプロパティ以外はNULLになってしまう…
		$this->assertNull($user->userName);
		$this->assertEquals($timezone, $user->timezone);
		$this->assertEquals('Y-m-d H:i:s', $user->dateFormat);
	}

	public function testFetchLazy()
	{
		$timezone = new \DateTimeZone('Asia/Tokyo');
		$now = new \DateTimeImmutable('now', $timezone);

		$pdo = $this->createRecord($now);

		$statement = $pdo->prepare("SELECT user_id AS userId, user_name AS userName, created_at AS createdAt FROM users WHERE user_id = :user_id");

		$statement->execute(['user_id' => 1]);

		$row = $statement->fetch(\PDO::FETCH_LAZY);

		$this->assertInstanceOf('PDORow', $row);
		$this->assertEquals($statement->queryString, $row->queryString);
	}

	/**
	 * @expectedException \BadMethodCallException
	 */
	public function testRaiseExceptionWhenUndefinedMethodIsCalled()
	{
		$timezone = new \DateTimeZone('Asia/Tokyo');
		$now = new \DateTimeImmutable('now', $timezone);

		$pdo = $this->createRecord($now);

		$statement = new PDOStatement($pdo->prepare("SELECT user_id AS userId, user_name AS userName, created_at AS createdAt FROM users ORDER BY user_id"));

		$statement->undefinedMethod();
	}

	public function testExecuteParamInt()
	{
		$timezone = new \DateTimeZone('Asia/Tokyo');
		$now = new \DateTimeImmutable('now', $timezone);
<commit_msg>不要なテストを削除<commit_after><?php
/**
 * magic-method-and-trait-example
 *
 * @copyright 2013 k-holy <<EMAIL>>
 * @license The MIT License (MIT)
 */

namespace Acme\Test;

use Acme\PDOStatement;
use Acme\Test\PDOTestDataImmutable;
use Acme\Test\PDOTestDataMutable;
use Acme\Domain\Data\User;
use Acme\JsonSerializer;

/**
 * Test for PDOStatement
 *
 * @author <EMAIL>
 */
class PDOStatementTest extends \PHPUnit_Framework_TestCase
{

	private function createRecord(\DateTimeImmutable $now)
	{
		$pdo = new \PDO('sqlite::memory:', null, null, [
			\PDO::ATTR_ERRMODE => \PDO::ERRMODE_EXCEPTION,
		]);

		$pdo->exec('DROP TABLE IF EXISTS users;');
		$pdo->exec(<<<'SQL'
CREATE TABLE users(
  user_id    INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT 
, user_name  TEXT    NOT NULL
, created_at INTEGER NOT NULL
);
SQL
		);

		$pdo->beginTransaction();
		$statement = new PDOStatement($pdo->prepare("INSERT INTO users (user_name, created_at) VALUES (:user_name, :created_at)"));
		$statement->execute([':user_name' => 'test1', ':created_at' => $now->getTimestamp()]);
		$statement->execute([':user_name' => 'test2', ':created_at' => $now->getTimestamp()]);
		$pdo->commit();

		return $pdo;
	}

	public function testCallPdoStatementMethod()
	{
		$timezone = new \DateTimeZone('Asia/Tokyo');
		$now = new \DateTimeImmutable('now', $timezone);

		$pdo = $this->createRecord($now);

		$statement = new PDOStatement($pdo->prepare("SELECT user_id, user_name, created_at FROM users ORDER BY user_id"));

		$statement->execute();

		$user = $statement->fetchObject();

		$this->assertEquals('1', $user->user_id);
		$this->assertEquals('test1', $user->user_name);
		$this->assertEquals($now->getTimestamp(), $user->created_at);
	}

	public function testCallPdoStatementMethodWithArguments()
	{
		$timezone = new \DateTimeZone('Asia/Tokyo');
		$now = new \DateTimeImmutable('now', $timezone);

		$pdo = $this->createRecord($now);

		$statement = new PDOStatement($pdo->prepare("SELECT user_id AS userId, user_name AS userName, created_at AS createdAt FROM users WHERE user_id = :user_id"));

		$statement->execute(['user_id' => 1]);
		$user = $statement->fetchObject('\Acme\Test\PDOTestDataImmutable', [['timezone' => $timezone, 'dateFormat' => 'Y-m-d H:i:s']]);

		$this->assertNull($user->userId); // プロパティがセットされた後でコンストラクタが呼ばれるため、コンストラクタで指定したプロパティ以外はNULLになってしまう…
		$this->assertNull($user->userName);
		$this->assertEquals($timezone, $user->timezone);
		$this->assertEquals('Y-m-d H:i:s', $user->dateFormat);
	}

	/**
	 * @expectedException \BadMethodCallException
	 */
	public function testRaiseExceptionWhenUndefinedMethodIsCalled()
	{
		$timezone = new \DateTimeZone('Asia/Tokyo');
		$now = new \DateTimeImmutable('now', $timezone);

		$pdo = $this->createRecord($now);

		$statement = new PDOStatement($pdo->prepare("SELECT user_id AS userId, user_name AS userName, created_at AS createdAt FROM users ORDER BY user_id"));

		$statement->undefinedMethod();
	}

	public function testExecuteParamInt()
	{
		$timezone = new \DateTimeZone('Asia/Tokyo');
		$now = new \DateTimeImmutable('now', $timezone);

{'fraction_non_alphanumeric': 0.16909009144056364, 'fraction_numerical': 0.002698246140008994, 'mean_word_length': 5.30028328611898, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 103, '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': '2539914', 'n_tokens_mistral': 2495, 'n_tokens_neox': 2309, 'n_words': 455}