<commit_before><?php

namespace MadeSimple\Database\Tests\Unit;

use MadeSimple\Database\Connection;
use MadeSimple\Database\Entity;
use MadeSimple\Database\EntityMap;
use MadeSimple\Database\Pool;
use MadeSimple\Database\Query;
use MadeSimple\Database\Tests\TestCase;

class EntityTest extends TestCase
{
    /**
     * @var \Mockery\Mock|Connection
     */
    private $mockConnection;

    /**
     * @var \Mockery\Mock|Pool
     */
    private $mockPool;

    protected function setUp()
    {
        parent::setUp();

        $this->mockConnection = \Mockery::mock(Connection::class);
        $this->mockPool       = \Mockery::mock(Pool::class);
    }


    /**
     * Test construction with no data.
     */
    public function testConstructNoData()
    {
        $entity = new SingleKeyEntity($this->mockPool);

        $this->assertNull($entity->id);
        $this->assertNull($entity->firstName);
        $this->assertNull($entity->lastName);
    }

    /**
     * Test construction with data.
     *
     * @param array $data
     * @dataProvider populateDataProvider
     */
    public function testConstructWithData($data)
    {
        $entity = new SingleKeyEntity($this->mockPool, $data);

        $this->assertEquals($data['ID'], $entity->id);
        $this->assertEquals($data['first_name'], $entity->firstName);
        $this->assertEquals($data['last_name'], $entity->lastName);
    }

    /**
     * Test populate with data.
     *
     * @param array $data
     * @dataProvider populateDataProvider
     */
    public function testPopulate($data)
    {
        $entity = new SingleKeyEntity($this->mockPool);
        $entity->populate($data);

        $this->assertEquals($data['ID'], $entity->id);
        $this->assertEquals($data['first_name'], $entity->firstName);
        $this->assertEquals($data['last_name'], $entity->lastName);
    }

    /**
     * Test populate with data with entity map.
     *
     * @param array $data
     * @dataProvider populateDataProvider
     */
    public function testPopulateWithMap($data)
    {
        $entity = new SingleKeyEntity($this->mockPool);
        $entity->populate($data, $entity->getMap());

        $this->assertEquals($data['ID'], $entity->id);
        $this->assertEquals($data['first_name'], $entity->firstName);
        $this->assertEquals($data['last_name'], $entity->lastName);
    }

    /**
     * Test persist creates an entity that does not have a primary key set.
     */
    public function testPersistSinglePrimaryKeyCreate()
    {
        /** @var \Mockery\Mock $mockInsert */
        $mockInsert = \Mockery::mock(Query\Insert::class);

        $this->mockPool->shouldReceive('get')->once()->with(null)->andReturn($this->mockConnection);
        $this->mockConnection->shouldReceive('insert')->once()->withNoArgs()->andReturn($mockInsert);
        $mockInsert->shouldReceive('into')->once()->with('dummy')->andReturnSelf();
        $mockInsert->shouldReceive('columns')->once()->with(['first_name', 'last_name'])->andReturnSelf();
        $mockInsert->shouldReceive('values')->once()->with(['Test', 'Person'])->andReturnSelf();
        $mockInsert->shouldReceive('query')->once()->withNoArgs()->andReturnSelf();
        $mockInsert->shouldReceive('lastInsertId')->once()->withNoArgs()->andReturn(5);

        $entity = new SingleKeyEntity($this->mockPool);
        $entity->firstName = 'Test';
        $entity->lastName  = 'Person';

        $this->assertTrue($entity->persist());
        $this->assertEquals(5, $entity->id);
        $this->assertTrue($entity->createdRecently);
    }

