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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T17:10:39+00:00 2026-05-30T17:10:39+00:00

How do you test multiple values for same Attribute ? class Test { private

  • 0

How do you test multiple values for same Attribute ?

class Test {

    private $_optionalValue = null;

    function setValue(String $optionalValue)
    {
        $this->_optionalValue = $optionalValue;
    }
}

So here, “$_optionalValue” could be NULL or User defined value, but when I check with phpunit like this :

$optionalValue = PHPUnit_Util_Class::getObjectAttribute($my_object, '_optionalValue');

$this->assertThat(
    $optionalValue,
    $this->logicalXor(
        $this->assertNull($optionalValue),
        $this->logicalAnd(
            $this->assertAttributeInternalType('string', '_optionalValue', $optionalValue),
            $this->assertRegExp('/[0-9]{2}:[0-9]{2}:[0-9]{2}/', (string) $optionalValue)
        )
    )
);

The Regexp assertion fails because $optionalValue isn’t a String (it’s null by default)

  • 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-30T17:10:41+00:00Added an answer on May 30, 2026 at 5:10 pm

    You’re testing a private property of an object which should be generally avoided because that’s an internal of the unit and you should not care about that.

    If your unit needs to validate values of that class, it’s likely that generally a value of some kind should be validated.

    You could therefore encapsulate the logic of the validation into a unit of it’s own, e.g. a validator:

    class FooValueValidator implements Validator {
        /**
         * @var string
         */
        private $value;
    
        /**
         * @var string
         */
        private $regex = '/[0-9]{2}:[0-9]{2}:[0-9]{2}/';
    
        public function __construct($value) {
            $this->value = $value;
        }
    
        /**
         * @return bool
         */
        public function isValid() {
    
            if (is_null($this->value)) {
                return TRUE;
            }
    
            if (!is_string($this->value)) {
                return FALSE;
            }
    
            $result = preg_match($this->pattern, $this->value);
            if (FALSE === $result) {
                throw new Exception(sprintf('Regular expression failed.'));
            }
            return (bool) $result;
        }
    }
    

    You can then write unit-tests for the validator. You then know that your validator works and you can use it everywhere you like.

    class Test {
    
        private $_optionalValue = null;
    
        /**
         * @var Validator
         */
        private $_validator;
    
        public function __construct(Validator $validator) {
            $this->_validator = $validator;
        }
    
        function setValue(String $optionalValue)
        {
            if (!$this->validator->isValid($optionalValue)) {
                throw new InvalidArgumentException(sprintf('Invalid value "%s".', $optionalValue));
            }
            $this->_optionalValue = $optionalValue;
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I update multiple values in MySQL? This didn't work: UPDATE test SET
Do you write one test per function/method, with multiple checks in the test, or
I would like to update multiple elements(with different values) from the same selector. What
We have multiple environments (development, test, Production, etc). Using Oracle 10g. All values are
Is it possible to test for multiple exceptions in a single JUnit unit test?
How can I test my website in multiple versions of firefox? I have v2
I need to test my PHP applications with multiple versions of PHP 5.x, such
Say there are multiple requests in a integration test, some of them are local
I have a unix shell script which test ftp ports of multiple hosts listed
We have multiple config files (app.DEV.config, app.TEST.config, etc) and a pre-build event that copies

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.