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

  • SEARCH
  • Home
  • 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 8500619
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T00:59:40+00:00 2026-06-11T00:59:40+00:00

I have very simple php interface: interface HasFormattableNumber { function setNumber(); function setFormat(); function

  • 0

I have very simple php interface:

interface HasFormattableNumber {
    function setNumber();
    function setFormat();
    function formatNumber();
}

Next, I have many classes implementing this interface. Unfortunately, these classes cannot have a common ancestor (possibly an abstract one which would implement these methods only once). The class basically looks like this:

class A implements HasFormattableNumber {
    private $_number;
    private $_format;

    public function setNumber($n) {
        $this->_number = $n;
    }
    public function setFormat($f) {
        $this->_format = $f;
    }
    public function formatNumber() {
        return sprintf($this->_format, $this->_number);
    }
}

An unit test would look something like this:

class ATest extends \PHPUnit_Framework_TestCase {
    public function testShouldFormatNumber() {
        $a = new A();
        $a->setNumber(123);
        $a->setFormat("xx-%d");
        $this->assertEquals("xx-123", $a->formatNumber());
    }
}

Is it possible to somehow reuse this test case for all of interface implementations? I know I can copy&paste testShouldFormatNumber() to all of my unit tests, but I prefer keeping it in one place if possible.

Would php Traits help in any way? (I’m not using php 5.4 yet, however)

  • 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-11T00:59:41+00:00Added an answer on June 11, 2026 at 12:59 am

    You can make a basetest that looks like your ATest except you don’t initialize the class A in it, but use a class variable that you will define in all your implementations. You place the file in a directory or with a name so that PHPUnit won’t run it. Then in your tests you can inherit from it and create the object as a class variable in setUp().

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

Sidebar

Related Questions

I have a very simple chat system I've built using PHP and MySQL (this
i have this very simple php script: <?php require 'functions.php'; $token = some-number; $id
I have a very strange issue with PHP. I run this very simple code:
I have a very simple test page for PHP session. <?php session_start(); if (isset($_SESSIONS['views']))
I have a very simple script that creates a user: <?php include 'mysqlserver.php'; session_start();
I have written a very very very simple!! script in php. header redirection not
I have the following PHP code doing a very simple select into a table.
So I have 4 pages. They are very simple. index.php (WORKS) <html> <form action=upload.php
I have a very simple php ldap script that is only failing when running
I have a very simple file upload PHP script. I am testing with a

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.