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 6032917
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:24:24+00:00 2026-05-23T05:24:24+00:00

In my TDD project I am trying to test a method in an abstract

  • 0

In my TDD project I am trying to test a method in an abstract class.

abstract class Database_Mapper_Abstract
{

    public function setTable($sTablename){
        return('foo');

    }
}

This is the way I wrote my simple test:

public function testCanSetTable(){
        $oMock = $this->getMockForAbstractClass('JCMS_Database_Mapper_Abstract');
        $oMock->expects($this->once())
              ->method('setTable')
              ->with($this->equalTo('foo'))
              ->will($this->returnValue('foo'));
        $this->assertEquals('foo',$oMock->setTable());
    }

When I run this test i get the following error:

PHPUnit 3.5.13 by Sebastian Bergmann.

E

Time: 1 second, Memory: 6.75Mb

There was 1 error:

1)
Database_Mapper_AbstractTest::testCanSetTable
Missing argument 1 for
Database_Mapper_Abstract::setTable(), called in
K:\xampp\htdocs\tests\library\Database\Mapper\Abstract.php
on line 15 and defined

K:\xampp\htdocs\library\Database\Mapper\Abstract.php:4
K:\xampp\htdocs\tests\library\Database\Mapper\Abstract.php:15

FAILURES! Tests: 1, Assertions: 0,
Errors: 1.

The way I understand this is that it can’t find the argument for the setTable function.
But I set it with the with() method. I also tried with('foo'). That also doesn’t help me.

Does anyone have an idea?

  • 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-05-23T05:24:25+00:00Added an answer on May 23, 2026 at 5:24 am

    Testing an abstract class:

    For testing an abstract class you don’t want to use the “create behavior methods”.

    Just getMockForAbstractClass() like this:

    <?php
    abstract class JCMS_Database_Mapper_Abstract
    {
    
        public function setTable($sTablename){
            return $sTablename."_test";
    
        }
    }
    
    class myTest extends PHPUnit_Framework_TestCase {
    
        public function testCanSetTable(){
            $oMock = $this->getMockForAbstractClass('JCMS_Database_Mapper_Abstract');
    
            $this->assertEquals('foo_test', $oMock->setTable('foo'));
        }
    
    }
    

    You just use the mocking functionality to create an instance of that abstract class and test against that.

    It’s only a shortcut for writing

    class MyDataMapperAbstractTest extends JCMS_Database_Mapper_Abstract {
        // and filling out the methods
    }
    

    The actual error:

    What happens is that you have a method with one parameter:

    public function setTable($sTablename){
    

    but you call it with zero paremters:

    $oMock->setTable()
    

    so you get an error from PHP and if PHP throws a warnings PHPUnit will show you an error.

    Reproduce:

    <?php
    abstract class JCMS_Database_Mapper_Abstract
    {
    
        public function setTable($sTablename){
            return('foo');
    
        }
    }
    
    class myTest extends PHPUnit_Framework_TestCase {
    
        public function testCanSetTable(){
            $oMock = $this->getMockForAbstractClass('JCMS_Database_Mapper_Abstract');
            $oMock->expects($this->once())
                  ->method('setTable')
                  ->with($this->equalTo('foo'))
                  ->will($this->returnValue('foo'));
            $this->assertEquals('foo',$oMock->setTable());
        }
    
    }
    

    Results in:

     phpunit blub.php
    PHPUnit 3.5.13 by Sebastian Bergmann.
    
    E
    
    Time: 0 seconds, Memory: 3.50Mb
    
    There was 1 error:
    
    1) myTest::testCanSetTable
    Missing argument 1 for JCMS_Database_Mapper_Abstract::setTable(), called in /home/.../blub.php on line 19 and defined
    

    Fixing

    Change:

    $this->assertEquals('foo',$oMock->setTable());
    

    to

    $this->assertEquals('foo',$oMock->setTable('foo'));
    

    then you don’t get a PHP Warning and it should work out 🙂

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

Sidebar

Related Questions

Converting my current code project to TDD, I've noticed something. class Foo { public
I'm trying to learn TDD by applying it to a simple project of mine.
Recently, I have worked in a project were TDD (Test Driven Development) was used.
When doing TDD , how to tell that's enough tests for this class /
I'm developing a project using BDD/TDD techniques and I'm trying my best to stay
Using TDD, I'm considering creating an (throw-away) empty project as Test-harness/container for each new
I have a project I am trying to learn unit testing and TDD practices
I am doing a TDD project using EF. I want to unit test the
I have a large project for which I am attempting to use TDD. I
I'm learning to do TDD in practice in small project. I want to create

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.