title: PHPUnit > How to test whether the correct type was passed to a method?
body: <p>I have a method where I've set the type of first argument to be passed (array):</p>
  
  <pre><code>public function create(array $values, $options=array())
  {
      // set datetime columns
      if(! isset($values['date_created']) or empty($values['date_created'])) {
          $values['date_created'] = date("Y-m-d H:i:s");
      }
      if(! isset($values['date_updated']) or empty($values['date_updated'])) {
          $values['date_updated'] = date("Y-m-d H:i:s");
      }
  
      return parent::create($values);
  }
  </code></pre>
  
  <p>How do I test this using PHPUnit, it keeps throwing a fatal error which I guess is to be expected but it doesn't allow my script to run:</p>
  
  <pre><code>1) UserTableTest::testCreateMethodWithInvalidArguments
  Failed asserting that exception of type "PHPUnit_Framework_Error" matches expected exception "InvalidArgumentException". Message was: "Argument 1 passed to app\models\UserTable::create() must be of the type array, string given, called in /var/www/phpdev/tests/models/UserTableMockTest.php on line 82 and defined" at
  #0 /var/www/phpdev/app/models/UserTable.php(16): PHPUnit_Util_ErrorHandler::handleError(4096, 'Argument 1 pass...', '/var/www/phpdev...', 16, Array)
  #1 /var/www/phpdev/tests/models/UserTableMockTest.php(82): app\models\UserTable-&gt;create('invalid argumen...')
  #2 [internal function]: UserTableTest-&gt;testCreateMethodWithInvalidArguments()
  #3 /var/www/phpdev/vendor/phpunit/phpunit/src/Framework/TestCase.php(962): ReflectionMethod-&gt;invokeArgs(Object(UserTableTest), Array)
  #4 /var/www/phpdev/vendor/phpunit/phpunit/src/Framework/TestCase.php(826): PHPUnit_Framework_TestCase-&gt;runTest()
  #5 /var/www/phpdev/vendor/phpunit/phpunit/src/Framework/TestResult.php(686): PHPUnit_Framework_TestCase-&gt;runBare()
  #6 /var/www/phpdev/vendor/phpunit/phpunit/src/Framework/TestCase.php(760): PHPUnit_Framework_TestResult-&gt;run(Object(UserTableTest))
  #7 /var/www/phpdev/vendor/phpunit/phpunit/src/Framework/TestSuite.php(699): PHPUnit_Framework_TestCase-&gt;run(Object(PHPUnit_Framework_TestResult))
  #8 /var/www/phpdev/vendor/phpunit/phpunit/src/TextUI/TestRunner.php(426): PHPUnit_Framework_TestSuite-&gt;run(Object(PHPUnit_Framework_TestResult))
  #9 /var/www/phpdev/vendor/phpunit/phpunit/src/TextUI/Command.php(179): PHPUnit_TextUI_TestRunner-&gt;doRun(Object(PHPUnit_Framework_TestSuite), Array)
  #10 /var/www/phpdev/vendor/phpunit/phpunit/src/TextUI/Command.php(132): PHPUnit_TextUI_Command-&gt;run(Array, true)
  #11 /var/www/phpdev/vendor/phpunit/phpunit/phpunit(55): PHPUnit_TextUI_Command::main()
  #12 {main}.
  
  FAILURES!                            
  Tests: 1, Assertions: 1, Failures: 1.
  </code></pre>
  
  <p>Should I instead remove the type hint and use is_array($values) to tell whether the correct type is passed then throw an exception? In PHPUnit I am expecting an exception is thrown anyway. By the way, below is my test:</p>
  
  <pre><code>/**
  * @expectedException InvalidArgumentException
  */
  public function testCreateMethodWithInvalidArguments()
  {
      // pass invalid argument, should be an array of values
      $result = $this-&gt;userTable-&gt;create('invalid argument type');
  }
  </code></pre>