paul-thebaud / phpunitgen-core

PhpUnitGen is a PHP tool to generate your unit tests' skeletons on your projects.
https://phpunitgen.io
MIT License
29 stars 6 forks source link

feat: Implement new Magento specific tests generators #21

Closed bytes-commerce closed 1 year ago

bytes-commerce commented 2 years ago

Subject of the issue

When phpunitgen.php is configured to use PhpUnit over Mockery and the Laravel flag is set to null (apparently, it doesn't matter), PhpUnitGen creates an faulty line in the setUp() method.

Your environment

Steps to reproduce

In phpunitgen.php

...
    'implementations' => array_merge(CommandTestGenerator::implementations(), [
        MockGenerator::class => PhpUnitMockGenerator::class,
    ]),
...
    'options'             => [
        /*
         |----------------------------------------------------------------------
         | Context.
         |
         | Tells the DelegateTestGenerator (default one) that we are in a
         | specific project context. If defined to "null", it will used basic
         | generators. If set to "laravel", it will use the Laravel tests
         | generators.
         |----------------------------------------------------------------------
         */
        'context' => null,
    ],

Then, run phpunitgen to create desired tests:

php ../../../../phpunitgen/vendor/bin/phpunitgen -C ../../../../phpunitgen/phpunitgen.php Bla/ Bla/Test/Unit/

Expected behaviour

The setUp() method contains all mocks and creations as expected.

Actual behaviour

PhpUnitGen always adds this line of code that will break in my non-laravel environment. Maybe its working in Laravel, tho?

    protected function setUp(): void
    {
        parent::setUp();

        $this->context = $this->createMock(Context::class);
        $this->registry = $this->createMock(Registry::class);
        $this->agecheckDataFactory = $this->createMock(AgeCheckInterfaceFactory::class);
        $this->dataObjectHelper = $this->createMock(DataObjectHelper::class);
        $this->resource = $this->createMock(AgeCheckAlias::class);
        $this->resourceCollection = $this->createMock(Collection::class);
        $this->data = [];
        $this->ageCheck = new AgeCheck($this->context, $this->registry, $this->agecheckDataFactory, $this->dataObjectHelper, $this->resource, $this->resourceCollection, $this->data);
        $this->app->instance(AgeCheck::class, $this->ageCheck);
    }

The problematic line is:

        $this->app->instance(AgeCheck::class, $this->ageCheck);

as app is not an defined property.

paul-thebaud commented 2 years ago

Hello, As stated in the webapp, the command test generator is a dedicated Laravel test generator. For non Laravel projects, you should always use the BasicTestGenerator (as there is no other framework specific tests generators for now).

bytes-commerce commented 2 years ago

How could I possibly contribute by implementing a solution that will work for i.e. Magento2? Right now I rewrite Php Classes via Composer to fit my need (its only a couple of files I had to change to get what I want).

Possibly, the now laravel only functionality could be switched over to a more broad solution that probably will work well with other Frameworks as well.

paul-thebaud commented 2 years ago

Hello, Thanks a lot, that's a great idea! I don't add other framework because I don't use them, so I don't think I'd produced great tests skeleton. I think the best way to start is to read the contribution guide and read the existing Laravel tests generators (and other associated implementation). After that, you can fork, create a branch and add new Magento generators on it. Once done (or before if you need help or explanation), you can create a PR! In addition to tests generators, you can also write additional Magento dedicated feature if you want! Thanks a lot, I think it's a great idea, feel free to ask if you need any help.

paul-thebaud commented 2 years ago

Link to useful docs or code:

Note: each class is unit tested and there is one or more feature test per feature (eg. a test generator). We follow PSR-2 coding standards, and there is a sonar cloud for code smells.

bytes-commerce commented 2 years ago

Lets see, albeit I contribute usually to private Repositories, forking and pushing to existing Repos is new to me. I will read trough it and maybe start something off of it.. From what I learned is that I could perfectly re-use existing code (or better say the code structure) and simply adapt it onto Magento2 needs. I will let you know eventually. :)) Have a nice evening!

paul-thebaud commented 2 years ago

Thanks. I'm looking forward to see what you create! Leave a comment if you need anything else!

NB: as this feature is not already available and need your investigation, I leave this issue open until this feature is available inside a release 😉

paul-thebaud commented 2 years ago

Hi @nopenopenope, I'm done with your two previous issue (see PRs #24 and #25). Those contains breaking change (new options, interfaces changes) and I'm to release a V3. Do you think your Magento tests generators will introduce other breaking changes? If yes, we'll have to wait for you to finish those before releasing V3. If no, I'll release the V3 soon.

bytes-commerce commented 2 years ago

Hi @paul-thebaud thanks for reaching out. I have some challenges going further with my adaptions and will likely take more time to contribute my changes as I like them, so you can already release V3 soon - which would be rather ideal for me, too, since I then could simply move on from there. Thanks a lot of taking care of my suggestions!

paul-thebaud commented 2 years ago

It's ok then. I will try to release it this week, but I have some work to prepare on console package and webapp before. I think I will set the typed properties as a default behavior too, since most people are using 7.4+. Thanks you for those suggestions! Still here if you need something 🙂

bytes-commerce commented 2 years ago

Hi,

as for your question if there will be breaking changes: likely no. Right now you have the configurable setting in option array in phpunitgen.php set to Laravel, and I will likely implement a bridge that also accepts magento2 and does its stuff based on that configuration value (and re-uses other components when possible). So the change itself would rather be minor. The more I work with the code, the less I think it will be an actual "Magento2 Generator" but rather a full grown strict test generator for any Framework without that does nothing much special with the tests, i.e. Sylius or similar.

paul-thebaud commented 2 years ago

Hi @nopenopenope, Yes, the options key of config is totally made for this! So if you need specific magento config, you can use it like the Laravel user class option. You can also take a look at DelegateTestGenerator, which choose the most adapted test generator depending on options.context and class namespace. Have fun!

paul-thebaud commented 2 years ago

Hi @nopenopenope, PhpUnitGen core V3 and console V4 are now released. The webapp is also received as V3 with new configuration on frontend and documentation update. :tada:

paul-thebaud commented 1 year ago

Hello @nopenopenope, I'm closing this issue due to no activity. Feel free to reopen if you are still working on it.