Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHPUnit][PHP7.1] Działanie typów zwracanych w PHPUnit
czychacz
post
Post #1





Grupa: Zarejestrowani
Postów: 189
Pomógł: 13
Dołączył: 20.09.2008
Skąd: Lublin

Ostrzeżenie: (0%)
-----


Cytat
G:\htdocs\project>.\vendor\bin\phpunit
PHP Fatal error: Cannot use 'null' as class name as it is reserved in G:\htdocs
\project\src\Named\NamedTrait.php on line 32


Na taki błąd natknąłem się dzisiaj, gdy chciałem napisać testy jednostkowe dla mojej aplikcaji. Podobnymi błędami rzuca w przypadku typu void.
Czy to standardowy błąd PHPUnit, czy może trzeba po prostu coś inaczej zrobić?
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
czychacz
post
Post #2





Grupa: Zarejestrowani
Postów: 189
Pomógł: 13
Dołączył: 20.09.2008
Skąd: Lublin

Ostrzeżenie: (0%)
-----


  1. <?php
  2.  
  3. namespace Project\Named;
  4.  
  5. trait NamedTrait
  6. {
  7.  
  8. /**
  9.   * Table name
  10.   *
  11.   * @var string
  12.   */
  13. protected $name;
  14.  
  15. public function __construct(string $name)
  16. {
  17. $this->setName($name);
  18. }
  19.  
  20. public function getName(): string
  21. {
  22. return $this->name;
  23. }
  24.  
  25. protected function setName(string $name): void
  26. {
  27. $this->name = $name;
  28. }
  29. }

  1. <?php
  2.  
  3. namespace Project;
  4.  
  5. use Project\Named\NamedTrait;
  6.  
  7. class Table
  8. {
  9.  
  10. use NamedTrait {
  11. NamedTrait::__construct as protected namedConstruct;
  12. }
  13.  
  14. public function __construct($name)
  15. {
  16. $this->namedConstruct($name);
  17. }
  18. }
  1. <?php
  2.  
  3. use PHPUnit\Framework\TestCase;
  4. use Project\Table;
  5.  
  6. class TableTest extends TestCase
  7. {
  8.  
  9. const TABLE_NAME_TEST = 'test';
  10.  
  11. /**
  12.   * @dataProvider providerTable
  13.   */
  14. public function testTable(Table $table)
  15. {
  16. $this->assertInstanceOf(\Project\Table::class, $table);
  17. $this->assertEquals(self::TABLE_NAME_TEST, $table->getName());
  18. }
  19.  
  20. public function providerTable()
  21. {
  22. return array(
  23. new Table(self::TABLE_NAME_TEST)
  24. )
  25. );
  26. }
  27. }


Dla
  1. NamedTrait::setName(string $name): void
dostaję błąd:
Cytat
The data provider specified for TableTest::testTable is invalid.
Return value of HaruDb\Table::setName() must be an instance of HaruDb\Named\void
, none returned

Jeśli zmienię zwracany typ na null, to dostaję komunikat z pierwszego postu.

Podałem informację o namespace HaruDb - taki jest namespace po przeróbkach kodu.

Cytat
Time: 40 ms, Memory: 4.00MB

There was 1 warning:

1) Warning
The data provider specified for TableTest::testTable is invalid.
Return value of Project\Table::setName() must be an instance of Project\Named\vo
id, none returned

WARNINGS!
Tests: 1, Assertions: 0, Warnings: 1.
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 4.10.2025 - 18:30