Skip to content

Commit

Permalink
Add assertCount() method (#1158)
Browse files Browse the repository at this point in the history
* add assertCount()

* Update MakesAssertionsTest.php

* style ci
  • Loading branch information
ktsm-th authored Feb 5, 2025
1 parent 0b4ba91 commit f0e6870
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/Concerns/MakesAssertions.php
Original file line number Diff line number Diff line change
Expand Up @@ -249,6 +249,26 @@ public function assertSeeNothingIn($selector)
return $this;
}

/**
* Assert that a given element is present a given amount of times.
*
* @param string $selector
* @param int $expected
* @return $this
*/
public function assertCount($selector, $expected)
{
$fullSelector = $this->resolver->format($selector);

PHPUnit::assertCount(
$expected,
$this->resolver->all($selector),
"Expected element [{$fullSelector}] exactly {$expected} times."
);

return $this;
}

/**
* Assert that the given JavaScript expression evaluates to the given value.
*
Expand Down
26 changes: 26 additions & 0 deletions tests/Unit/MakesAssertionsTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -1257,6 +1257,32 @@ public function test_assert_vue_contains_with_no_result()
}
}

public function test_assert_count()
{
$driver = m::mock(stdClass::class);

$element1 = m::mock(RemoteWebElement::class);
$element2 = m::mock(RemoteWebElement::class);

$resolver = m::mock(stdClass::class);
$resolver->shouldReceive('format')->with('foo')->andReturn('body foo');
$resolver->shouldReceive('all')->with('foo')->andReturn([$element1, $element2]);

$browser = new Browser($driver, $resolver);

$browser->assertCount('foo', 2);

try {
$browser->assertCount('foo', 3);
$this->fail();
} catch (ExpectationFailedException $e) {
$this->assertStringContainsString(
'Expected element [body foo] exactly 3 times.',
$e->getMessage()
);
}
}

public function test_assert_script()
{
$driver = m::mock(stdClass::class);
Expand Down

0 comments on commit f0e6870

Please sign in to comment.