Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> [PHPUnit] Nadpisanie metody statycznej
kapslokk
post
Post #1





Grupa: Zarejestrowani
Postów: 965
Pomógł: 285
Dołączył: 19.06.2015
Skąd: Warszawa

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


Cześć,
próbuje napisać test, dla klasy, która korzysta z innej. Przykład:

  1. class A{
  2. public static function get(){
  3. return 1;
  4. }
  5. }
  6.  
  7. class B{
  8. private $a;
  9. public function __construct($a){
  10. $this->a = $a;
  11. }
  12. public function doSomething(){
  13. return $this->a->get();
  14. }
  15. }

No i jak próbuje zrobić mock metody get i jest tam "static", to po prostu pomija ten test. Jeżeli usunę "static" - wszystko działa ok.

Nie pytajcie po co ten static tam w ogole jest - nie moja wina, grzebe w starym kodzie, nie mogę go usunąć.
Używam PHPUnit 5.4.2


Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Crozin
post
Post #2





Grupa: Zarejestrowani
Postów: 6 476
Pomógł: 1306
Dołączył: 6.08.2006
Skąd: Kraków

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


Możesz spróbować wyrzucić odwołania do nieszczęsnej klasy statycznej do pośredniczącego obiektu.
  1. class Config {
  2. public static get(...);
  3. public static set(...);
  4. }
  5.  
  6. interface ConfigProviderInterface {
  7. public get();
  8. public set();
  9. }
  10.  
  11. // implementacja wykorzystywana w normalnym kodzie
  12. class StaticConfigProvider implements ConfigProviderInterface {
  13. public get() {
  14. return Config::get(...);
  15. }
  16.  
  17. public set() {
  18. Config::set(...);
  19. }
  20. }
  21.  
  22. // implementacja wykorzystywana w testach
  23. class ArrayConfigProvider implements ConfigProviderInterface {
  24. private $config;
  25.  
  26. public get() {
  27. return $this->config[...];
  28. }
  29.  
  30. public set() {
  31. $this->config[...] = ...;
  32. }
  33. }
  34.  
  35. class MyClass {
  36. private $config;
  37.  
  38. public __construct(ConfigProviderInterface $config) {
  39. $this->config = $config;
  40. }
  41.  
  42. public doSth() {
  43. return 'abc' . $this->config->get(..);
  44. }
  45. }
  46.  
  47. // ---- TESY:
  48.  
  49. $config = new ArrayConfigProvider([....]);
  50. $class = new MyClass($class);
  51.  
  52. $this->assertEquals('abc123', $class->doSth());
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: 5.10.2025 - 20:56