[Bug]: Assigning `$this->foo` in `beforeEach` is flagged as an undefined property, because `TestCall` doesn't allow dynamic properties
I maintainer di solito rispondono entro 2 giorni
Nessuno ha ancora preso questa issue.
Valutazione
- Difficoltà
- 2/5
- Tempo stimato
- 1-3 ore
- Idoneità per principianti
- 62/100
Direzione di ricerca
Inizia da src/PendingCalls/TestCall.php, dove esiste il metodo __get e non sono dichiarati né __set né l'attributo #[\AllowDynamicProperties], e confronta con la classe di test generata in src/Factories/TestCaseFactory.php intorno alla riga 169. Controlla src/Functions.php intorno alla riga 63 per l'annotazione @param-closure-this TestCall. È completato quando una scrittura di proprietà su $this dentro beforeEach supera l'analisi statica e la suite di test esistente continua a passare.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
What Happened
Assigning state to $this in a beforeEach (or in any test/it/afterEach closure) is reported by editors as an undefined property. Intelephense reports P1014, Undefined property '$foo'. Reading the same property doesn't produce a warning.
beforeEach(function () {
$this->foo = new Foo(); // Undefined property '$foo'.
});
it('uses foo', function () {
expect($this->foo)->toBeInstanceOf(Foo::class); // no warning
});
The tests run fine. The warning comes from the declared type of $this:
- Since #1628,
test,it,beforeEachandafterEachdeclare@param-closure-this TestCall(Functions.php#L63). TestCallhas__get(TestCall.php#L718), which is why reads pass. It has no__setand no#[\AllowDynamicProperties], so writes are flagged, and PHP 8.2+ treats them as deprecated dynamic properties.- At runtime
$thisis the generated test case class, which is declared#[\AllowDynamicProperties](TestCaseFactory.php#L169). So the declared type is stricter than the real one, and the documented$this->foo = …pattern inbeforeEachgets flagged.
I expected $this in these closures to accept property writes, the same as it does at runtime.
How to Reproduce
-
Open a project using Pest 4.x in VS Code with Intelephense 1.15 or later (I'm on 1.18.5), with undefined-property diagnostics enabled, which is the default.
-
Add a test file:
<?php beforeEach(function () { $this->value = 1; }); it('reads the value', function () { expect($this->value)->toBe(1); }); -
The assignment in
beforeEachis flaggedUndefined property '$value'. The test passes.
Sample Repository
None. The file above is enough to reproduce it in any Pest 4 project.
Pest Version
4.7.8. The same annotations and TestCall are on the current 4.x branch and in v5.3.7.
PHP Version
8.5.13
Operation System
macOS
Notes
Possible fix: add #[\AllowDynamicProperties] to Pest\PendingCalls\TestCall. That matches the runtime class, and nothing assigns properties to a TestCall instance today, so it shouldn't change any runtime behaviour. It would finish what #1628 started: #1628 fixed method calls and reads on $this, but not writes. #996 already made a similar fix for PHPStan by adding HigherOrderTapProxy and Expectation to universalObjectCratesClasses in extension.neon. TestCall could be added there as well, if you want PHPStan users covered too.
Related reports: Intelephense users have hit this repeatedly: bmewburn/vscode-intelephense#3446, bmewburn/vscode-intelephense#3458 and bmewburn/vscode-intelephense#3502. The Intelephense maintainer's position is that it correctly follows Pest's @param-closure-this type, and the workaround offered is a project-level stub that redeclares beforeEach/it with a custom TestCase. A fix in Pest would remove the need for that in every project. Discussion #1469 asks a related question about $this in beforeEach.
- Lingua principale
- PHP
- Stelle
- 11.8k
- Fork
- 563
- Merge medio
- 2g 21h
- PR unite (30g)
- 39
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di pestphp/pest
-
[Bug]: [mutate] A --mutate run given --filter scores every mutant as testedForse già presa @jfatr l’ha presa 2 giorni fa. Aperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 2 giorni
-
[Bug]: [mutate] Windows: StreamWrapper::stream_lock() always takes LOCK_SH, so LOCK_EX writes fail in a mutant child and the mutant counts as killedForse già presa @joshdaugherty l’ha presa 2 giorni fa. Aperta
Difficoltà 1/5 1-3 ore Idoneità per principianti 88/100
I maintainer di solito rispondono entro 2 giorni
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 2 giorni
-
[Bug]: --mutate hides unreadable files, so mutants behind a failed read can never be killedForse di nuovo libera Una pull request per questa issue è stata chiusa senza essere unita. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 88/100
I maintainer di solito rispondono entro 2 giorni
-
[Bug]: [mutate] Windows: @pest-mutate-ignore comments land on line 1 in files with LF line endingsForse già presa @webdevsamran l’ha presa 64 giorni fa. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
I maintainer di solito rispondono entro 2 giorni
Tutte le issue di pestphp/pest
Issue simili
-
bug needs grooming
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 63/100
phpstan/phpstan-phpunit#361 ·
I maintainer di solito rispondono entro 1 giorno
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
HE-Arc/le-panier-du-coin#49 ·
I maintainer di solito rispondono entro 1 giorno
-
15.0
Difficoltà 2/5 1-3 ore Idoneità per principianti 65/100
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
woocommerce/woocommerce-subscriptions-importer-exporter#298 ·