title: How do I test for an exact Exception message, rather than a substring, with PHPUnit?
body: <p>According to the <a href="http://phpunit.de/manual/3.7/en/appendixes.annotations.html#appendixes.annotations.expectedExceptionMessage" rel="noreferrer">PHPUnit Documentation</a> on <code>@expectedExceptionMessage</code>, the string must only be a substring of the actual <code>Exception</code> thrown.</p>
  
  <p>In one of my validation methods, an array item is pushed for each error that occurs, and the final <code>Exception</code> message is displayed by imploding the array of errors. </p>
  
  <pre class="lang-php prettyprint-override"><code>class MyClass
  {
      public function validate($a, $b, $c, $d)
      {
          if($a &lt; $b) $errors[] = "a &lt; b.";
          if($b &lt; $c) $errors[] = "b &lt; c.";
          if($c &lt; $d) $errors[] = "c &lt; d.";
  
          if(count($errors) &gt; 0) throw new \Exception(trim(implode(" ", $errors)));
      }
  }
  </code></pre>
  
  <p>The problem I have here is that in the PHPUnit test method I check for different combinations. This causes tests to pass that I intend to fail.</p>
  
  <pre class="lang-php prettyprint-override"><code>/**
   * @expectedException \Exception
   * @expectedExceptionMessage a &lt; b.
   */
  public function testValues_ALessBOnly()
  {
      $myClass = new MyClass()
      $myClass-&gt;validate(1, 2, 4, 3);
  }
  </code></pre>
  
  <p>The string of the <code>Exception</code> message is actually <code>"a &lt; b. b &lt; c."</code> but this test still passes. I intend for this test to fail because the message is not exactly what I expect.</p>
  
  <p><strong>Is there a way with PHPUnit to expect an exact string, rather than a substring?</strong> I hope to <em>avoid</em> the following:</p>
  
  <pre class="lang-php prettyprint-override"><code>public function testValues_ALessBOnly()
  {
      $myClass = new MyClass()
      $fail = FALSE;
  
      try
      {
          $myClass-&gt;validate(1, 2, 4, 3);
      }
      catch(\Exception $e)
      {
          $fail = TRUE;
          $this-&gt;assertEquals($e-&gt;getMessage(), "a &lt; b.";
      }
  
      if(!$fail) $this-&gt;fail("No Exceptions were thrown.");
  }
  </code></pre>