<commit_before><?php
/**
 * Created by PhpStorm.
 * User: hameijer
 * Date: 20-12-16
 * Time: 16:06
 */

namespace ActiveRecord;


class SchemaTest extends \PHPUnit_Framework_TestCase
{
    public function testTransformColumnToProperty_When_ColumnIdentifierSupplied_Expect_ColumnIdPrefixedWithUnderscore()
    {

        $schema = new Schema('\Test\Record', new class extends \PDO
        {
            public function __construct()
            {
            }
        });

        $this->assertEquals("_id", $schema->transformColumnToProperty('id'));
    }

    public function testTransformTableIdentifierToRecordClassIdentifier_When_TableIdentifierSupplied_Expect_TableIdPrefixedWithTargetNamespace()
    {

        $schema = new Schema('\Test\Record', new class extends \PDO
        {
            public function __construct()
            {
            }
        });

        $this->assertEquals('\Test\Record\activiteit', $schema->transformTableIdentifierToRecordClassIdentifier('activiteit'));
    }

    public function testPrepareParameters_When_ColumnIdentifiersSupplied_Expect_ColumnIdPrefixedWithUnderscore()
    {

        $schema = new Schema('\Test\Record', new class extends \PDO
        {
            public function __construct()
            {
            }
        });

        $namedParameter = ':' . sha1('where_id');

        $this->assertEquals([["id" => "id = " . $namedParameter],[$namedParameter => '1']], $schema->prepareParameters('where', ['id' => '1']));
    }

    public function testPrepareFields_When_ColumnIdentifiersSupplied_Expect_ColumnsAliassedAsColumnIdsPrefixedWithUnderscore()
    {

        $schema = new Schema('\Test\Record', new class extends \PDO
        {
            public function __construct()
            {
            }
        });

        $this->assertEquals(["id AS _id"], $schema->prepareFields(['id']));
    }

    public function testMakeWhereCondition_When_ColumnIdentifiersAndValuesSupplied_Expect_ProperSQLWhereConditionAndNamedParameters()
    {

        $schema = new Schema('\Test\Record', new class extends \PDO
        {
            public function __construct()
            {
            }
        });

        $namedParameterId = ':' . sha1('where_id');
        $namedParameterName = ':' . sha1('where_name');

        $namedParameters = [];
        $this->assertEquals(" WHERE id = $namedParameterId AND name = $namedParameterName", $schema->makeWhereCondition(['id' => '1', 'name' => 'MYName'], $namedParameters));
        $this->assertEquals('1', $namedParameters[$namedParameterId]);
        $this->assertEquals('MYName', $namedParameters[$namedParameterName]);
    }

    public function testExecute_When_WhenProperQueryWithNamedParametersSupplied_Expect_PDOStatementWithFiveRecords()
    {
        $schema = new Schema('\Test\Record', new class extends \PDO
        {
            public function __construct()
            {
            }

            public function prepare($query, $options = null)
            {
                if ($query === 'SELECT id AS _id, name AS _name FROM activiteit WHERE id = :param1') {
                    return new class extends \PDOStatement
                    {
                        public function __construct()
                        {
                        }

                        public function fetchAll($how = NULL, $class_name = NULL, $ctor_args = NULL)
                        {
                            if ($how === \PDO::FETCH_CLASS && $class_name === '\Test\Record\activiteit') {
                                return [
                                    new class
                                    {
                                    },
                                    new class
                                    {
                                    },
                                    new class
                                    {
                                    },
                                    new class
                                    {
                                    },
                                    new class
                                    {
                                    },
                                ];
                            }
                        }
                    };
                }
            }
        });

        $statement = $schema->execute('SELECT id AS _id, name AS _name FROM activiteit WHERE id = :param1', [':param1' => '1']);

        $this->assertCount(5, $statement->fetchAll(\PDO::FETCH_CLASS, '\Test\Record\activiteit'));
    }
}<commit_msg>test for no where conditions<commit_after><?php
/**
 * Created by PhpStorm.
 * User: hameijer
 * Date: 20-12-16
 * Time: 16:06
 */

namespace ActiveRecord;


