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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T16:42:44+00:00 2026-06-14T16:42:44+00:00

Test method: public function convert(AbstractMessage $message) { $data = array(); // Text conversion $text

  • 0

Test method:

public function convert(AbstractMessage $message)
{
    $data = array();

    // Text conversion
    $text = $message->getText();

    if(null !== $text) {
        if(!is_string($text) && (is_object($text)
            && !method_exists($text, '__toString'))) {
            throw new UnexpectedTypeException(gettype($text), 'string');
        }

        $data['text'] = (string) $text;
    }
}

How can I mock a generic object (no matter the class) that has a __toString method?

  • 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-14T16:42:45+00:00Added an answer on June 14, 2026 at 4:42 pm
    <?php
    
    // UnderTest.php
    
    class UnderTest
    {
        public function hasTostring($obj)
        {
            return method_exists($obj, '__toString');
        }
    }
    
    
    
    // Observer.php
    
    class ObserverTest extends PHPUnit_Framework_TestCase
    {
        public function testHasTostring()
        {
    
            $tester = new UnderTest();
            $with = new WithToString();
            $without = new WithoutToString();
    
            $this->assertTrue($tester->hasTostring($with));
            $this->assertFalse($tester->hasTostring($without));
    
            // this automatically calls to string
            // and if the method toString doesnt exists - returns E_RECOVERABLE_ERROR
            // so this line should work
            $x = $with . '';
    
    
            // but this shouldnt work, because the method doesnt exist
            // therefore you are supposed to get an exception
            $this->setExpectedException('PHPUnit_Framework_Error');
            $x = $without . '';
        }
    }
    
    
            class WithToString
            {
                public function __toString() { return 'hi'; }
            }
    
            class WithoutToString{}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given the following method: public function foo($callback) { call_user_func($callback); } How would I test
I have this method: public void testJSNI2(){ String x = test; } I can
As in the title, I want to test a method like this: public void
I'm trying to write a method like this: public static T Test<T>() { if
MyClass.java: package test; public class MyClass { public void myMethod(){ System.out.println(My Method Called); }
I have a method like this, public List<T> Test<T>() { // do something. }
I have a method that prints private variable of the class. public class Test
<?php class ReportsController extends CController { public function __call($name,$argument) { echo test; } }
I´m trying to test if a protected method is called in a public interface.
Consider the following method that stops a service: Public Function StopService(ByVal serviceName As String,

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.