Completed
Push — master ( 5fac04...2eb7c3 )
by Henry
06:50
created

tests/unit/Model/ModelAbstractTest.php (2 issues)

Labels
Severity

Upgrade to new PHP Analysis Engine

These results are based on our legacy PHP analysis, consider migrating to our new PHP analysis engine instead. Learn more

1
<?php
2
namespace Redaxscript\Tests\Model;
3
4
use Redaxscript\Db;
5
use Redaxscript\Model;
6
use Redaxscript\Tests\TestCaseAbstract;
7
8
/**
9
 * ModelAbstractTest
10
 *
11
 * @since 4.0.0
12
 *
13
 * @package Redaxscript
14
 * @category Tests
15
 * @author Henry Ruhs
16
 *
17
 * @covers Redaxscript\Model\ModelAbstract
18
 */
19
20
class ModelAbstractTest extends TestCaseAbstract
21
{
22
	/**
23
	 * setUp
24
	 *
25
	 * @since 4.0.0
26
	 */
27
28
	public function setUp() : void
29
	{
30
		parent::setUp();
31
		$installer = $this->installerFactory();
32
		$installer->init();
33
		$installer->rawCreate();
34
		 Db::forTablePrefix('articles')
35
			->create()
36
			->set(
37
			[
38
				'title' => 'Article One',
39
				'alias' => 'article-one',
40
				'language' => 'en'
41
			])
42
			->save();
43
		Db::forTablePrefix('articles')
44
			->create()
45
			->set(
46
			[
47
				'title' => 'Article Two',
48
				'alias' => 'article-two',
49
				'language' => 'en'
50
			])
51
			->save();
52
	}
53
54
	/**
55
	 * tearDown
56
	 *
57
	 * @since 4.0.0
58
	 */
59
60
	public function tearDown() : void
61
	{
62
		$this->dropDatabase();
63
	}
64
65
	/**
66
	 * testGetById
67
	 *
68
	 * @since 4.0.0
69
	 *
70
	 * @param int $id
71
	 * @param string $expect
72
	 *
73
	 * @dataProvider providerAutoloader
74
	 */
75
76
	public function testGetById(int $id = null, string $expect = null) : void
77
	{
78
		/* setup */
79
80
		$articleModel = new Model\Article();
81
82
		/* actual */
83
84
		$actual = $articleModel->getById($id)->alias;
85
86
		/* compare */
87
88
		$this->assertEquals($expect, $actual);
89
	}
90
91
	/**
92
	 * testGetById
93
	 *
94
	 * @since 4.0.0
95
	 *
96
	 * @param array $expectArray
97
	 *
98
	 * @dataProvider providerAutoloader
99
	 */
100
101
	public function testGetAll(array $expectArray = []) : void
102
	{
103
		/* setup */
104
105
		$articleModel = new Model\Article();
106
107
		/* actual */
108
109
		$actualArray = [];
110
		$actualObject = $articleModel->getAll();
111
112
		/* process articles */
113
114
		foreach ($actualObject as $value)
0 ignored issues
show
The expression $actualObject of type object|null is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
115
		{
116
			$actualArray[] = $value->alias;
117
		}
118
119
		/* compare */
120
121
		$this->assertEquals($expectArray, $actualArray);
122
	}
123
124
	/**
125
	 * testQuery
126
	 *
127
	 * @since 4.0.0
128
	 *
129
	 * @param array $expectArray
130
	 *
131
	 * @dataProvider providerAutoloader
132
	 */
133
134
	public function testQuery(array $expectArray = []) : void
135
	{
136
		/* setup */
137
138
		$articleModel = new Model\Article();
139
140
		/* actual */
141
142
		$actualArray = $articleModel->query()->findFlatArray('id');
143
144
		/* compare */
145
146
		$this->assertEquals($expectArray, $actualArray);
147
	}
148
149
	/**
150
	 * testClearCache
151
	 *
152
	 * @since 4.0.0
153
	 */
154
155
	public function testClearCache() : void
156
	{
157
		/* setup */
158
159
		$articleModel = new Model\Article();
160
161
		/* actual */
162
163
		$actual = $articleModel->clearCache();
0 ignored issues
show
Are you sure the assignment to $actual is correct as $articleModel->clearCache() (which targets Redaxscript\Model\ModelAbstract::clearCache()) seems to always return null.

This check looks for function or method calls that always return null and whose return value is assigned to a variable.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
$object = $a->getObject();

The method getObject() can return nothing but null, so it makes no sense to assign that value to a variable.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
164
165
		/* compare */
166
167
		$this->assertNull($actual);
168
	}
169
}
170