jeudi 25 juin 2015

Testing Laravel 4.2 with phpunit DB::table()->get() returns arrays not an objects

I started adding unit / functional tests to legacy Laravel 4.2 webapp using phpunit and I'm seeing a weird error when DB::table is used.

Here is a very simple example, the test hits a controller method which calls DB::table then dies and dumps the result.

class ExternalFormTest extends TestCase {

    public function testGetExternalFormThankYouPage()
    {
        $response = $this->call('GET', 'test');

This is the controller method being hit.

public function getIndex()
{
    $results = DB::table('users')->get();
    dd($results);

This returns an array of arrays.

..array(12) {
  [0] =>
  array(74) {
    'id' =>
    int(1)
    [0] =>
    int(1)
    'account_number' =>
    int(1000)
    [1] =>
    int(1000)
    'account_admin' =>
    int(1)

But if I hit it with my browser.

And I get an array of objects...

array (size=12)
  0 => 
    object(stdClass)[1967]
      public 'id' => int 1
      public 'account_number' => int 1000
      public 'account_admin' => int 1
      public 'user_type' => int 1

This causes a bunch of errors throughout the App. Because the code expects to access properties (ie $individual_result->id) but the results are arrays. I have tried this with sqlite in memory and normal MySql database. Is this a bug or am I missing something about how Laravel returns results and/or how phpunit works.

Any suggestions would be helpful.

This is my phpunit.xml file.

<?xml version="1.0" encoding="UTF-8"?>
<phpunit backupGlobals="false"
     backupStaticAttributes="false"
     bootstrap="bootstrap/autoload.php"
     colors="true"
     convertErrorsToExceptions="true"
     convertNoticesToExceptions="true"
     convertWarningsToExceptions="true"
     processIsolation="false"
     stopOnFailure="false"
     syntaxCheck="false"
>
    <testsuites>
        <testsuite name="Application Test Suite">
            <directory>./app/tests/</directory>
        </testsuite>
    </testsuites>
</phpunit>

Aucun commentaire:

Enregistrer un commentaire