2

I'm new in Testing and in PHPUnit. I have a function that brings me several headaches. It's was time to change me mint and try unit test and some kind of TDD. I read cakePHP documentation and many articles like this: http://programinginreallife.blogspot.com.ar/2013/07/test-driven-designtdd-in-cakephp-2.html http://programinginreallife.blogspot.com.ar/2013/08/test-driven-designtdd-in-cakephp-2-part.html In all of those articles using returns or $this->vars to check asserts. My function doesn't have return or SET variables. The arrays that I need to check are saved in database. To solve this I made a SET with this arrays, but I don't know if the best solution.

This is my not working (yet) function:

<?php
App::uses('AppController', 'Controller');
class CuentasController extends AppController {
    public function pongoAsientos(){
        $this->autoRender = false;

        $club_Nro = 2;
        $añoInicio = 2013;
        $numeroInicio = 8156;//9042;

        $grabaCaja = array();
        $grabaTest = array();
        $grabaCajaTest = array();

        $clubs = $this->Cuenta->Socio->Club->find('first',array('recursive' => -1,
                                                              'conditions' => array('Club.id' => $club_Nro),
                                                              'fields' => array('Club.fechaBalance')));


        list($diaBalance,$mesBalance) = explode("-",$clubs['Club']['fechaBalance']);
        $inicioBalance = date('Y-m-d',mktime(0,0,0,$mesBalance, $diaBalance,intval($añoInicio)));
        $finBalance    = date('Y-m-d',mktime(0,0,0,$mesBalance, $diaBalance,intval($añoInicio+1)));

        $cuentas = $this->Cuenta->find('all',array('recursive' => 0,
                                                   'conditions' => array('PlanCuenta.club_id' => $club_Nro,
                                                                         //'Cuenta.fecha >' => $inicioBalance,
                                                                         //'Cuenta.fecha <=' => $finBalance,
                                                                         'Cuenta.id' => $numeroInicio // Solo para el test
                                                                         //'Cuenta.id >=' => $numeroInicio // Sacar en produccion
                                                                    ),
                                                   'order' => array('Cuenta.fecha ASC','Cuenta.id ASC'),
                                                   //'limit' => 1
                                                   ));

        $nroAsiento = 0;
        $nroOrden = 1;
        $ultimo = 'qwerty';

        foreach ($cuentas as $key => $cuenta){  
            $grabaCaja['Cuenta']['fecha'] = $cuenta['Cuenta']['fecha'];
            $grabaCaja['Cuenta']['plan_cuenta_id'] = $cuenta['Cuenta']['plan_cuenta_id'];
            $grabaCaja['Cuenta']['socio_id'] = $cuenta['Cuenta']['socio_id'];
            $grabaCaja['Cuenta']['detalle'] = $cuenta['Cuenta']['detalle'];
            $grabaCaja['Cuenta']['monto'] = $cuenta['Cuenta']['monto'];

            if ($cuenta['Cuenta']['socio_id'] == '1332'){
                $cuenta['Cuenta']['asiento'] = ++$nroAsiento;
                $grabaCaja['Cuenta']['asiento'] = $nroAsiento;
                $grabaCaja['Cuenta']['plan_cuenta_id'] = '226';
                $cuenta['Cuenta']['plan_cuenta_id'] = '349';
                $cuenta['Cuenta']['socio_id'] = NULL;
                $grabaCaja['Cuenta']['monto'] *= -1;
                if($cuenta['Cuenta']['monto'] > 0 ){
                    $cuenta['Cuenta']['orden'] = $nroOrden;
                    $grabaCaja['Cuenta']['orden'] = $nroOrden+1;
                } else {
                    $cuenta['Cuenta']['concepto']    = 'Gastos Varios';
                    $grabaCaja['Cuenta']['concepto'] = 'Gastos Varios';
                    $cuenta['Cuenta']['orden']    = $nroOrden+1;
                    $grabaCaja['Cuenta']['orden'] = $nroOrden;
                    $cuenta['Cuenta']['monto'] *=-1;
                }
            } else if ($cuenta['Cuenta']['detalle'] != 'Apertura de libros' && 
                    stripos($cuenta['Cuenta']['detalle'],'Cierre de libros') === FALSE &&
                            stripos($cuenta['Cuenta']['detalle'],'Vuelos') === FALSE &&
                                stripos($cuenta['Cuenta']['detalle'],'Hangaraje') === FALSE &&
                                    stripos($cuenta['Cuenta']['detalle'],'Cuota')  === FALSE) {

                $cuenta['Cuenta']['asiento'] = ++$nroAsiento;
                $grabaCaja['Cuenta']['asiento'] = $cuenta['Cuenta']['asiento'];

                $nroOrden = 1;

                $ultimo = $cuenta['Cuenta']['detalle'];

                if($cuenta['Cuenta']['monto'] > 0 ){
                    $cuenta['Cuenta']['orden'] = $nroOrden;
                    $grabaCaja['Cuenta']['orden'] = $nroOrden+1;
                } else {
                    $cuenta['Cuenta']['concepto']    = 'Cobranza';
                    $grabaCaja['Cuenta']['concepto'] = 'Cobranza';
                    $cuenta['Cuenta']['orden']    = $nroOrden+1;
                    $grabaCaja['Cuenta']['orden'] = $nroOrden;
                }

            } else {
                if (stripos($cuenta['Cuenta']['detalle'],$ultimo) === FALSE){
                    $cuenta['Cuenta']['asiento'] = ++$nroAsiento;
                    $nroOrden = 1;
                    $cuenta['Cuenta']['orden'] = $nroOrden;
                    $cuenta['Cuenta']['concepto'] = $cuenta['Cuenta']['detalle'];
                    $ultimo = $cuenta['Cuenta']['detalle'];
                }  else {
                    $cuenta['Cuenta']['asiento'] = $nroAsiento;
                    $cuenta['Cuenta']['orden'] = ++$nroOrden;
                    $ultimo = $cuenta['Cuenta']['detalle'];
                    $cuenta['Cuenta']['concepto'] = $cuenta['Cuenta']['detalle'];
                }
            }
            $graba['Cuenta'] = $cuenta['Cuenta'];
            $this->Cuenta->id = $cuenta['Cuenta']['id'];
            $this->Cuenta->save($graba); 

            $grabaTest[$key] = $graba;

            if (isset($grabaCaja)){
                $grabaCaja['Cuenta']['plan_cuenta_id'] = '226';
                $grabaCaja['Cuenta']['socio_id'] = null;
                $grabaCaja['Cuenta']['monto'] *= -1;
                if ($cuenta['Cuenta']['socio_id']){
                    $socio = $this->Cuenta->Socio->find('first',array('recursive' => 0,
                                                        'conditions' => array('Socio.id' => $cuenta['Cuenta']['socio_id'])));
                    $nombre = $socio['Persona']['apellido'];
                    if (isset($socio['Persona']['nombre'])){
                        $nombre .= ', '.$socio['Persona']['nombre'];
                    }
                    $grabaCaja['Cuenta']['detalle'] .= ' '.$nombre;
                }

                $this->Cuenta->create();
                $this->Cuenta->save($grabaCaja); 
                $grabaCajaTest[$key] = $grabaCaja;
            }
            ////////////////////////////////////////////
            /// ONLY FOR TEST
            ///
            $this->set('grabaTest',$grabaTest);
            $this->set('grabaCajaTest',$grabaCajaTest);
            ///
            ///
            ////////////////////////////////////////////
        }
    }
}

