# |
May 14th 2021, 15:17 |
greenmanspirit |
@kevin.pfeifer I have $this->belongsToMany('B'); in C and $this->belongsToMany('C'); in B |
# |
May 14th 2021, 15:16 |
matthttam |
@ndm What if.... What if I stubbed the LocationsEndpoint::class. Then mocked the query that is returned? |
# |
May 14th 2021, 15:13 |
kevin.pfeifer |
@greenmanspirit and you are sure you configured the HABTM Connection in your Table correctly on the B Model side? |
# |
May 14th 2021, 15:12 |
greenmanspirit |
@kevin.pfeifer Yes, that works just fine. |
# |
May 14th 2021, 15:10 |
slackebot |
building custom locators (if you can't use DI yet), then you could wrap things in custom services that have a cleaner, easier to mock API. As far as the error is concerned, check your error logs for more details (`/logs/`), 500 indicates that an exception was thrown. |
# |
May 14th 2021, 15:10 |
slackebot |
->getMockBuilder(\Muffin\Webservice\Datasource\Query::class) ->onlyMethods(['toArray']) ->getMock(); $queryMock ->expects($this->once()) ->method('toArray') ->willReturn([ 'some test data' ]); $locationsMock ->expects($this->once()) ->method('find') ->willReturn($queryMock);``` With that amount of mocking required, especially mocking 3rd party code, you may want to consider learning about |
# |
May 14th 2021, 15:10 |
ndm |
@matthttam Yes, expectations define what you expect with your mocks to happen, for example `find` being invoked and returning an object that you've defined, one that provides a `toArray()` method that returns your test data. That's PhpUnit basics, so I'd highly recommended that you make yourself familiar with it first: https://phpunit.readthedocs.io/en/9.5/test-doubles.html Quick and dirty example: ```$queryMock = $this |
# |
May 14th 2021, 15:08 |
kevin.pfeifer |
but when you delete an entry from model C does it remove the connection data between C and B for that? |
# |
May 14th 2021, 14:59 |
greenmanspirit |
delete when B is deleted. Any help will be appreciated. |
# |
May 14th 2021, 14:59 |
greenmanspirit |
hello, I am using CakePHP 4. I have three models (A, B, C). A hasMany B with saveStrategy = replace, B HABTM C. I am managing B's records using this method https://codethepixel.com/cakephp/cakephp-dynamic-input-and-saveassociated adjusted for CakePHP 4. The B records add and remove just fine but I cannot get entries in the join table for B and C to |
# |
May 14th 2021, 14:50 |
slackebot |
$this->assertResponseOk(); }``` |
# |
May 14th 2021, 14:50 |
slackebot |
$locationsMock = $this ->getMockBuilder(\Freshservice\Model\Endpoint\LocationsEndpoint::class) ->onlyMethods(['find', 'toArray']) ->getMock(); $locator = \Cake\Datasource\FactoryLocator::get('Endpoint'); $locator->set('Freshservice.Locations', $locationsMock); }); $this->login(); $this->get('assets/test'); |
# |
May 14th 2021, 14:50 |
matthttam |
``` public function testTest(): void { //$this->modelFactory('Endpoint', ['Muffin\Webservice\Model\EndpointRegistry', 'factory']); //$locations = $this->getMockForAbstractClass( // 'Freshservice\Webservice\LocationsWebservice', // array(), 'Locations', false // ); \Cake\Event\EventManager::instance()->on('Controller.initialize', function () { |
# |
May 14th 2021, 14:50 |
matthttam |
```Time: 2 seconds, Memory: 16.00 MB There was 1 failure: 1) App\Test\TestCase\Controller\AssetsControllerTest::testTest Failed asserting that 500 is between 200 and 204.``` |
# |
May 14th 2021, 14:50 |
slackebot |
further than I've gotten in my days of playing with this problem |
# |
May 14th 2021, 14:50 |
matthttam |
@ndm... so it is failing less hard now I think. I'm trying to understand what adding expectations would look like. Or what that does exactly... In my head, I expect: ```$locations = $this->Locations->find('all')->toArray();``` to return an array of data... Does this mean I need to add an expect to it where the method "find" returns an a resource or something? I'm just so confused.... TY for your help though so far this is |
# |
May 14th 2021, 14:45 |
matthttam |
Yea, no sandbox I don't think... that would be nice |
# |
May 14th 2021, 14:44 |
ndm |
Also check whether your api has a sandbox |
# |
May 14th 2021, 14:43 |
matthttam |
The API is quite extensive and subject to change. it would be way to difficult to simulate the whole thing honestly. I just want to mock the endpoints/webservices I'm using so that the controller can be tested. https://api.freshservice.com/v2/ |
# |
May 14th 2021, 14:42 |
ndm |
Alternatively consider using some kind of dummy server for your test environment that simulates the API. |
# |
May 14th 2021, 14:42 |
ndm |
Yes. |
# |
May 14th 2021, 14:40 |
matthttam |
where do I put this? in the test itself? |
# |
May 14th 2021, 14:35 |
slackebot |
->onlyMethods(['the', 'methods', 'to', 'mock']) ->getMock(); // ... add expectations to mock $locator = \Cake\Datasource\FactoryLocator::get('Endpoint'); $locator->set('Freshservice.Locations', $locationsMock); });``` |
# |
May 14th 2021, 14:35 |
ndm |
I'm not familiar with that plugin, but the endpoint locator certainly seems to hold endpoint instances, so if you want to mock what `loadModel()` returns, then you'd probably want to mock `Freshservice\Model\Endpoint\LocationsEndpoint`. ```\Cake\Event\EventManager::instance()->on('Controller.initialize', function () { $locationsMock = $this ->getMockBuilder(\Freshservice\Model\Endpoint\LocationsEndpoint::class) |
# |
May 14th 2021, 14:29 |
matthttam |
But test doesn't know what to do with it |
# |
May 14th 2021, 14:28 |
matthttam |
The app itself works of course. It does the API call for realz and is currently just dumping a debug of $locations... |
# |
May 14th 2021, 14:28 |
matthttam |
Well... branch 3.0.0-RC3 though https://github.com/UseMuffin/Webservice/blob/3.0.0-RC3/src/Model/Endpoint.php |
# |
May 14th 2021, 14:27 |
matthttam |
I don't know enough of cakephp to do what you just asked honestly. I'm still trying to learn this beast of a framework. An endpoint extends Muffin\Webservice\Model\Endpoint https://github.com/UseMuffin/Webservice/blob/master/src/Model/Endpoint.php |
# |
May 14th 2021, 14:25 |
ndm |
Also a webservice isn't an endpoint, or is it? |
# |
May 14th 2021, 14:24 |
ndm |
Try in an event that happens after the plugin that provides the locator has registered it, say for example `Controller.initialize` (assuming you don't need to use the model earlier). |
# |
May 14th 2021, 14:19 |
slackebot |
); $locator = \Cake\Datasource\FactoryLocator::get('Endpoint'); $locator->set('Freshservice.Locations', $locations);``` |
# |
May 14th 2021, 14:19 |
matthttam |
@ndm ```1) App\Test\TestCase\Controller\AssetsControllerTest::testTest InvalidArgumentException: Unknown repository type "Endpoint". Make sure you register a type before trying to use it.``` I mocked the webservice like so too... not sure if this is right honestly... ```$locations = $this->getMockForAbstractClass( 'Freshservice\Webservice\LocationsWebservice', array(), 'Locations', false |
# |
May 14th 2021, 14:16 |
matthttam |
I'll try this |
# |
May 14th 2021, 14:16 |
matthttam |
@ndm I am unfortunately currently on 4.0.7. I plan to upgrade soon though. |
# |
May 14th 2021, 14:15 |
ndm |
@matthttam Two suggestions, if you're on CakePHP 4.2+, use DI, container dependencies can be easily mocked (https://book.cakephp.org/4/en/development/dependency-injection.html#mocking-services-in-tests). Alternatively mock the model in the `Endpoint` locator, like: ```$locator = \Cake\Datasource\FactoryLocator::get('Endpoint'); $locator->set('Freshservice.Locations', $yourLocationsMockInstance);``` |
# |
May 14th 2021, 14:15 |
matthttam |
You install the plugin and then specify the host and scheme for the connection in your app_local.php file. Then, you set the API key dynamically based on the user who signs in (or at least that is how my app I am building does it). ```'freshservice' => [ 'host' => 'your_domain.freshservice.com/api/v2/', 'scheme' => 'https', ],``` |
# |
May 14th 2021, 14:13 |
jpramirez |
I see no config folder in your project. Where are the Datasources defined? |
# |
May 14th 2021, 14:08 |
matthttam |
Maybe I need to build a test_app folder like UseMuffin/Webservice does in my plugin? https://github.com/UseMuffin/Webservice/blob/master/tests/test_app/Webservice/StaticWebservice.php |
# |
May 14th 2021, 14:07 |
matthttam |
The thing is... I have a test database and tests that use my database work fine. The Freshservice plugin utilizes https://github.com/UseMuffin/Webservice This is the plugin I am using (and the one I built actually....) https://packagist.org/packages/matt_henry_ops/freshservice |
# |
May 14th 2021, 14:05 |
jpramirez |
@matthttam reading the error you received, I would check that: https://book.cakephp.org/4/en/development/testing.html#test-database-setup Does this helps? |
# |
May 14th 2021, 13:48 |
slackebot |
an actual model... its a model of type "Endpoint".... and I tried mocking the class but even though I could do that it didn't make the test work lol. I'm just at a loss. |