objFromFixture(PeopleGroup::class, 'group'); $list = $group->People(); $this->mockSingleton(Person::class) ->expects($this->once()) ->method('canCreate') ->with( $this->anything(), $this->callback(function ($arg) use ($group) { return isset($arg['Parent']) && $arg['Parent']->ID == $group->ID; }) ); $this->mockButtonFragments($list, $group); } public function testButtonPassesNoParentContextToSingletonWhenRelationListIsNotUsed() { $group = $this->objFromFixture(PeopleGroup::class, 'group'); $this->mockSingleton(Person::class) ->expects($this->once()) ->method('canCreate') ->with( $this->anything(), $this->callback(function ($arg) { return !isset($arg['Parent']); }) ); $this->mockButtonFragments(Person::get(), $group); } public function testButtonPassesNoParentContextToSingletonWhenNoParentRecordExists() { $group = $this->objFromFixture(PeopleGroup::class, 'group'); $list = $group->People(); $this->mockSingleton(Person::class) ->expects($this->once()) ->method('canCreate') ->with( $this->anything(), $this->callback(function ($arg) { return !isset($arg['Parent']); }) ); $this->mockButtonFragments($list, null); } protected function mockButtonFragments(SS_List $list, $parent = null) { $form = Form::create( new TestController(), 'test', FieldList::create( $fakeGrid = GridField::create( 'dummy', 'dummy', $list, new GridFieldConfig( $button = new GridFieldAddNewButton() ) ) ), FieldList::create() ); if ($parent) { $form->loadDataFrom($parent); } $button->getHTMLFragments($fakeGrid); } protected function mockSingleton($class) { $mock = $this->getMockBuilder($class) ->setMethods(['canCreate']) ->getMock(); Injector::inst()->registerService($mock, $class); return $mock; } }