laracoft's avatar

Location of assets/resources for Unit Tests

Just curious on where I should be storing my assets/resources that are required by Unit Tests?

Context is: I'm trying to parse some text/binary files, some of them can be placed inline, but makes the code harder to read. Others are in binary form, recoding them to base64 etc doesn't seem to make sense to me.

0 likes
6 replies
tykus's avatar

You can create a directory inside the tests directory to contain your Test Fixtures. I don't understand exactly what you're wanting to store in there; but that is the general solution.

laracoft's avatar

@tykus I'm storing binary files for a parser, I would like it to continue to be able to parse older versions as it evolves. Is there a shortcut like storage_path(...) to access the tests/Fixtures folder or something?

martinbean's avatar

Is there a shortcut like storage_path(...) to access the tests/Fixtures folder or something?

@laracoft If you’re calling framework functions and classes, then your test will no longer be a unit test.

If you’re unit-testing a class or method, then you should be able to invoke that method and assert the result without any external dependencies. If you need the framework or the database, then you have an integration test and not a unit test.

tykus's avatar
tykus
Best Answer
Level 104

@laracoft no, but nothing to prevent you creating a helper function:

function fixtures_path($path = '')
{
        return basepath('tests/fixtures/' . $path);
}
1 like
laracoft's avatar

@martinbean ok interesting what you said. Ignoring the type of test I need for now, how should I structure tests for a binary reader? e.g. GIF/PNG

My plan was to store some known binary files and test that the reader can parse and decode correctly and my goal is to ensure it doesn't break as time goes on and there are newer versions.

baumgars's avatar

@laracoft additionally i want to store the files in git. At least the storage folder is not suitable for this even if linked into public folder (root level, what the browser sees). Manipulating .gitignore files to unignore my test data i want to seed into my db for testing purposes...my IDE PHPStorm just did not realize there is more to add. So i will try with the tests folder or the actual resources folder (in the root of the project). I know this is not exactly unit testing but for special big test data i dont want to put into unit test code and messing around with escaping special characters...i mean i can put the data into database as is, read it from database in unit test and assert a specific result.

Please or to participate in this conversation.