1

I want to test a model in zend project,

<?php

//require_once('CustomModelBase.php');

class Application_Model_User extends Custom_Model_Base {


    protected function __construct() {
        parent::__construct();
    }

    static function create(array $data) {

    }

    static function load($id) {

    }

    static function find($name, $order=null, $limit=null, $offset=null) {

        );
    }

}

the model in under application/model folder, it extends a base class Custom_Model_Base which is under the same folder as class User.

In my test, I try to create a new object of User in this way

    <?php


class Model_UserTest extends ControllerTestCase

{
    protected $user2;

    public function setUp() {

        parent::setUp();

        $this->user2 = new Application_Model_User2();
    }

    public function testCanDoTest() {
        $this->assertTrue(true);
    }

}

this is CustomModelBase.php: abstract class Custom_Model_Base { protected function __construct($adapter=null) {} }

it gives me error, say "PHP Fatal error: Class 'Custom_Model_Base' not found in \application\models\User.php on line 4", the I include "CustomModelBase.php" in User.php,it gives me another error "PHP Fatal error: Call to protected Application_Model_User::__construct() from context 'Model_User2Test' in D:\PHP\apache2\htdocs\ID24_Xiao\tests\application\models \UserTest.php on line 13"

then How could I handle it? can anyone give some suggestion?

razlebe
  • 7,134
  • 6
  • 42
  • 57
jizae
  • 11
  • 1
  • 2
    Is there a reason for your constructor to be protected? – JF Dion May 09 '11 at 19:45
  • possible duplicate of [Best practices to test protected methods with PHPUnit](http://stackoverflow.com/questions/249664/best-practices-to-test-protected-methods-with-phpunit) – Gordon May 11 '11 at 07:37
  • Not the solution to your question, but should that be $this->user2 = new Application_Model_User(); instead of $this->user2 = new Application_Model_User2(); – Jannie Theunissen Aug 05 '11 at 12:44

3 Answers3

2

If you use 5.3.2 or better you could do it this way:

public function testCanDoTest() {

    // set method "nameOfProctedMethod" to accessible on Class App...
    $method = new ReflectionMethod(
        'Application_Model_User2', 'nameOfProtectedMethod'
    );

    $method->setAccessible(true);
    $this->assertTrue($method->doSomething());
}
opHASnoNAME
  • 20,224
  • 26
  • 98
  • 143
  • actually the protected method is the constructor, is it the same to create a object being tested? –  May 10 '11 at 06:45
  • ReflectionMethod() is not defined in zend test? –  May 10 '11 at 06:55
  • no,php5.2, how can I update the php version without affecting the phpunit installation and other parts? –  May 10 '11 at 09:12
  • $method should be called by calling invoke (that can take the object as argument). – AsTeR Aug 14 '12 at 22:30
0

You can not call any protected member of class from outside the class.
Either change the access modifier from protected to public
Or create a static function which will give the instance of that class, for e.g.

static function getInstance(){
    return new Model_UserTest();
}
Gaurav
  • 28,447
  • 8
  • 50
  • 80
0

As Jeff has said, you can make your constructor testable by making it public like this:

 public function __construct() { ...
Jannie Theunissen
  • 28,256
  • 21
  • 100
  • 127