Hacktoberfest 2026: le issue che i maintainer hanno segnato per ottobre, aperte e adatte ai principianti. Sfoglia le issue Hacktoberfest

Fully automate the module without requiring changes to tests

Aperta
#23 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
5/5
Tempo stimato
Più di una settimana
Idoneità per principianti
25/100
Tipo di issue
Funzionalità
Chiarezza
Da chiarire
Stato di attività
Ferma
Stack tecnologico
php
Ambito
testing

Direzione di ricerca

Inizia dal punto di ingresso _before(TestInterface $test) del modulo e dalla chiamata Codeception Di::injectDependencies mostrata nell’issue. Traccia il modo in cui vengono eseguiti Cest e gli altri tipi di test, quindi definisci l’hook del ciclo di vita necessario per eseguire la pulizia di Mockery senza modificare i test. Il lavoro è completato quando il modulo funziona con tutti i tipi di test supportati e sono presenti test per la pulizia e la gestione degli errori.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

Currently using this module requires changes in each test.
I have found an ugly way to alleviate that problem.

The code below was tested for Cest type tests only. It should not break for any other test type but might simply not work.


use Codeception\Lib\Di;
use Codeception\Module;
use Codeception\TestInterface;

class Mockery extends Module
{


    public function _before(TestInterface $test)
    {
        $meta = $test->getMetadata();
        $rc = new \ReflectionClass($meta);
        $prop = $rc->getProperty('services');
        $prop->setAccessible(true);
        $services = $prop->getValue($meta);
        /** @var Di $di */
        $di = $services['di'];

        $services['di'] = new class($di, $test) extends Di {
            public function __construct(Di $fallback, private readonly TestInterface $test)
            {
                parent::__construct($fallback);

            }


            public function injectDependencies(object $object, string $injectMethodName = self::DEFAULT_INJECT_METHOD_NAME, array $defaults = []): void
            {
                if ($object === $this->test) {
                    parent::injectDependencies(...func_get_args());
                    return;
                }
                // We check if the object is our test, if so we close mockery after.

                codecept_debug(['CALLING',
                    get_class($object),
                    $injectMethodName
                ]);
                try {
                    parent::injectDependencies(...func_get_args());
                    \Mockery::close();
                } catch (\Throwable $t) {
                    \Mockery::resetContainer();
                    throw $t;
                }
                \Mockery::close();
            }
        };
        $prop->setValue($meta, $services);
    }
}

Essentially we hijack the Di container and replace it with a custom implementation. Based on the fact that for Cest files the actual test is ran like this:

$this->getMetadata()->getService('di')->injectDependencies($this->testInstance, $methodName, $context);

We abuse this to insert a mockery closure after calling the test method.

While I'm happy to make a PR for this, I'm not sure it should be in an "official" repository. I probably don't have time to implement a proper fix where we add an event to allow modules to fail a test before it is finished.

Lingua principale
PHP
Stelle
34
Fork
12
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Guida per i contributori

Nessuna guida per i contributori indicizzata per questo repository

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di Codeception/MockeryModule

Tutte le issue di Codeception/MockeryModule

Issue simili

Altre issue su PHP

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.