class SchemaTest extends \PHPUnit_Framework_TestCase
{
    public function testTransformColumnToProperty_When_ColumnIdentifierSupplied_Expect_ColumnIdPrefixedWithUnderscore()
    {

        $schema = new Schema('\Test\Record', new class extends \PDO
        {
            public function __construct()
            {
            }
        });

        $this->assertEquals("_id", $schema->transformColumnToProperty('id'));
    }

    public function testTransformTableIdentifierToRecordClassIdentifier_When_TableIdentifierSupplied_Expect_TableIdPrefixedWithTargetNamespace()
    {

        $schema = new Schema('\Test\Record', new class extends \PDO
        {
            public function __construct()
            {
            }
        });

        $this->assertEquals('\Test\Record\activiteit', $schema->transformTableIdentifierToRecordClassIdentifier('activiteit'));
    }

    public function testPrepareParameters_When_ColumnIdentifiersSupplied_Expect_ColumnIdPrefixedWithUnderscore()
    {

        $schema = new Schema('\Test\Record', new class extends \PDO
        {
            public function __construct()
            {
            }
        });

        $namedParameter = ':' . sha1('where_id');

        $this->assertEquals([["id" => "id = " . $namedParameter],[$namedParameter => '1']], $schema->prepareParameters('where', ['id' => '1']));
    }

    public function testPrepareFields_When_ColumnIdentifiersSupplied_Expect_ColumnsAliassedAsColumnIdsPrefixedWithUnderscore()
    {

        $schema = new Schema('\Test\Record', new class extends \PDO
        {
            public function __construct()
            {
            }
        });

        $this->assertEquals(["id AS _id"], $schema->prepareFields(['id']));
    }

    public function testMakeWhereCondition_When_NoColumnIdentifiersAndValuesSupplied_Expect_NoProperSQLWhereConditionAndNamedParameters()
    {

        $schema = new Schema('\Test\Record', new class extends \PDO
        {
            public function __construct()
            {
            }
        });

        $namedParameters = [];
        $this->assertEquals("", $schema->makeWhereCondition([], $namedParameters));
        $this->assertCount(0, $namedParameters);
    }

    public function testMakeWhereCondition_When_ColumnIdentifiersAndValuesSupplied_Expect_ProperSQLWhereConditionAndNamedParameters()
    {

        $schema = new Schema('\Test\Record', new class extends \PDO
        {
            public function __construct()
            {
            }
        });

        $namedParameterId = ':' . sha1('where_id');
        $namedParameterName = ':' . sha1('where_name');

        $namedParameters = [];
        $this->assertEquals(" WHERE id = $namedParameterId AND name = $namedParameterName", $schema->makeWhereCondition(['id' => '1', 'name' => 'MYName'], $namedParameters));
        $this->assertEquals('1', $namedParameters[$namedParameterId]);
        $this->assertEquals('MYName', $namedParameters[$namedParameterName]);
    }

    public function testExecute_When_WhenProperQueryWithNamedParametersSupplied_Expect_PDOStatementWithFiveRecords()
    {
        $schema = new Schema('\Test\Record', new class extends \PDO
        {
            public function __construct()
            {
            }

            public function prepare($query, $options = null)
            {
                if ($query === 'SELECT id AS _id, name AS _name FROM activiteit WHERE id = :param1') {
                    return new class extends \PDOStatement
                    {
                        public function __construct()
                        {
                        }

                        public function fetchAll($how = NULL, $class_name = NULL, $ctor_args = NULL)
                        {
                            if ($how === \PDO::FETCH_CLASS && $class_name === '\Test\Record\activiteit') {
                                return [
                                    new class
                                    {
                                    },
                                    new class
                                    {
                                    },
                                    new class
                                    {
                                    },
                                    new class
                                    {
                                    },
                                    new class
                                    {
                                    },
                                ];
                            }
                        }
                    };
                }
            }
        });

        $statement = $schema->execute('SELECT id AS _id, name AS _name FROM activiteit WHERE id = :param1', [':param1' => '1']);

        $this->assertCount(5, $statement->fetchAll(\PDO::FETCH_CLASS, '\Test\Record\activiteit'));
    }
}
{'fraction_non_alphanumeric': 0.1036699149906697, 'fraction_numerical': 0.004665146174580137, 'mean_word_length': 1.5018153526970954, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 48, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 30, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '324218', 'n_tokens_mistral': 2348, 'n_tokens_neox': 2236, 'n_words': 425}