    /**
     * Test persist updates an entity that does have a primary key set.
     */
    public function testPersistSinglePrimaryKeyUpdate()
    {
        /** @var \Mockery\Mock $mockUpdate */
        $mockUpdate = \Mockery::mock(Query\Update::class);

        $this->mockPool->shouldReceive('get')->once()->with(null)->andReturn($this->mockConnection);
        $this->mockConnection->shouldReceive('update')->once()->withNoArgs()->andReturn($mockUpdate);
        $mockUpdate->shouldReceive('table')->once()->with('dummy')->andReturnSelf();
        $mockUpdate->shouldReceive('columns')->once()->with(['first_name' => 'Test', 'last_name' => 'Person'])->andReturnSelf();
        $mockUpdate->shouldReceive('where')->once()->with('ID', '=', 5);
        $mockUpdate->shouldReceive('query')->once()->withNoArgs()->andReturnSelf();
        $mockUpdate->shouldReceive('affectedRows')->once()->withNoArgs()->andReturn(1);


        $entity = new SingleKeyEntity($this->mockPool);
        $entity->id        = 5;
        $entity->firstName = 'Test';
        $entity->lastName  = 'Person';

        $this->assertTrue($entity->update());
        $this->assertFalse($entity->createdRecently);
    }

    /**
     * Test persist throws an exception when attempting to persist a composite key entity.
     *
     * @expectedException \InvalidArgumentException
     * @expectedExceptionMessage Cannot persist
     */
    public function testPersistCompositePrimaryKey()
    {
        $entity = new CompositeKeyEntity($this->mockPool);
        $entity->userId    = 5;
        $entity->companyId = 7;
        $entity->value     = 'value';

        $entity->persist();
    }

    /**
     * Test create with a single primary key.
     */
    public function testCreateSinglePrimaryKey()
    {
        /** @var \Mockery\Mock $mockInsert */
        $mockInsert = \Mockery::mock(Query\Insert::class);

        $this->mockPool->shouldReceive('get')->once()->with(null)->andReturn($this->mockConnection);
        $this->mockConnection->shouldReceive('insert')->once()->withNoArgs()->andReturn($mockInsert);
        $mockInsert->shouldReceive('into')->once()->with('dummy')->andReturnSelf();
        $mockInsert->shouldReceive('columns')->once()->with(['first_name', 'last_name'])->andReturnSelf();
        $mockInsert->shouldReceive('values')->once()->with(['Test', 'Person'])->andReturnSelf();
        $mockInsert->shouldReceive('query')->once()->withNoArgs()->andReturnSelf();
        $mockInsert->shouldReceive('lastInsertId')->once()->withNoArgs()->andReturn(5);

        $entity = new SingleKeyEntity($this->mockPool);
        $entity->firstName = 'Test';
        $entity->lastName  = 'Person';

        $this->assertTrue($entity->create());
        $this->assertEquals(5, $entity->id);
        $this->assertTrue($entity->createdRecently);
    }

