Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Assertion Tests & Implementation #51

Closed
wants to merge 2 commits into from

Conversation

Juhlinus
Copy link

@Juhlinus Juhlinus commented Aug 12, 2019

Assertions in the Inertia Laravel Adapter

I'm pretty sure that most of us that are testing are making use of the neat macros that was made for PingCRM, so I thought I'd make a PR merging those assertions into the adapter.

Motivations

I don't really want my TestCase.php to be bloated with a ton of macros, so I decided to make these assertions and override the default Laravel TestResponse with these. (You will of course be able to use all assertions that comes with Laravel as well.)

Usage

Simply extend the Inertia TestCase instead of the default Laravel one.

Before

<?php

namespace Tests;

use Illuminate\Support\Arr;
use PHPUnit\Framework\Assert;
use Illuminate\Foundation\Testing\TestResponse;
use Illuminate\Foundation\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;

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

        TestResponse::macro('props', function ($key = null) {
            $props = json_decode(json_encode($this->original->getData()['page']['props']), JSON_OBJECT_AS_ARRAY);
            if ($key) {
                return Arr::get($props, $key);
            }
            return $props;
        });

        TestResponse::macro('assertHasProp', function ($key) {
            Assert::assertTrue(Arr::has($this->props(), $key));
            return $this;
        });

        TestResponse::macro('assertPropValue', function ($key, $value) {
            $this->assertHasProp($key);
            if (is_callable($value)) {
                $value($this->props($key));
            } else {
                Assert::assertEquals($this->props($key), $value);
            }
            return $this;
        });

        TestResponse::macro('assertPropCount', function ($key, $count) {
            $this->assertHasProp($key);
            Assert::assertCount($count, $this->props($key));
            return $this;
        });
    }
}

After

<?php

namespace Tests;

use Tests\CreatesApplication;
use Inertia\Testing\TestCase as BaseTestCase;

abstract class TestCase extends BaseTestCase
{
    use CreatesApplication;
}

See? Much cleaner than before!

Docs

If this get merged I'll sit down and write some documentation for this as well. 😄

@Juhlinus Juhlinus changed the title Tests & Implementation Assertion Tests & Implementation Aug 12, 2019
@reinink
Copy link
Member

reinink commented Aug 13, 2019

Thanks for this @Juhlinus, but I actually have some updated testing helpers. I'm going to leave this PR open and will post those helpers when I have a minute.

@Juhlinus
Copy link
Author

Thanks for this @Juhlinus, but I actually have some updated testing helpers. I'm going to leave this PR open and will post those helpers when I have a minute.

Great! Looking forward to it!

@herpaderpaldent
Copy link

i am curious about these new helpers too @reinink

@Juhlinus
Copy link
Author

Juhlinus commented Aug 21, 2019

i am curious about these new helpers too @reinink

@herpaderpaldent Jonathan has a day-job and can't push out new changes everyday, unfortunately.

He will post an update in this thread when the new helpers are available. :)

herpaderpaldent pushed a commit to seatplus/web that referenced this pull request Aug 30, 2019
@m1guelpf
Copy link

m1guelpf commented Sep 5, 2019

This would probably make better sense as a trait instead of a class?

@robsontenorio
Copy link

robsontenorio commented Oct 1, 2019

it would be nice!

I do prefer strategy proposed by @Juhlinus insted of traits, because we get autocompletion advantage. It will require one single change on your app codebase base (TestCase.php)

@nasyrov
Copy link

nasyrov commented Dec 21, 2019

IMHO better to create a proper mixin class and boot in the service provider or when needed

TestResponse::mixin(new TestMixins);

@robsontenorio
Copy link

Macros would be a option. But auto completion would be a plus!

@johanvanhelden
Copy link

But what if the user already has his own BaseTestCase that he uses with certain customizations.
This way it is going to be extend-ception.

Maybe a trait or macro would be a better option.

@mojtabaahn
Copy link

Hi there. any updates on this ?

@Juhlinus
Copy link
Author

@mojtabaahn Not yet!

If you want to use it, then just add it to your project. 😄

@jartaud
Copy link

jartaud commented Aug 21, 2020

While I wait for the PR to be merged, I'm doing something like that:

TestResponse::macro('assertComponentIs', function ($component) {
     Assert::assertEquals(
         $component,
         $this->original->getData()['page']['component']
     );
     return $this;
});

Thanks for the idea @Juhlinus

@claudiodekker
Copy link
Member

claudiodekker commented Mar 2, 2021

Thanks @Juhlinus! We just merged #220, meaning this PR is no longer necessary, so I'll be closing this one now 👍

@Juhlinus
Copy link
Author

Juhlinus commented Mar 3, 2021

Thanks @Juhlinus! We just merged #220, meaning this PR is no longer necessary, so I'll be closing this one now 👍

Nice job buddy!

@Juhlinus Juhlinus deleted the assertions branch March 3, 2021 10:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

10 participants