<commit_before><?php

namespace Balance\Model\Persistence\Db;

use Balance\Mvc\Application;
use PHPUnit_Framework_TestCase as TestCase;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\Parameters;

class PostingsTest extends TestCase
{
    public function testFetch()
    {
        // Inicialização
        $persistence = new Postings();

        // Localizador de Serviços
        $serviceLocator = new ServiceManager();
        // Configuração
        $persistence->setServiceLocator($serviceLocator);

        // Banco de Dados
        $db = Application::getApplication()->getServiceManager()->get('db');
        // Configuração
        $serviceLocator->setService('db', $db);

        // Tabelas
        $tbPostings = Application::getApplication()->getServiceManager()->get('Balance\Db\TableGateway\Postings');

        // Limpeza
        $tbPostings->delete(function ($delete) {});

        // Inserir Lançamento 1
        $tbPostings->insert(array(
            'datetime'    => '2010-10-10 09:10:10',
            'description' => 'Posting 1',
        ));
        // Inserir Lançamento 2
        $tbPostings->insert(array(
            'datetime'    => '2010-10-10 10:10:10',
            'description' => 'Posting 2',
        ));

        // Consulta
        $result = $persistence->fetch(new Parameters())->getCurrentItems();

        // Verificações
        $this->assertCount(2, $result);

        // Elemento
        $element = current($result);
        // Verificações
        $this->assertEquals('2010-10-10 10:10:10', $element['datetime']);
        $this->assertEquals('Posting 2', $element['description']);

        // Elemento
        $element = next($result);
        // Verificações
        $this->assertEquals('2010-10-10 09:10:10', $element['datetime']);
        $this->assertEquals('Posting 1', $element['description']);
    }
}<commit_msg>Issue #88 Testes unitários para pesquisa de lançamentos com palavras-chave.<commit_after><?php

namespace Balance\Model\Persistence\Db;

use Balance\Model\AccountType;
use Balance\Model\EntryType;
use Balance\Mvc\Application;
use PHPUnit_Framework_TestCase as TestCase;
use Zend\ServiceManager\ServiceManager;
use Zend\Stdlib\Parameters;

class PostingsTest extends TestCase
{
    protected function getPersistence()
    {
        // Inicialização
        $persistence = new Postings();

        // Localizador de Serviços
        $serviceLocator = new ServiceManager();
        // Configuração
        $persistence->setServiceLocator($serviceLocator);

        // Banco de Dados
        $db = Application::getApplication()->getServiceManager()->get('db');
        // Configuração
        $serviceLocator->setService('db', $db);

        // Tabelas
        $tbAccounts = Application::getApplication()->getServiceManager()->get('Balance\Db\TableGateway\Accounts');
        $tbPostings = Application::getApplication()->getServiceManager()->get('Balance\Db\TableGateway\Postings');
        $tbEntries  = Application::getApplication()->getServiceManager()->get('Balance\Db\TableGateway\Entries');

        // Limpeza
        $tbPostings->delete(function ($delete) {});
        $tbAccounts->delete(function ($delete) {});

        // Chaves Primárias
        $primaries = array(
            'postings' => array(),
            'accounts' => array(),
        );

        // Inserir Conta 1
        $tbAccounts->insert(array(
            'name'        => 'Account AA',
            'type'        => AccountType::ACTIVE,
            'description' => 'Account AA Description',
            'position'    => 0,
            'accumulate'  => 0,
        ));
        // Captura de Chave Primária
        $primaries['accounts']['aa'] = (int) $tbAccounts->getLastInsertValue();

        // Inserir Conta 2
        $tbAccounts->insert(array(
            'name'        => 'Account BB',
            'type'        => AccountType::ACTIVE,
            'description' => 'Account BB Description',
            'position'    => 1,
            'accumulate'  => 0,
        ));
        // Captura de Chave Primária
        $primaries['accounts']['bb'] = (int) $tbAccounts->getLastInsertValue();

        // Inserir Lançamento 1
        $tbPostings->insert(array(
            'datetime'    => '2010-10-10 09:10:10',
            'description' => 'Posting XX',
        ));
        // Captura de Chave Primária
        $primaries['postings']['xx'] = (int) $tbPostings->getLastInsertValue();

        // Inserir Lançamento 2
        $tbPostings->insert(array(
            'datetime'    => '2010-10-10 10:10:10',
            'description' => 'Posting YY',
        ));
        // Captura de Chave Primária
        $primaries['postings']['yy'] = (int) $tbPostings->getLastInsertValue();

        // Relacionamento 0-0
        $tbEntries->insert(array(
            'posting_id' => $primaries['postings']['xx'],
            'account_id' => $primaries['accounts']['aa'],
            'type'       => EntryType::CREDIT,
            'value'      => 100,
            'position'   => 0,
        ));
        // Relacionamento 0-1
        $tbEntries->insert(array(
            'posting_id' => $primaries['postings']['xx'],
            'account_id' => $primaries['accounts']['bb'],
            'type'       => EntryType::DEBIT,
            'value'      => 100,
            'position'   => 1,
        ));

        // Relacionamento 1-0
        $tbEntries->insert(array(
            'posting_id' => $primaries['postings']['yy'],
            'account_id' => $primaries['accounts']['aa'],
            'type'       => EntryType::CREDIT,
            'value'      => 200,
            'position'   => 0,
        ));
        // Relacionamento 1-1
        $tbEntries->insert(array(
            'posting_id' => $primaries['postings']['yy'],
            'account_id' => $primaries['accounts']['bb'],
            'type'       => EntryType::DEBIT,
            'value'      => 200,
            'position'   => 1,
        ));

        // Apresentação
        return $persistence;
    }

    public function testFetch()
    {
        // Inicialização
        $persistence = $this->getPersistence();

        // Consulta
        $result = $persistence->fetch(new Parameters())->getCurrentItems();

        // Verificações
        $this->assertCount(2, $result);

        // Elemento
        $element = current($result);
        // Verificações
        $this->assertEquals('2010-10-10 10:10:10', $element['datetime']);
        $this->assertEquals('Posting YY', $element['description']);

        // Elemento
        $element = next($result);
        // Verificações
        $this->assertEquals('2010-10-10 09:10:10', $element['datetime']);
        $this->assertEquals('Posting XX', $element['description']);
    }

    public function testFetchWithKeywords()
    {
        // Inicialização
        $persistence = $this->getPersistence();

        // Consulta
        $result = $persistence->fetch(new Parameters(array('keywords' => 'XX')))->getCurrentItems();

        // Verificações
        $this->assertCount(1, $result);

        // Elemento
        $element = current($result);
        // Verificações
        $this->assertEquals('Posting XX', $element['description']);
    }
}
{'fraction_non_alphanumeric': 0.15563605963494495, 'fraction_numerical': 0.02159676745158144, 'mean_word_length': 2.151009657594381, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 97, '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': '6960735', 'n_tokens_mistral': 2145, 'n_tokens_neox': 2024, 'n_words': 357}