    /**
     * Test create with a composite primary key.
     */
    public function testCreateCompositePrimaryKey()
    {
        /** @var \Mockery\Mock $mockInsert */
        $mockInsert = \Mockery::mock(Query\Insert::class);

        $this->mockPool->shouldReceive('get')->once()->with(null)->andReturn($this->mockConnection);
        $this->mockConnection->shouldReceive('insert')->once()->withNoArgs()->andReturn($mockInsert);
        $mockInsert->shouldReceive('into')->once()->with('dummy_link')->andReturnSelf();
        $mockInsert->shouldReceive('columns')->once()->with(['value'])->andReturnSelf();
        $mockInsert->shouldReceive('values')->once()->with(['value'])->andReturnSelf();
        $mockInsert->shouldReceive('query')->once()->withNoArgs()->andReturnSelf();
        $mockInsert->shouldReceive('lastInsertId')->never();

        $entity = new CompositeKeyEntity($this->mockPool);
        $entity->userId    = 5;
        $entity->companyId = 7;
        $entity->value     = 'value';
<commit_msg>[fix] EntityTest creating a composite key with set primary keys now works again<commit_after><?php

namespace MadeSimple\Database\Tests\Unit;

use MadeSimple\Database\Connection;
use MadeSimple\Database\Entity;
use MadeSimple\Database\EntityMap;
use MadeSimple\Database\Pool;
use MadeSimple\Database\Query;
use MadeSimple\Database\Tests\TestCase;

class EntityTest extends TestCase
{
    /**
     * @var \Mockery\Mock|Connection
     */
    private $mockConnection;

    /**
     * @var \Mockery\Mock|Pool
     */
    private $mockPool;

    protected function setUp()
    {
        parent::setUp();

        $this->mockConnection = \Mockery::mock(Connection::class);
        $this->mockPool       = \Mockery::mock(Pool::class);
    }


    /**
     * Test construction with no data.
     */
    public function testConstructNoData()
    {
        $entity = new SingleKeyEntity($this->mockPool);

        $this->assertNull($entity->id);
        $this->assertNull($entity->firstName);
        $this->assertNull($entity->lastName);
    }

    /**
     * Test construction with data.
     *
     * @param array $data
     * @dataProvider populateDataProvider
     */
    public function testConstructWithData($data)
    {
        $entity = new SingleKeyEntity($this->mockPool, $data);

        $this->assertEquals($data['ID'], $entity->id);
        $this->assertEquals($data['first_name'], $entity->firstName);
        $this->assertEquals($data['last_name'], $entity->lastName);
    }

    /**
     * Test populate with data.
     *
     * @param array $data
     * @dataProvider populateDataProvider
     */
    public function testPopulate($data)
    {
        $entity = new SingleKeyEntity($this->mockPool);
        $entity->populate($data);

        $this->assertEquals($data['ID'], $entity->id);
        $this->assertEquals($data['first_name'], $entity->firstName);
        $this->assertEquals($data['last_name'], $entity->lastName);
    }

    /**
     * Test populate with data with entity map.
     *
     * @param array $data
     * @dataProvider populateDataProvider
     */
    public function testPopulateWithMap($data)
    {
        $entity = new SingleKeyEntity($this->mockPool);
        $entity->populate($data, $entity->getMap());

        $this->assertEquals($data['ID'], $entity->id);
        $this->assertEquals($data['first_name'], $entity->firstName);
        $this->assertEquals($data['last_name'], $entity->lastName);
    }

    /**
     * Test persist creates an entity that does not have a primary key set.
     */
    public function testPersistSinglePrimaryKeyCreate()
    {
        /** @var \Mockery\Mock $mockInsert */
        $mockInsert = \Mockery::mock(Query\Insert::class);

        $this->mockPool->shouldReceive('get')->once()->with(null)->andReturn($this->mockConnection);
        $this->mockConnection->shouldReceive('insert')->once()->withNoArgs()->andReturn($mockInsert);
        $mockInsert->shouldReceive('into')->once()->with('dummy')->andReturnSelf();
        $mockInsert->shouldReceive('columns')->once()->with(['first_name', 'last_name'])->andReturnSelf();
        $mockInsert->shouldReceive('values')->once()->with(['Test', 'Person'])->andReturnSelf();
        $mockInsert->shouldReceive('query')->once()->withNoArgs()->andReturnSelf();
        $mockInsert->shouldReceive('lastInsertId')->once()->withNoArgs()->andReturn(5);

        $entity = new SingleKeyEntity($this->mockPool);
        $entity->firstName = 'Test';
        $entity->lastName  = 'Person';

        $this->assertTrue($entity->persist());
        $this->assertEquals(5, $entity->id);
        $this->assertTrue($entity->createdRecently);
    }

    /**
     * Test persist updates an entity that does have a primary key set.
     */
    public function testPersistSinglePrimaryKeyUpdate()
    {
        /** @var \Mockery\Mock $mockUpdate */
        $mockUpdate = \Mockery::mock(Query\Update::class);

        $this->mockPool->shouldReceive('get')->once()->with(null)->andReturn($this->mockConnection);
        $this->mockConnection->shouldReceive('update')->once()->withNoArgs()->andReturn($mockUpdate);
        $mockUpdate->shouldReceive('table')->once()->with('dummy')->andReturnSelf();
        $mockUpdate->shouldReceive('columns')->once()->with(['first_name' => 'Test', 'last_name' => 'Person'])->andReturnSelf();
        $mockUpdate->shouldReceive('where')->once()->with('ID', '=', 5);
        $mockUpdate->shouldReceive('query')->once()->withNoArgs()->andReturnSelf();
        $mockUpdate->shouldReceive('affectedRows')->once()->withNoArgs()->andReturn(1);


        $entity = new SingleKeyEntity($this->mockPool);
        $entity->id        = 5;
        $entity->firstName = 'Test';
        $entity->lastName  = 'Person';

        $this->assertTrue($entity->update());
        $this->assertFalse($entity->createdRecently);
    }

    /**
     * Test persist throws an exception when attempting to persist a composite key entity.
     *
     * @expectedException \InvalidArgumentException
     * @expectedExceptionMessage Cannot persist
     */
    public function testPersistCompositePrimaryKey()
    {
        $entity = new CompositeKeyEntity($this->mockPool);
        $entity->userId    = 5;
        $entity->companyId = 7;
        $entity->value     = 'value';

        $entity->persist();
    }

    /**
     * Test create with a single primary key.
     */
    public function testCreateSinglePrimaryKey()
    {
        /** @var \Mockery\Mock $mockInsert */
        $mockInsert = \Mockery::mock(Query\Insert::class);

        $this->mockPool->shouldReceive('get')->once()->with(null)->andReturn($this->mockConnection);
        $this->mockConnection->shouldReceive('insert')->once()->withNoArgs()->andReturn($mockInsert);
        $mockInsert->shouldReceive('into')->once()->with('dummy')->andReturnSelf();
        $mockInsert->shouldReceive('columns')->once()->with(['first_name', 'last_name'])->andReturnSelf();
        $mockInsert->shouldReceive('values')->once()->with(['Test', 'Person'])->andReturnSelf();
        $mockInsert->shouldReceive('query')->once()->withNoArgs()->andReturnSelf();
        $mockInsert->shouldReceive('lastInsertId')->once()->withNoArgs()->andReturn(5);

        $entity = new SingleKeyEntity($this->mockPool);
        $entity->firstName = 'Test';
        $entity->lastName  = 'Person';

        $this->assertTrue($entity->create());
        $this->assertEquals(5, $entity->id);
        $this->assertTrue($entity->createdRecently);
    }

    /**
     * Test create with a composite primary key.
     */
    public function testCreateCompositePrimaryKey()
    {
        /** @var \Mockery\Mock $mockInsert */
        $mockInsert = \Mockery::mock(Query\Insert::class);

        $this->mockPool->shouldReceive('get')->once()->with(null)->andReturn($this->mockConnection);
        $this->mockConnection->shouldReceive('insert')->once()->withNoArgs()->andReturn($mockInsert);
        $mockInsert->shouldReceive('into')->once()->with('dummy_link')->andReturnSelf();
        $mockInsert->shouldReceive('columns')->once()->with(['user_id', 'company_id', 'value'])->andReturnSelf();
        $mockInsert->shouldReceive('values')->once()->with([5, 7, 'value'])->andReturnSelf();
        $mockInsert->shouldReceive('query')->once()->withNoArgs()->andReturnSelf();
        $mockInsert->shouldReceive('lastInsertId')->never();

        $entity = new CompositeKeyEntity($this->mockPool);
        $entity->userId    = 5;
        $entity->companyId = 7;
        $entity->value     = 'value';

{'fraction_non_alphanumeric': 0.1826209110977323, 'fraction_numerical': 0.0016054585591009432, 'mean_word_length': 3.26777048244362, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 385, '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': '4594276', 'n_tokens_mistral': 4208, 'n_tokens_neox': 4059, 'n_words': 616}