StepTest.php 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. <?php
  2. use Facebook\WebDriver\WebDriverBy;
  3. use Codeception\Util\Locator;
  4. class StepTest extends \PHPUnit_Framework_TestCase
  5. {
  6. /**
  7. * @param $args
  8. * @return Codeception\Step
  9. */
  10. protected function getStep($args)
  11. {
  12. return $this->getMockBuilder('\Codeception\Step')->setConstructorArgs($args)->setMethods(null)->getMock();
  13. }
  14. public function testGetArguments()
  15. {
  16. $by = WebDriverBy::cssSelector('.something');
  17. $step = $this->getStep([null, [$by]]);
  18. $this->assertEquals('"' . Locator::humanReadableString($by) . '"', $step->getArgumentsAsString());
  19. $step = $this->getStep([null, [['just', 'array']]]);
  20. $this->assertEquals('["just","array"]', $step->getArgumentsAsString());
  21. $step = $this->getStep([null, [function () {
  22. }]]);
  23. $this->assertEquals('"Closure"', $step->getArgumentsAsString());
  24. $step = $this->getStep([null, [[$this, 'testGetArguments']]]);
  25. $this->assertEquals('["StepTest","testGetArguments"]', $step->getArgumentsAsString());
  26. $step = $this->getStep([null, [['PDO', 'getAvailableDrivers']]]);
  27. $this->assertEquals('["PDO","getAvailableDrivers"]', $step->getArgumentsAsString());
  28. }
  29. public function testGetHtml()
  30. {
  31. $step = $this->getStep(['Do some testing', ['arg1', 'arg2']]);
  32. $this->assertSame('I do some testing <span style="color: #732E81">"arg1","arg2"</span>', $step->getHtml());
  33. $step = $this->getStep(['Do some testing', []]);
  34. $this->assertSame('I do some testing', $step->getHtml());
  35. }
  36. public function testLongArguments()
  37. {
  38. $step = $this->getStep(['have in database', [str_repeat('a', 2000)]]);
  39. $output = $step->toString(200);
  40. $this->assertLessThan(201, strlen($output), 'Output is too long: ' . $output);
  41. $step = $this->getStep(['have in database', [str_repeat('a', 100), str_repeat('b', 100)]]);
  42. $output = $step->toString(50);
  43. $this->assertEquals(50, strlen($output), 'Incorrect length of output: ' . $output);
  44. $this->assertEquals('have in database "aaaaaaaaaaa...","bbbbbbbbbbb..."', $output);
  45. $step = $this->getStep(['have in database', [1, str_repeat('b', 100)]]);
  46. $output = $step->toString(50);
  47. $this->assertEquals('have in database 1,"bbbbbbbbbbbbbbbbbbbbbbbbbb..."', $output);
  48. $step = $this->getStep(['have in database', [str_repeat('b', 100), 1]]);
  49. $output = $step->toString(50);
  50. $this->assertEquals('have in database "bbbbbbbbbbbbbbbbbbbbbbbbbb...",1', $output);
  51. }
  52. public function testArrayAsArgument()
  53. {
  54. $step = $this->getStep(['see array', [[1,2,3], 'two']]);
  55. $output = $step->toString(200);
  56. $this->assertEquals('see array [1,2,3],"two"', $output);
  57. }
  58. public function testSingleQuotedStringAsArgument()
  59. {
  60. $step = $this->getStep(['see array', [[1,2,3], "'two'"]]);
  61. $output = $step->toString(200);
  62. $this->assertEquals('see array [1,2,3],"\'two\'"', $output);
  63. }
  64. public function testSeeUppercaseText()
  65. {
  66. $step = $this->getStep(['see', ['UPPER CASE']]);
  67. $output = $step->toString(200);
  68. $this->assertEquals('see "UPPER CASE"', $output);
  69. }
  70. public function testMultiByteTextLengthIsMeasuredCorrectly()
  71. {
  72. $step = $this->getStep(['see', ['ŽŽŽŽŽŽŽŽŽŽ', 'AAAAAAAAAAA']]);
  73. $output = $step->toString(30);
  74. $this->assertEquals('see "ŽŽŽŽŽŽŽŽŽŽ","AAAAAAAAAAA"', $output);
  75. }
  76. public function testAmOnUrl()
  77. {
  78. $step = $this->getStep(['amOnUrl', ['http://www.example.org/test']]);
  79. $output = $step->toString(200);
  80. $this->assertEquals('am on url "http://www.example.org/test"', $output);
  81. }
  82. public function testNoArgs()
  83. {
  84. $step = $this->getStep(['acceptPopup', []]);
  85. $output = $step->toString(200);
  86. $this->assertEquals('accept popup ', $output);
  87. $output = $step->toString(-5);
  88. $this->assertEquals('accept popup ', $output);
  89. }
  90. public function testSeeMultiLineStringInSingleLine()
  91. {
  92. $step = $this->getStep(['see', ["aaaa\nbbbb\nc"]]);
  93. $output = $step->toString(200);
  94. $this->assertEquals('see "aaaa\nbbbb\nc"', $output);
  95. }
  96. }