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

Unable to make spy of the "Schema" facade #37983

Closed
awjudd opened this issue Jul 12, 2021 · 6 comments
Closed

Unable to make spy of the "Schema" facade #37983

awjudd opened this issue Jul 12, 2021 · 6 comments

Comments

@awjudd
Copy link

awjudd commented Jul 12, 2021

  • Laravel Version: 8.49.2
  • PHP Version: 8.0.7
  • Database Driver & Version: N/A

Description:

When testing and trying set up the spy on the Schema object (i.e. Schema::spy()), you receive an exception.

Steps To Reproduce:

1 - Create a test case
2 - Call the following:

Schema::spy();

3 - When you run the test case, you will receive the following error:

TypeError: Illegal offset type in isset or empty

/Users/judda/code/schema-facade/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:128
/Users/judda/code/schema-facade/vendor/laravel/framework/src/Illuminate/Support/Facades/Facade.php:52
/Users/judda/code/schema-facade/tests/Feature/ExampleTest.php:18

Commit with the issue:
https://github.com/awjudd/laravel-schema-issue/commit/9040182ce26fc4572c6c79f6200d13d12ddc8042

Extra Information

When digging a little deeper, it looks like there are at least 2 spots that have the same issue. In src/Illuminate/Support/Facades/Schema.php the following methods:

  • swap
  • isMock

I believe that it has to do with the following change:
63d87d7

The reason being, at this point in time the method no longer could be trusted to only return a string, so doing the dictionary lookup was ignored in one spot (63d87d7#diff-85c2ee3096a7c4457f3abe3785b2584af93e1672402207cee10195543acbba49R167)

@taylorotwell
Copy link
Member

Why do you need to spy on the Schema facade?

@awjudd
Copy link
Author

awjudd commented Jul 12, 2021

I was trying to write a test for something that was calling the hasTable method on this object. I just tried and verified that it also happens when you use partialMock.

@taylorotwell
Copy link
Member

taylorotwell commented Jul 13, 2021

It's unclear at the moment if this is solvable on our side due to how our the schema stuff is setup.

My best suggestion is to put your Schema operations you want to test behind another class like App\Schema. Within that class, make methods for the schema operations you want to mock, then inject that class into whatever needs to interact with the schema. Basically, put the schema operations behind another class / layer that you can inject and mock the old fashioned way.

@driesvints
Copy link
Member

Going to close this one now. @awjudd there doesn't seems to be an easy way for us to fix this so please try the suggestions by Taylor.

@driesvints
Copy link
Member

driesvints commented Jul 15, 2021

I sent in a possible solution for this here: #38017

Btw @awjudd, while investigating I noticed you have a slight issue in your code. It should be shouldHaveReceived instead of shouldReceive. Former is used in combination with spying while the latter is used for mocking. See https://laravel.com/docs/8.x/mocking#facade-spies

@awjudd
Copy link
Author

awjudd commented Jul 15, 2021

Good catch - about the shouldHaveReceived, it was a copy and paste from my previous example where I was defining them all upfront.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants