Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7944413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T00:32:38+00:00 2026-06-04T00:32:38+00:00

I am new to phpunit and unit testing in general. I am attempting to

  • 0

I am new to phpunit and unit testing in general. I am attempting to convert a large app to cakephp 2.0 and make unit tests for everything.

I am trying to create a mock object in which when, $this->Session->read(‘Auth.Account.id’) is called, it returns 144… this will give an account with an id that has items.

however, I get an error as the Mock seems to be erroring out on other Session::read(‘AuthCode’) calls in various beforeFilter calls;

Expectation failed for method name is equal to when invoked 1 time(s)
Parameter 0 for invocation SessionComponent::read(‘AuthCode’) does not match expected value.
Failed asserting that two strings are equal.

Like I said I am new to phpunit and unit testing… what am I doing wrong?

class PagesController extends MastersController {
        public function support(){
        if($this->Session->read('Auth.Account.id')) {
            $items = $this->Account->Items->find('list', array('conditions'=>array('Items.account_id'=>$this->Session->read('Auth.Account.id'))));           
        }
        $this->set(compact('items'));
    }
}


class PagesControllerTestCase extends CakeTestCase {
        /**
     * Test Support
     *
     * @return void
     */
    public function testSupport() { 
        #mock controller
        $this->PagesController = $this->generate('Pages', array(
            'methods'=>array('support'),
            'components' => array(
                'Auth',
                'Session',
            ),
        ));

                #mock controller expects
        $this->PagesController->Session->expects(
            $this->once())
                ->method('read') #Session:read() method will be called at least once
                ->with($this->equalTo('Auth.Account.id')) #when read method is called with 'Auth.Account.id' as a param
                ->will($this->returnValue(144)); #will return value 144


        #test action
        $this->testAction('support');
    }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-04T00:32:39+00:00Added an answer on June 4, 2026 at 12:32 am

    I’ve decided to just write the Sessions out manually. Like they do is the SessionComponentTest in the core.

    https://github.com/cakephp/cakephp/blob/master/lib/Cake/Test/Case/Controller/Component/SessionComponentTest.php

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm a relatively new convert to unit testing in general and I've run into
I'm implementing unit testing with PHPUnit on an existing codebase. I'm fairly new to
I'm still new to Unit testing, and specifically PHPUnit as the testing framework. Suppose
I am quite new to unit testing and testing in general. I am developing
I'm new to PHPUnit, and I'm having some trouble with unit testing HTML output.
Well, I'm new to unit-testing (with phpUnit) and just started to test one class
When I'm unit-testing my php code with PHPUnit, I'm trying to figure out the
I'm new to unit testing and PHPUnit, but I've reading a lot lately about
Currently, I'm trying to apply unit testing to a project for the first time.
I'm trying to write a unit test for a controller using Zend and PHPUnit

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.