<commit_before><?php

/**
 * This file is part of the miBadger package.
 *
 * @author <NAME> <<EMAIL>>
 * @license http://opensource.org/licenses/Apache-2.0 Apache v2 License
 */

namespace miBadger\ActiveRecord\AutoAPITest;

use PHPUnit\Framework\TestCase;
use miBadger\ActiveRecord\AbstractActiveRecord;
use miBadger\ActiveRecord\Traits\AutoApi;
use miBadger\ActiveRecord\Traits\SoftDelete;
use miBadger\ActiveRecord\ColumnProperty;

/**
 * The abstract active record test class.
 *
 * @since 1.0.0
 */
class AbstractActiveRecord_OperationsTest extends TestCase
{
	/** @var \PDO The PDO. */
	private $pdo;

	public function setUp()
	{
		// Note: If using sqlite, Exception messages will differ!
		// $this->pdo = new \PDO('sqlite::memory:');
		// $this->pdo->query('CREATE TABLE IF NOT EXISTS `name` (`id` INTEGER PRIMARY KEY, `field` VARCHAR(255))');
		$this->pdo = new \PDO(sprintf('mysql:host=%s;dbname=%s;charset=utf8mb4', 'localhost', DB_NAME), DB_USER, DB_PASS);

		$e = new TestEntity($this->pdo);
		$e->createTable();
	}

	public function tearDown()
	{
		$this->pdo->query('DROP TABLE `test_entity_mock`');
	}


	private function createMockData()
	{
		$cEntity = new TestEntity($this->pdo);
		$cEntity->setName("badger");
		$cEntity->setBirthday(new \DateTime("1990-01-01"));
		$cEntity->create();		

		$cEntity2 = new TestEntity($this->pdo);
		$cEntity2->setName("turtle");
		$cEntity2->setBirthday(new \DateTime("1990-01-01"));
		$cEntity2->create();

		return [$cEntity->getId(), $cEntity2->getId()];
	}

	public function testApiRead()
	{
		// Create sample data
		[$id,] = $this->createMockData();

		// Test read
		$entity = new TestEntity($this->pdo);
		$res = $entity->apiRead($id, ['name', 'birthday', 'soft_delete']);
		$this->assertEquals(array_keys($res), ['name', 'birthday', 'soft_delete']);
		$this->assertEquals($res['name'], 'badger');
	}

	public function testApiCreate()
	{
		$input = [
			'name' => 'badger',
			'birthday' => '1990-01-01'
		];

		$entity = new TestEntity($this->pdo);
		[$errors, $data] = $entity->apiCreate($input, ['name', 'birthday']);

		$this->assertNull($errors);
		$this->assertNotNull($data);
		$this->assertNotNull($entity->getId());
	}

	public function testAipCreateWithValidInputSubset()
	{
		$input = [
			'name' => 'badger',
		];

		$entity = new TestEntity($this->pdo);
		[$errors, $data] = $entity->apiCreate($input, ['name', 'birthday']);

		$this->assertNull($errors);
		$this->assertNotNull($data);
		$this->assertNotNull($entity->getId());
	}

	public function testApiCreateMissingKeys()
	{
		$input = [
			'birthday' => '1990-01-01'
		];

		$entity = new TestEntity($this->pdo);
		[$errors, $data] = $entity->apiCreate($input, ['name', 'birthday']);
		$this->assertNotNull($errors);
		$this->assertNull($data);
	}

	public function testApiCreateExcessFields() 
	{
		$input = [
			'name' => 'badger',
			'birthday' => '1990-01-01',
			'blablabla' => 0
		];

		$entity = new TestEntity($this->pdo);
		[$errors, $data] = $entity->apiCreate($input, ['name', 'birthday', 'blablabla']);
		$this->assertNotNull($errors);
	}

	public function testApiCreateInvalidInput()
	{
		$input = [
			'name' => 'badger',
			'birthday' => 'NOT_A_VALID_INPUT'
		];

		$entity = new TestEntity($this->pdo);
		[$errors, $data] = $entity->apiCreate($input, ['name', 'birthday']);
		$this->assertNotNull($errors);
	}

	public function testApiUpdate()
	{
		[$id,] = $this->createMockData();

		$entity = new TestEntity($this->pdo);
		$entity->read($id);

		$inputData = [
			'name' => 'bear'
		];

		[$errors, $data] = $entity->apiUpdate($inputData, ['name', 'birthday']);
		$this->assertNotNull($data);
		$this->assertNull($errors);
	}

