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

Integration testing #5

Closed
KKSzymanowski opened this issue Jun 15, 2016 · 10 comments
Closed

Integration testing #5

KKSzymanowski opened this issue Jun 15, 2016 · 10 comments

Comments

@KKSzymanowski
Copy link
Collaborator

I have an idea for doing some integration tests.

Create a Laravel app, using Laratrust and write tests there.

What I mean is:

  1. Setup an sqlite database with all the needed scaffolding
  2. Fill it with several users, with different roles and permissions
  3. Write some routes and views
  4. Write simple tests for all of this like
Auth::loginUsingId(1);
$this->visit('/permission/1')
     ->see('You are authorized to see this');

Auth::loginUsingId(2);
$this->visit('/permission/1')
     ->see('You are not authorized to see this');

// or

$this->visit('/permission/1')
     ->assertRedirectedTo('/', ['message' => 'Unauthorized access']);

and so on.

This app will pull in santigarcor/laratrust: "master" so every time there is a change in Laratrust you would click "Restart Build" in Travis for this app to see if it all still works.

What do you think about this?
Do you have better ideas?

I also think it would be great for regression tests eg. user says "When I do this and that everything crashes" we would be able to reproduce this error, write tests to fail it, fix the error and see tests turn green(just like Jeffrey explains here)

@santigarcor
Copy link
Owner

So would it be necessary to create another package? or the whole app would be in the tests folder? please explain me because i didn't understand the travis part.

@KKSzymanowski
Copy link
Collaborator Author

Yep, another package would be an overkill. If we do the the app in tests folder, the travis part doesn't matter anymore. I can fiddle with that if you want and see if it's possible.

@Mathius17
Copy link
Contributor

I'd love to see some integration tests, but what worries me is that if we put an app inside the tests folder, when you get the package via composer, it would also retrieve the app. That surely is an overkill.

And if I'm not wrong, the integration tests are responsibility of the app maintainer. A package should only take care of itself, hence the unit tests. This is just testing theory, so if we find a workaround for doing integration tests without creating an app inside a folder, I'm in.

@KKSzymanowski
Copy link
Collaborator Author

KKSzymanowski commented Jun 15, 2016

A HA! Here comes gitattributes to the rescue.
You just add

/tests export-ignore

to the .gitattributes file and it's excluded from archive(composer download, download as zip etc.)

Update: here's an example from Laravel https://github.com/laravel/framework/blob/5.2/.gitattributes

@santigarcor
Copy link
Owner

I'm with @Mathius17

@Mathius17
Copy link
Contributor

Mathius17 commented Jun 17, 2016

Nice suggestion @KKSzymanowski! The problem for me is that it's supposed to be automatic tests, it seems much of a hassle to execute the integration tests from inside the app (inside tests folder) manually. Unless there's a way to do it wth Travis CI, I don't see it possible.

I'll close this issue but feel free to comment, and if you find a way to do it let us know!

@KKSzymanowski
Copy link
Collaborator Author

I don't think it's that much complicated. Take a look at my fork:
https://github.com/KKSzymanowski/laratrust
What I want you to notice in particular is:
In phpunit.xml excluded integration because we need to run in separately

    <testsuites>
        <testsuite name="Package Test Suite">
            <directory suffix=".php">tests</directory>
            <exclude>tests/Integration</exclude>
        </testsuite>
    </testsuites>

In tests/Integration/composer.json everything like in ordinary Laravel installation apart from autoload:

    "autoload": {
        "classmap": [
            "../../src/commands",
            "database"
        ],
        "psr-4": {
            "Santigarcor\\Laratrust\\": "../../src/Laratrust/",
            "App\\": "app/"
        }
    },

I do not require Laratrust as a depencency. Instead I autoload it from above along with the commands.
Now the .travis.yml:

script:
  - php vendor/bin/phpcs --standard=PSR2 src
  - phpunit --verbose
  - cd tests/Integration
  - composer update
  - phpunit --verbose

I haven't figured out how to do this better, but it works.
I fetch all dependencies for Laratrust(as you normally would), run classic tests. Then I go into tests/Integration, fetch all the dependencies for Laravel(which takes a while) and run the tests there.

I've written some sample tests, I know they require refactoring but they're just as a just proof of concept.

Here is the Travis build: https://travis-ci.org/KKSzymanowski/laratrust/builds

Also, important part, if you take a look at my latest commits, I removed some files from .gitignore, because we need them for Laravel to run correctly.

By the way, don't worry. I will squash those stupid commits into one meaningful.

@KKSzymanowski
Copy link
Collaborator Author

KKSzymanowski commented Jun 17, 2016

Added caching to Travis. It decreased build time by nearly a half.
KKSzymanowski@015fb2b

@KKSzymanowski
Copy link
Collaborator Author

What do you think about this solution? Do you know a better way to handle this?

My only concern is different Laravel versions. How to test them all? Currently a lot of people uses 5.1 because it's a LTS release, rest of them uses 5.2 but soon some part of them will upgrade to 5.3. Some of the Laravel plugins keeps different branches for different releases of Laravel.
Do you know if there are changes in laravel that cause errors when using Laratrust/Entrust?

@santigarcor
Copy link
Owner

Well i see everything fine, the thing is i don't know if this is the best way to do it, and the thing with all the laravel versions

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

No branches or pull requests

3 participants