Mocking files via factory

Hi,

Im currently trying to mock files of a (mocked) page. For my unit tests I currently use a setup with real pages, I would like to replace that with mocks.

Mocking pages works, but I have problems with mocking files for a page. I tried the page factory like so:

        $outbox = File::factory([
            'parent' => kirby()->page(),
            'filename' => 'a-test.json',
            'content' => ['test']
        ]);

which results in an error:

Exception: The property “parent” is required

I just tried around with some other examples I found in the forum:

File::factory([
  'parent'   => kirby()->user(),
  'filename' => 'test',
]);

resulting in the same message.

The docs (https://getkirby.com/docs/reference/objects/cms/file/factory) do not give a lot more information other than that it has to be set in the props. So I thought I did that right.

Obviousely I am not :wink: can somebody tell me what’s wrong here?
Thanks

Maurice

What if you create a new page object and pass that to the file factory?

Thank you, that did the trick!

What I did:

  1. try to create a file via factory using kirby()->page() as model
  2. create a files collection via files factory, handing in the created file
  3. create a page mock and hand in the files collection

What I had to do

  1. Create the mock page
  2. create the file mock via factory with the page as parent

This way it works :slight_smile:

So this is probably my last kirby action of 2022 :wink:

Have a nice evening and a good start into 2023!