This is one of my tests:

<?php
App::uses('CuentasController', 'Controller');


/**
 * CuentasController Test Case
 *
 */
class CuentasControllerTest extends ControllerTestCase {

/**
 * index method shared by 
 * testPongoAsientos_AperturaDeLibros()
 * testPongoAsientos_MontoMenorCero()
 * testPongoAsientos_id_socio_1332()
 *
 *
 * @param string $enviado
 * @return void
 */
    private function pongoAsientos($enviado){

        $this->generate('Cuentas', array(
            'models' => array(
                'Cuenta' => array(
                    'find'     
                )
            )
        ));

        $this->controller->Cuenta->expects($this->at(0))
            ->method('find')
            ->with('all')
            ->will($this->returnValue($enviado))
            ;

        $result = $this->testAction('/cuentas/pongoAsientos', array('method' => 'GET',
                                                                    'return' => 'contents',
                                                                    $this->headers['Location']
                                                                    //'data' => $data
                                                                    ));
        debug($result);
    }

/**
 * testPongoAsientos_AperturaDeLibros method
 *
 * @return void
 */
    public function testPongoAsientos_AperturaDeLibros() {
        $enviado = array(
            array(
                'Cuenta' => array(
                    'id' => '9000',
                    'asiento' => '1',
                    'fecha' => '2013-06-01',
                    'concepto' => 'Apertura de libros',
                    'orden' => '1',
                    'plan_cuenta_id' => '234',
                    'socio_id' => '1309',
                    'detalle' => 'Apertura de libros',
                    'monto' => '11935'
                )
            )
        );
        $this->pongoAsientos($enviado);

        $esperado = array(
            array(
                'Cuenta' => array(
                    'id' => '9000',
                    'asiento' => (int) 1,
                    'fecha' => '2013-06-01',
                    'concepto' => 'Apertura de libros',
                    'orden' => (int) 1,
                    'plan_cuenta_id' => '234',
                    'socio_id' => '1309',
                    'detalle' => 'Apertura de libros',
                    'monto' => '11935'
                )
            ),
        );
        $this->assertEquals($esperado, $this->vars['grabaTest']);
    }
}

The test working fine, but I not if is the correct way. Any ideas or sugestions will be appreciated.

Thanks in advance.

1 Answer 1

1

I answer my own question. It's was more easy than I was think. I don't know if the best way, but works. I just read the database from my test!!!

<?php
App::uses('CuentasController', 'Controller');
App::uses('Cuenta', 'Model'); // <-- Add This

/**
 * CuentasController Test Case
 *
 */
class CuentasControllerTest extends ControllerTestCase {

/**
 * Fixtures
 *
 * @var array
 */
    public $fixtures = array(
        'app.cuenta',
        'app.persona'
    );

/**
 * setUp method
 *
 * @return void
 */
    public function setUp() {
        parent::setUp();
        $this->Cuenta = ClassRegistry::init('Cuenta');
    }

/**
 * tearDown method
 *
 * @return void
 */
    public function tearDown() {
        unset($this->Cuenta);
        parent::tearDown();
    }

/**
 * testPongoAsientos_global method
 *
 * @return void
 */
    public function testPongoAsientos_global() {

        // Call the function
        $this->testAction('/cuentas/pongoAsientos/1');

        // search in database
        $resultFromDB = $this->Cuenta->find('all');

        $myExpectedResult = array(/* Something here */)

        $this->assertEquals($myExpectedResult,$resultFromDB ,'testPongoAsientos_global');

    }
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.