	public function testApiUpdateImmutables()
	{
		[$id,] = $this->createMockData();

		$entity = new TestEntity($this->pdo);
		$entity->read($id);

		$inputData = [
			'birthday' => '2018-01-01'
		];

		[$errors, $data] = $entity->apiUpdate($inputData, ['name', 'birthday']);
		$this->assertNull($data);
		$this->assertNotNull($errors);<commit_msg>Fixing CI<commit_after><?php

/**
 * This file is part of the miBadger package.
 *
 * @author <NAME> <<EMAIL>>
 * @license http://opensource.org/licenses/Apache-2.0 Apache v2 License
 */

namespace miBadger\ActiveRecord\AutoAPITest;

use PHPUnit\Framework\TestCase;
use miBadger\ActiveRecord\AbstractActiveRecord;
use miBadger\ActiveRecord\Traits\AutoApi;
use miBadger\ActiveRecord\Traits\SoftDelete;
use miBadger\ActiveRecord\ColumnProperty;

/**
 * The abstract active record test class.
 *
 * @since 1.0.0
 */
class AbstractActiveRecord_OperationsTest extends TestCase
{
	/** @var \PDO The PDO. */
	private $pdo;

	public function setUp()
	{
		// Note: If using sqlite, Exception messages will differ!
		// $this->pdo = new \PDO('sqlite::memory:');
		// $this->pdo->query('CREATE TABLE IF NOT EXISTS `name` (`id` INTEGER PRIMARY KEY, `field` VARCHAR(255))');
		$this->pdo = new \PDO(sprintf('mysql:host=%s;dbname=%s;charset=utf8mb4', 'localhost', DB_NAME), DB_USER, DB_PASS);

		$e = new TestEntity($this->pdo);
		$e->createTable();
	}

	public function tearDown()
	{
		$this->pdo->query('DROP TABLE `test_entity_mock`');
	}


	private function createMockData()
	{
		$cEntity = new TestEntity($this->pdo);
		$cEntity->setName("badger");
		$cEntity->setBirthday(new \DateTime("1990-01-01"));
		$cEntity->create();		

		$cEntity2 = new TestEntity($this->pdo);
		$cEntity2->setName("turtle");
		$cEntity2->setBirthday(new \DateTime("1990-01-01"));
		$cEntity2->create();

		return [$cEntity->getId(), $cEntity2->getId()];
	}

	public function testApiRead()
	{
		// Create sample data
		$id = $this->createMockData()[0];

		// Test read
		$entity = new TestEntity($this->pdo);
		$res = $entity->apiRead($id, ['name', 'birthday', 'soft_delete']);
		$this->assertEquals(array_keys($res), ['name', 'birthday', 'soft_delete']);
		$this->assertEquals($res['name'], 'badger');
	}

	public function testApiCreate()
	{
		$input = [
			'name' => 'badger',
			'birthday' => '1990-01-01'
		];

		$entity = new TestEntity($this->pdo);
		[$errors, $data] = $entity->apiCreate($input, ['name', 'birthday']);

		$this->assertNull($errors);
		$this->assertNotNull($data);
		$this->assertNotNull($entity->getId());
	}

	public function testAipCreateWithValidInputSubset()
	{
		$input = [
			'name' => 'badger',
		];

		$entity = new TestEntity($this->pdo);
		[$errors, $data] = $entity->apiCreate($input, ['name', 'birthday']);

		$this->assertNull($errors);
		$this->assertNotNull($data);
		$this->assertNotNull($entity->getId());
	}

	public function testApiCreateMissingKeys()
	{
		$input = [
			'birthday' => '1990-01-01'
		];

		$entity = new TestEntity($this->pdo);
		[$errors, $data] = $entity->apiCreate($input, ['name', 'birthday']);
		$this->assertNotNull($errors);
		$this->assertNull($data);
	}

	public function testApiCreateExcessFields() 
	{
		$input = [
			'name' => 'badger',
			'birthday' => '1990-01-01',
			'blablabla' => 0
		];

		$entity = new TestEntity($this->pdo);
		[$errors, $data] = $entity->apiCreate($input, ['name', 'birthday', 'blablabla']);
		$this->assertNotNull($errors);
	}

	public function testApiCreateInvalidInput()
	{
		$input = [
			'name' => 'badger',
			'birthday' => 'NOT_A_VALID_INPUT'
		];

		$entity = new TestEntity($this->pdo);
		[$errors, $data] = $entity->apiCreate($input, ['name', 'birthday']);
		$this->assertNotNull($errors);
	}

	public function testApiUpdate()
	{
		$id = $this->createMockData()[0];

		$entity = new TestEntity($this->pdo);
		$entity->read($id);

		$inputData = [
			'name' => 'bear'
		];

		[$errors, $data] = $entity->apiUpdate($inputData, ['name', 'birthday']);
		$this->assertNotNull($data);
		$this->assertNull($errors);
	}

	public function testApiUpdateImmutables()
	{
		$id = $this->createMockData()[0];

		$entity = new TestEntity($this->pdo);
		$entity->read($id);

		$inputData = [
			'birthday' => '2018-01-01'
		];

		[$errors, $data] = $entity->apiUpdate($inputData, ['name', 'birthday']);
		$this->assertNull($data);
		$this->assertNotNull($errors);
{'fraction_non_alphanumeric': 0.2066865523241362, 'fraction_numerical': 0.01652995277156351, 'mean_word_length': 4.565006915629322, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 145, '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': '2059998', 'n_tokens_mistral': 3214, 'n_tokens_neox': 2862, 'n_words': 522}