<commit_before><?php
/**
 * Created by PhpStorm.
 * User: <NAME>
 * Date: 26.11.2015
 * Time: 16:09
 */

namespace Blast\Tests\Db\Orm;

use Blast\Db\ConfigInterface;
use Blast\Db\Entity\CollectionInterface;
use Blast\Db\Entity\EntityInterface;
use Blast\Db\Factory;
use Blast\Db\Orm\Mapper;
use Blast\Tests\Db\Stubs\Entities\Post;
use Interop\Container\ContainerInterface;
use Prophecy\Prophecy\ObjectProphecy;
use Stubs\Entities\User;

class MapperTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var EntityInterface
     */
    private $entity;

    /**
     * @var ObjectProphecy
     */
    private $container;

    /**
     * @var Factory
     */
    private $factory;

    protected function setUp()
    {
        $this->container = $this->prophesize(ContainerInterface::class)->willImplement(ContainerInterface::class);

        $container = $this->container->reveal();
        $factory = Factory::create($container, [
            'url' => 'sqlite:///:memory:',
            'memory' => 'true'
        ]);

        $this->entity = new Post();

        $connection = $factory->getConfig()->getConnection(ConfigInterface::DEFAULT_CONNECTION);
        $connection->prepare('CREATE TABLE post (id int, user_id int, title VARCHAR(255), content TEXT)')->execute();
        $connection->prepare('CREATE TABLE user (id int, name VARCHAR(255))')->execute();
        $connection->insert('post', [
            'id' => 1,
            'user_id' => 1,
            'title' => 'Hello World',
            'content' => 'Some text',
        ]);
        $connection->insert('post', [
            'id' => 2,
            'user_id' => 1,
            'title' => 'Next thing',
            'content' => 'More text to read'
        ]);
        $connection->insert('user', [
            'id' => 1,
            'name' => 'Franz'
        ]);
    }

    protected function tearDown()
    {
        $factory = Factory::getInstance();
        $connection = $factory->getConfig()->getConnection(ConfigInterface::DEFAULT_CONNECTION);
        $connection->prepare('DROP TABLE test')->execute();
        $factory->shutdown();
    }

    /**
     * select any field
     */
    public function testSelect()
    {
        $entity = $this->entity;
        $mapper = new Mapper($entity);

        $query = $mapper->select();
        $result = $query->where('user_id = 42')->execute();

        $this->assertInstanceOf(CollectionInterface::class, $result);
        $this->assertEquals(2, $result->count());
    }

    /**
     * find by pk
     */
    public function testFind()
    {
        $entity = $this->entity;
        $mapper = new Mapper($entity);

        $result = $mapper->find(1);

        $this->assertInstanceOf(EntityInterface::class, $result);
    }

    /**
     * find by pk
     */
    public function testBelongsTo()
    {<commit_msg>Fix tests<commit_after><?php
/**
 * Created by PhpStorm.
 * User: <NAME>
 * Date: 26.11.2015
 * Time: 16:09
 */

namespace Blast\Tests\Db\Orm;

use Blast\Db\ConfigInterface;
use Blast\Db\Entity\CollectionInterface;
use Blast\Db\Entity\EntityInterface;
use Blast\Db\Factory;
use Blast\Db\Orm\Mapper;
use Blast\Tests\Db\Stubs\Entities\Post;
use Interop\Container\ContainerInterface;
use Prophecy\Prophecy\ObjectProphecy;
use Stubs\Entities\User;

class MapperTest extends \PHPUnit_Framework_TestCase
{
    /**
     * @var EntityInterface
     */
    private $entity;

    /**
     * @var ObjectProphecy
     */
    private $container;

    /**
     * @var Factory
     */
    private $factory;

    protected function setUp()
    {
        $this->container = $this->prophesize(ContainerInterface::class)->willImplement(ContainerInterface::class);

        $container = $this->container->reveal();
        $factory = Factory::create($container, [
            'url' => 'sqlite:///:memory:',
            'memory' => 'true'
        ]);

        $this->entity = new Post();

        $connection = $factory->getConfig()->getConnection(ConfigInterface::DEFAULT_CONNECTION);
        $connection->prepare('CREATE TABLE post (id int, user_id int, title VARCHAR(255), content TEXT)')->execute();
        $connection->prepare('CREATE TABLE user (id int, name VARCHAR(255))')->execute();
        $connection->insert('post', [
            'id' => 1,
            'user_id' => 1,
            'title' => 'Hello World',
            'content' => 'Some text',
        ]);
        $connection->insert('post', [
            'id' => 2,
            'user_id' => 1,
            'title' => 'Next thing',
            'content' => 'More text to read'
        ]);
        $connection->insert('user', [
            'id' => 1,
            'name' => 'Franz'
        ]);
    }

    protected function tearDown()
    {
        $factory = Factory::getInstance();
        $connection = $factory->getConfig()->getConnection(ConfigInterface::DEFAULT_CONNECTION);
        $connection->prepare('DROP TABLE post')->execute();
        $connection->prepare('DROP TABLE user')->execute();
        $factory->shutdown();
    }

    /**
     * select any field
     */
    public function testSelect()
    {
        $entity = $this->entity;
        $mapper = new Mapper($entity);

        $query = $mapper->select();
        $result = $query->where('user_id = 1')->execute();

        $this->assertInstanceOf(CollectionInterface::class, $result);
        $this->assertEquals(2, $result->count());
    }

    /**
     * find by pk
     */
    public function testFind()
    {
        $entity = $this->entity;
        $mapper = new Mapper($entity);

        $result = $mapper->find(1);

        $this->assertInstanceOf(EntityInterface::class, $result);
    }

    /**
     * find by pk
     */
    public function testBelongsTo()
    {
{'fraction_non_alphanumeric': 0.16243654822335024, 'fraction_numerical': 0.009277087344652548, 'mean_word_length': 2.327897495631916, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 91, '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': '2726709', 'n_tokens_mistral': 1739, 'n_tokens_neox': 1675, 'n_words': 330}