Question

Hello I'm pretty new to PHPUnit and Laravel 4. I've created an REST API and I'm trying to add a new User using laravel (PHPUnit) unit tests, to my test DB. The problem is, that it completely disregards the Object array that I'm trying to add with the user doc creation.

A typical User Doc would look as follows:

{
    _id": ObjectId("53021089b4d15220118b4568"),
   "oArr": {
     "something": {
          "somewhere": "5th Avenue",
          "ip": "192.168.0.20"
     },
     "something2": {
          "somewhere": "6th Avenue",
          "ip": "192.168.0.21"
     }
  }
   "email": "jessica@example.com",
   "name": "Jessica Alba",
   "password": "$2y$10$RAVzUGLAG.81IOOUum0k0u5vrcY98H.L42FeSJekEywUCV.ycttn6"
}

Now Laravel has it's own Testing class TestCase which your Unit tests extend. Typical example would be the following:

class AddUserTest extends TestCase {

    /**
     * A basic functional test example.
     *
     * @return void
     */


    public function testCreateUsers()
    {
      $post_data = array(
          'email' => 'emma@example.com',
          'name' => 'Emma Watson',
          'password' => 'password',
          'oArr' => array(
              'something' => array(
                  'somewhere' => '7th Avenue',
                  'ip' : => '192.168.0.31'
              ),
          ),
      );
    }
}

Once the user test has run (successfully), the new Emma Watson user (Mongo Doc) look as follows:

{
    _id": ObjectId("53021089b4d15220118b4568"),
   "email": "emma@example.com",
   "name": "Emma Watson",
   "password": "$2y$10$asdAzUGLAG.8asvWQOUum0k0u5vrcY98H.LEDeSJekEywUCV.ycttn6"
}

I have no idea how, and couldn't find anyone else with the same problem.

My assumption: The ORM I'm using (https://github.com/jenssegers/Laravel-MongoDB), doesn't cater for Object arrays in Unit Tests.

I have yet to look into how the ORM is handling unit tests, I'm just hoping someone else has had the same problem, and has a more basic solution.

Was it helpful?

Solution 2

Completely forgot about this post.
Laravel's Input class handles for JSON input also. So if you're trying to post embedded documents, you might as well do it in an JSON format. The following worked for me.

class AddUserTest extends TestCase {

    /**
     * A basic functional test example.
     *
     * @return void
     */


    public function testCreateUsers()
    {
      $post_data = array(
          'email' => 'emma@example.com',
          'name' => 'Emma Watson',
          'password' => 'password',
          'oArr' => array(
              'something' => array(
                  'somewhere' => '7th Avenue',
                  'ip' : => '192.168.0.31'
              ),
          ),
      );
      $post_data = json_encode($post_data);
      // Send through the serialized data to the Controller.
    }
}

OTHER TIPS

Well, your test returned modified document. Mongodb is automatically appending new _id to every insertion, that is not explicitly marked as update. Also mongo is hashing you password - it is probably done by you package driver. Since mongodb is using nested document, you test probably didn't reach that nested level, and there is no that document.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top