Unit Test Mock Objects Segmentation Faults

Hey guys,

I found an article about mocking the database for unit testing: http://www.rami.me.uk/how-to-unit-test-code-with-phalcon-mvc-models/

Unfortunately, I've been getting segmentation faults. Specifically when the model tries to save. I've never really used gdb but did a backtrace I believe. Is this an issue with codeception/phpunit or phalcon? I've had a lot of trouble mocking and/or using fixtures. Anyone have any experience or resources they'd like to share?

Trying to test validation (UserTest::testValidation) ... 
Program received signal SIGSEGV, Segmentation fault.
0x00000000005bdb08 in zend_get_class_entry (zobject=0x12c5b00)
    at /usr/src/debug/php-5.5.24/Zend/zend_API.c:237
237     if (Z_OBJ_HT_P(zobject)->get_class_entry) {
Missing separate debuginfos, use: debuginfo-install cyrus-sasl-lib-2.1.23-15.el6_6.2.x86_64 freetype-2.3.11-15.el6_6.1.x86_64 glibc-2.12-1.149.el6_6.7.x86_64 krb5-libs-1.10.3-37.el6_6.x86_64 lcms-libs-1.19-1.el6.x86_64 libICE-1.0.6-1.el6.x86_64 libcurl-7.19.7-40.el6_6.4.x86_64 libedit-2.11-4.20080712cvs.1.el6.x86_64 libidn-1.18-2.el6.x86_64 libssh2-1.4.2-1.el6_6.1.x86_64 libtool-ltdl-2.2.6-15.5.el6.x86_64 libuuid-2.17.2-12.18.el6.x86_64 libxml2-2.7.6-17.el6_6.1.x86_64 ncurses-libs-5.7-3.20090208.el6.x86_64 nspr-4.10.6-1.el6_5.x86_64 nss-3.16.2.3-3.el6_6.x86_64 nss-softokn-freebl-3.14.3-22.el6_6.x86_64 nss-util-3.16.2.3-2.el6_6.x86_64 openldap-2.4.39-8.el6.x86_64 openssl-1.0.1e-30.el6.8.x86_64 sqlite-3.6.20-1.el6.x86_64
(gdb) bt
#0  0x00000000005bdb08 in zend_get_class_entry (zobject=0x12c5b00)
    at /usr/src/debug/php-5.5.24/Zend/zend_API.c:237
#1  0x00007fffe63e0919 in zim_Phalcon_Mvc_Model_Query__executeSelect ()
   from /usr/lib64/php/modules/phalcon.so
#2  0x00000000005abe49 in dtrace_execute_internal (execute_data_ptr=<value optimized out>, 
    fci=<value optimized out>, return_value_used=<value optimized out>)
    at /usr/src/debug/php-5.5.24/Zend/zend_dtrace.c:97
#3  0x00000000005af1d5 in zend_call_function (fci=0x7fffffff7810, fci_cache=0x7fffffff76f0)
    at /usr/src/debug/php-5.5.24/Zend/zend_execute_API.c:954
#4  0x00007fffe6263869 in phalcon_call_user_function ()
   from /usr/lib64/php/modules/phalcon.so
#5  0x00007fffe6264a3b in phalcon_call_class_method_aparams ()
   from /usr/lib64/php/modules/phalcon.so
#6  0x00007fffe63d3246 in zim_Phalcon_Mvc_Model_Query_execute ()
   from /usr/lib64/php/modules/phalcon.so
#7  0x00000000005abe49 in dtrace_execute_internal (execute_data_ptr=<value optimized out>, 
    fci=<value optimized out>, return_value_used=<value optimized out>)
    at /usr/src/debug/php-5.5.24/Zend/zend_dtrace.c:97
#8  0x00000000005af1d5 in zend_call_function (fci=0x7fffffff7d00, fci_cache=0x7fffffff7be0)
    at /usr/src/debug/php-5.5.24/Zend/zend_execute_API.c:954
#9  0x00007fffe6263869 in phalcon_call_user_function ()
   from /usr/lib64/php/modules/phalcon.so
#10 0x00007fffe6264a3b in phalcon_call_class_method_aparams ()

And the php code:

protected function _before()
    {

        $di = new DI();
        $di->setDefault($di);
        $di->set("security", new \Phalcon\Security());

        $config = new \Phalcon\Config(include PATH_COMMON . '/config/config.php');
        $di->set("config", $config);


        $metadata = new Files(["metaDataDir"=>PATH_COMMON . "/data/metadata/", "lifetime"=>84600]);

        $di->set("modelsMetadata", $metadata);

        $di->set("modelsManager", new Manager());

        $con = $this->getMock('\\Phalcon\\Db\\Adapter\\Pdo\\Mysql', array(
            'getDialect', 'query', 'execute', 'tableExists', 'executeSelect'),
            array(),'',false);

        $dialect = $this->getMock('\\Phalcon\\Db\\Dialect\\Mysql', array('select'), array(), '', false);

        $results = $this->getMock('\\Phalcon\\Db\\Result\\Pdo', array('numRows', 'setFetchMode', 'fetchall'), array(), '', false);

        $numRowsFound = array(0, null);

        $fetchAllData = array(1, array(array()));

        $results->expects($this->any())
            ->method('numRows')
            ->will($this->returnValue($numRowsFound));

        $results->expects($this->any())
            ->method('fetchall')
            ->will($this->returnValue($fetchAllData));

        $dialect->expects($this->any())
            ->method("tableExists")
            ->will($this->returnValue(true));

        $dialect->expects($this->any())
            ->method("select")
            ->will($this->returnValue($results));

        $con->expects($this->any())
            ->method('getDialect')
            ->will($this->returnValue($dialect));

        $con->expects($this->any())
            ->method('query')
            ->will($this->returnValue(array()));

        $con->expects($this->any())
            ->method('execute');


        $di->setShared("db", $con);

        $user = new User;
        $user->setDI($di);
        $user->assign(array(
            'first_name'=>'John',
            'last_name'=>'Doe',
            'email'=>'[email protected]',
            'pass'=>'myPassword',
            'role_id'=>1
        ));
        $this->user = $user;

        $this->di = $di;
    }

Could you please compile from ext/ and try again:

git clone https://github.com/phalcon/cphalcon
cd cphalcon/ext
git checkout 2.0.x
sudo ./install-ext


10.1k

I was actually on 1.3.5. I did as above though, unfortunately install-ext doesnt exist in the ext directory. I tried sudo ./install but that didnt work either. Got this:

Configuring for:
PHP Api Version:         20121113
Zend Module Api No:      20121212
Zend Extension Api No:   220121212
configure: error: in `/home/vagrant/cphalcon/ext':
configure: error: C compiler cannot create executables
See `config.log' for more details.
make: *** No targets specified and no makefile found.  Stop.