FOSSology  4.7.1
Open Source License Compliance by Open Source Software
KotobaAgentPluginTest.php
1 <?php
2 /*
3  SPDX-FileCopyrightText: © 2026 Siemens AG
4 
5  SPDX-License-Identifier: GPL-2.0-only
6 */
7 
10 use Mockery as M;
11 
12 require_once(dirname(dirname(dirname(__DIR__))) . '/lib/php/Test/Reflectory.php');
13 require_once(dirname(dirname(dirname(__DIR__))) . '/lib/php/Plugin/FO_Plugin.php');
14 require_once(dirname(dirname(dirname(__DIR__))) . '/lib/php/common-plugin.php');
15 require_once(__DIR__ . '/../../ui/agent-kotoba.php');
16 
27 class KotobaAgentPluginTest extends \PHPUnit\Framework\TestCase
28 {
30  private $assertCountBefore;
32  private $plugin;
34  private $dbManager;
35 
36  protected function setUp(): void
37  {
38  $this->assertCountBefore = \Hamcrest\MatcherAssert::getCount();
39 
40  global $container, $Plugins;
41  $this->dbManager = M::mock(DbManager::class);
42  $container = M::mock('ContainerBuilder');
43  $container->shouldReceive('get')->with('db.manager')->andReturn($this->dbManager);
44 
45  $Plugins = array();
46  $this->plugin = new KotobaAgentPlugin();
47  }
48 
49  protected function tearDown(): void
50  {
51  $this->addToAssertionCount(\Hamcrest\MatcherAssert::getCount() - $this->assertCountBefore);
52  M::close();
53  }
54 
56  private function registerStubPlugin($pluginName, $agentName)
57  {
58  global $Plugins;
59  $Plugins[$pluginName] = (object) ['AgentName' => $agentName];
60  }
61 
71  {
72  $this->registerStubPlugin('agent_scancode', 'scancode');
73 
74  $this->dbManager->shouldReceive('getSingleRow')
75  ->once()
76  ->with(M::pattern('/jq_endtime IS NULL/'), ['scancode', 42])
77  ->andReturn(['jq_pk' => 7]);
78 
79  $result = Reflectory::invokeObjectsMethodnameWith(
80  $this->plugin, 'isAgentScheduledForUpload', [42, 'agent_scancode']
81  );
82 
83  $this->assertTrue($result);
84  }
85 
93  public function testReturnsFalseWhenAgentHasNoJob(): void
94  {
95  $this->registerStubPlugin('agent_scancode', 'scancode');
96 
97  $this->dbManager->shouldReceive('getSingleRow')->once()->andReturn(null);
98 
99  $result = Reflectory::invokeObjectsMethodnameWith(
100  $this->plugin, 'isAgentScheduledForUpload', [42, 'agent_scancode']
101  );
102 
103  $this->assertFalse($result);
104  }
105 
113  {
114  $this->dbManager->shouldNotReceive('getSingleRow');
115 
116  $result = Reflectory::invokeObjectsMethodnameWith(
117  $this->plugin, 'isAgentScheduledForUpload', [42, 'agent_unknown']
118  );
119 
120  $this->assertFalse($result);
121  }
122 
128  public function testChecksEachAgentIndependently(): void
129  {
130  $this->registerStubPlugin('agent_nomos', 'nomos');
131  $this->registerStubPlugin('agent_scancode', 'scancode');
132 
133  $this->dbManager->shouldReceive('getSingleRow')
134  ->once()
135  ->with(M::any(), ['nomos', 99])
136  ->andReturn(['jq_pk' => 1]);
137  $this->dbManager->shouldReceive('getSingleRow')
138  ->once()
139  ->with(M::any(), ['scancode', 99])
140  ->andReturn(null);
141 
142  $nomosResult = Reflectory::invokeObjectsMethodnameWith(
143  $this->plugin, 'isAgentScheduledForUpload', [99, 'agent_nomos']
144  );
145  $scancodeResult = Reflectory::invokeObjectsMethodnameWith(
146  $this->plugin, 'isAgentScheduledForUpload', [99, 'agent_scancode']
147  );
148 
149  $this->assertTrue($nomosResult);
150  $this->assertFalse($scancodeResult);
151  }
152 }
Test for class KotobaAgentPlugin::isAgentScheduledForUpload()
registerStubPlugin($pluginName, $agentName)
fo_dbManager * dbManager
fo_dbManager object
Definition: process.c:16