PHP-Programmierer chatten hier
Anonymous
Ich weiß nicht, wie ich von at () zu einmal () in Phpunit 10 und höher wechseln soll
Post
by Anonymous » 07 Apr 2025, 06:11
Die Methodenfunktion
at () wurde später von Phpunit 10 entfernt.
Code: Select all
...
/**
* KernelEvents::REQUEST => 'onRequest'
*/
public function onRequest(RequestEvent $event) {
...
$termStorage = $this->entityTypeManager->getStorage('taxonomy_term');
$query = $termStorage->getQuery()
->condition('vid', $vocabulary_id)
->condition('name', $name);
$termIds = $query->execute();
....
...
< /code>
Unittest Code: < /p>
...
public function testOnRequest() {
...
$query = $this->createMock(QueryInterface::class);
$query->expects($this->once())
->method('condition')
->with('vid', 'test_tags')
->willReturnSelf();
$query->expects($this->once())
->method('condition')
->with('name', 'Existing Tag')
->willReturnSelf();
$query->expects($this->once())
->method('execute')
->willReturn([123]);
$termStorage = $this->createMock(EntityStorageInterface::class);
$termStorage->expects($this->once())
->method('getQuery')
->willReturn($query);
$this->entityTypeManager->expects($this->once())
->method('getStorage')
->with('taxonomy_term')
->willReturn($termStorage);
...
}
...
< /code>
Führen Sie Phpunit aus: < /p>
...
Expectation failed for method name is "condition" when invoked 1 time
Parameter 0 for invocation Drupal\Core\Entity\Query\QueryInterface::condition('vid', 'test_tags', null, null) does not match expected value.
Failed asserting that two strings are equal.
--- Expected
+++ Actual
@@ @@
-'name'
+'vid'
...
Wie schreibe ich Testcode?
1743999073
Anonymous
Die Methodenfunktion [b] at () [/b] wurde später von Phpunit 10 entfernt.[code]... /** * KernelEvents::REQUEST => 'onRequest' */ public function onRequest(RequestEvent $event) { ... $termStorage = $this->entityTypeManager->getStorage('taxonomy_term'); $query = $termStorage->getQuery() ->condition('vid', $vocabulary_id) ->condition('name', $name); $termIds = $query->execute(); .... ... < /code> Unittest Code: < /p> ... public function testOnRequest() { ... $query = $this->createMock(QueryInterface::class); $query->expects($this->once()) ->method('condition') ->with('vid', 'test_tags') ->willReturnSelf(); $query->expects($this->once()) ->method('condition') ->with('name', 'Existing Tag') ->willReturnSelf(); $query->expects($this->once()) ->method('execute') ->willReturn([123]); $termStorage = $this->createMock(EntityStorageInterface::class); $termStorage->expects($this->once()) ->method('getQuery') ->willReturn($query); $this->entityTypeManager->expects($this->once()) ->method('getStorage') ->with('taxonomy_term') ->willReturn($termStorage); ... } ... < /code> Führen Sie Phpunit aus: < /p> ... Expectation failed for method name is "condition" when invoked 1 time Parameter 0 for invocation Drupal\Core\Entity\Query\QueryInterface::condition('vid', 'test_tags', null, null) does not match expected value. Failed asserting that two strings are equal. --- Expected +++ Actual @@ @@ -'name' +'vid' ... [/code] Wie schreibe ich Testcode?
0 Replies
28 Views
Last post by Guest
14 Jan 2025, 10:42
0 Replies
13 Views
Last post by Anonymous
06 Feb 2025, 04:47
0 Replies
9 Views
Last post by Anonymous
03 Mar 2025, 20:16
0 Replies
15 Views
Last post by Guest
14 Feb 2025, 03:11
0 Replies
18 Views
Last post by Guest
14 Feb 2025, 03:11