0

I am doing some unit testing in Symfony2 which I am quiet new to. I have a very basic class:

<?php
namespace MyBundle\TestBundle\Dto;

class AddressResponse
{
    /**
     * @var string
     */
    public $name;

    /**
     * @var boolean
     */
    public $surname;

    /**
     * @var array
     */
    public $addresses = array();

    /**
     * @param $name
     * @param $surname
     * @param array $addresses
     */
    public function __construct($name, $surname, array $addresses)
    {
        $this->name = $name;
        $this->surname = $surname;
        $this->addresses = $addresses;
    }
}

My Test Class:

<?php

namespace MyBundle\TestBundle\Tests\Dto;

use MyBundle\TestBundle\Dto\AddressResponse;
/**
 * Generated by PHPUnit_SkeletonGenerator on 2015-05-19 at 14:01:52.
 */
class AddressResponseTest extends \PHPUnit_Framework_TestCase
{

    /**
     * @var AddressResponse
     */
    protected $object;

    public $name;

    public $surname;

    public $address = array();

    /**
     * Sets up the fixture, for example, opens a network connection.
     * This method is called before a test is executed.
     */
    protected function setUp()
    {
        $this->object = $this->createAddressResponseInstance();
    }

    public function createAddressResponseInstance()
    {
        return new AddressResponse(
            $this->name,
            $this->surname,
            $this->address
        );
    }

    public function testAddressResponse()
    {
        $this->assertTrue($this->object->name, $this->object->surname, $this->object->addresses);
    }

}

The above test is all I could come up for my simple calss to test but i get this error message: Cannot assert null on true.

And how else could I test my simple class apart form AssertTrue

1
  • 1
    1. It totaly irrelevant to Symfony; 2. assertTrue method checks only one parameter - check out the documentation; 3. Your unit test is invalid - the assertion is wrong (those filed aren't true, they're strings with some values) Commented May 28, 2015 at 11:55

1 Answer 1

1

as @Crozin already commented, your problems seem to be more with phpunit and the assert used. And additionally you don't have that much to test if your class contains only the variables and the constructor...

If you really want to test that the constructor stores the values correctly (in general, or just for the sake of practice), you could write your test class for instance in the following way (this would be just my preference on how to write it - you can ofc use the class variables and setUp function as well to achieve the same thing):

<?php

namespace MyBundle\TestBundle\Tests\Dto;

use MyBundle\TestBundle\Dto\AddressResponse;
/**
 * Generated by PHPUnit_SkeletonGenerator on 2015-05-19 at 14:01:52.
 */
class AddressResponseTest extends \PHPUnit_Framework_TestCase
{
    public function createAddressResponseInstance($name, $surname, $address)
    {
        return new AddressResponse($name, $surname, $address);
    }

    public function testAddressResponse()
    {
        $name      = "John";
        $surname   = "Doe";
        $addresses = null; // for example, you can be more creative here

        $object = $this->createAddressResponseInstance($name, $surname, $addresses);

        // test the constructor
        $this->assertEquals($name, $object->name);
        $this->assertEquals($surname, $object->surname);
        $this->assertNull($object->addresses);
    }
}

Check phpunit manual for more assert options...

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.