We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 38371
    • 42 Posts
    Hello to everyone,

    First of all, I am using MODx Revo 2.2.4pl.

    What I am trying to do is unit test the processors I have created for an Extra.

    I use the PHPUnit Framework 3.6 via NetBeans 7.2.

    I use a bootstrap file with the following content:

    define('MODX_CORE_PATH', '/path/to/modx/core/');
    define('MODX_CONFIG_KEY', 'config');
    require_once MODX_CORE_PATH.'model/modx/modx.class.php';
    require_once MODX_CORE_PATH.'model/modx/modprocessor.class.php';


    and, then on the setUp method of each test case I put:

    $this->modx = new modX();
    $this->modx->initialize('mgr');


    The problem I run into is that during one of my tests the error:
    "failure" method being called on a non-object

    is produced. Specifically, the test method's code is the following:

    $this->processor->setProperty('id', 7);
    $this->assertFalse( $this->processor->initialize() );


    and on the processor the "initialize" method code goes like this:

    $initialized = parent::initialize();
    if ($initialized !== true) {
        return $initialized;
    }
    		
    $this->categoryId = $this->getProperty('category_id');
    if ( empty($this->categoryId) ) {
        $this->failure('No category was specified.');
    
        return false;
    }
    		
    return true;


    So, as you can see, this method is supposed to halt initialization if no "category_id" parameter was given.
    The test case stops with the aforementioned error which means that "$this->processor" of the test case is a non-object. But, the processor is initialized and runs properly for all the other test methods of the same test case.

    Could anyone explain why this is happening?


    Thank you very much. [ed. note: leffyrant last edited this post 11 years, 7 months ago.]
      • 3749
      • 24,544 Posts
      I think initialize() returns true if it's initialized, but if not, the return value is undefined and probably not false.

      Try something like this:

      $this->assertFalse( $this->processor->initialize() === true);



      ------------------------------------------------------------------------------------------
      PLEASE, PLEASE specify the version of MODX you are using.
      MODX info for everyone: http://bobsguides.com/modx.html
        Did I help you? Buy me a beer
        Get my Book: MODX:The Official Guide
        MODX info for everyone: http://bobsguides.com/modx.html
        My MODX Extras
        Bob's Guides is now hosted at A2 MODX Hosting
        • 38371
        • 42 Posts
        Thank you for your answer BobRay.

        You are right that with the modification you propose the test passes. However, the "initialize" method of the parent should execute fine.

        It is my mistake that I did not mention that it is a modObjectUpdateProcessor I am talking about.

        So, it is my understanding that since I provide an "id" (that exists in the database) the "initialize" method of the parent should return true. And that is exactly the case on one of my other test methods where I write:

        $properties = array(
            'id' => 7,
            'category_id' => 1
        );
        $this->processor->setProperties($properties);
                
        $this->assertTrue( $this->processor->initialize() );


        The test case that causes the problem is expected to fail on my custom "initialize" method (see first post) and return false.

        Do you see any reason why the modObjectUpdateProcessor#initialize method would fail when a valid id is provided?


        P.S.: The returned message is the one with the "_err_nfs" suffix that is produced when the object cannot be retrieved from the database.
          • 3749
          • 24,544 Posts
          My only guess would be that either the object's primaryKeyField or classKey are not set properly.

          On the plus side, looking at that code, I see why people are getting untranslated _err_nfs error messages. wink



          ------------------------------------------------------------------------------------------
          PLEASE, PLEASE specify the version of MODX you are using.
          MODX info for everyone: http://bobsguides.com/modx.html
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 38371
            • 42 Posts
            I found a workaround for this issue. I have just changed the order of execution in my custom "initialize" method so that all of my custom validation precedes the modObjectUpdateProcessor's one.

            So, now my "initialize" method looks like this:

            $requiredFieldsSet = $this->requiredFieldsAreSet();
            if ($requiredFieldsSet !== true) {
                return $requiredFieldsSet;
            }
            
            return parent::initialize();


            But, now I have come across another problem that is also "processor testing" related. I will see to it some more and, perhaps, I will create another post about it.

            Thank you very much for your answers, BobRay.