BestPracticesTest.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. <?php
  2. namespace Behat\Mink\Tests\Driver\Basic;
  3. use Behat\Mink\Tests\Driver\TestCase;
  4. /**
  5. * This testcase ensures that the driver implementation follows recommended practices for drivers.
  6. */
  7. class BestPracticesTest extends TestCase
  8. {
  9. public function testExtendsCoreDriver()
  10. {
  11. $driver = $this->createDriver();
  12. $this->assertInstanceOf('Behat\Mink\Driver\CoreDriver', $driver);
  13. return $driver;
  14. }
  15. /**
  16. * @depends testExtendsCoreDriver
  17. */
  18. public function testImplementFindXpath()
  19. {
  20. $driver = $this->createDriver();
  21. $this->assertNotImplementMethod('find', $driver, 'The driver should overwrite `findElementXpaths` rather than `find` for forward compatibility with Mink 2.');
  22. $this->assertImplementMethod('findElementXpaths', $driver, 'The driver must be able to find elements.');
  23. $this->assertNotImplementMethod('setSession', $driver, 'The driver should not deal with the Session directly for forward compatibility with Mink 2.');
  24. }
  25. /**
  26. * @dataProvider provideRequiredMethods
  27. */
  28. public function testImplementBasicApi($method)
  29. {
  30. $driver = $this->createDriver();
  31. $this->assertImplementMethod($method, $driver, 'The driver is unusable when this method is not implemented.');
  32. }
  33. public function provideRequiredMethods()
  34. {
  35. return array(
  36. array('start'),
  37. array('isStarted'),
  38. array('stop'),
  39. array('reset'),
  40. array('visit'),
  41. array('getCurrentUrl'),
  42. array('getContent'),
  43. array('click'),
  44. );
  45. }
  46. private function assertImplementMethod($method, $object, $reason = '')
  47. {
  48. $ref = new \ReflectionClass(get_class($object));
  49. $refMethod = $ref->getMethod($method);
  50. $message = sprintf('The driver should implement the `%s` method.', $method);
  51. if ('' !== $reason) {
  52. $message .= ' '.$reason;
  53. }
  54. $this->assertNotSame('Behat\Mink\Driver\CoreDriver', $refMethod->getDeclaringClass()->name, $message);
  55. }
  56. private function assertNotImplementMethod($method, $object, $reason = '')
  57. {
  58. $ref = new \ReflectionClass(get_class($object));
  59. $refMethod = $ref->getMethod($method);
  60. $message = sprintf('The driver should not implement the `%s` method.', $method);
  61. if ('' !== $reason) {
  62. $message .= ' '.$reason;
  63. }
  64. $this->assertSame('Behat\Mink\Driver\CoreDriver', $refMethod->getDeclaringClass()->name, $message);
  65. }
  66. }