Completed
Push — master ( 1da492...320203 )
by Henry
07:00
created

tests/unit/Model/ModelAbstractTest.php (1 issue)

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
			])
41
			->save();
42
		Db::forTablePrefix('articles')
43
			->create()
44
			->set(
45
			[
46
				'title' => 'Article Two',
47
				'alias' => 'article-two'
48
			])
49
			->save();
50
	}
51
52
	/**
53
	 * tearDown
54
	 *
55
	 * @since 4.0.0
56
	 */
57
58
	public function tearDown() : void
59
	{
60
		$this->dropDatabase();
61
	}
62
63
	/**
64
	 * testGetById
65
	 *
66
	 * @since 4.0.0
67
	 *
68
	 * @param int $id
69
	 * @param string $expect
70
	 *
71
	 * @dataProvider providerAutoloader
72
	 */
73
74
	public function testGetById(int $id = null, string $expect = null) : void
75
	{
76
		/* setup */
77
78
		$articleModel = new Model\Article();
79
80
		/* actual */
81
82
		$actual = $articleModel->getById($id)->alias;
83
84
		/* compare */
85
86
		$this->assertEquals($expect, $actual);
87
	}
88
89
	/**
90
	 * testGetById
91
	 *
92
	 * @since 4.0.0
93
	 *
94
	 * @param array $expectArray
95
	 *
96
	 * @dataProvider providerAutoloader
97
	 */
98
99
	public function testGetAll(array $expectArray = []) : void
100
	{
101
		/* setup */
102
103
		$articleModel = new Model\Article();
104
105
		/* actual */
106
107
		$actualArray = [];
108
		$actualObject = $articleModel->getAll();
109
110
		/* process articles */
111
112
		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...
113
		{
114
			$actualArray[] = $value->alias;
115
		}
116
117
		/* compare */
118
119
		$this->assertEquals($expectArray, $actualArray);
120
	}
121
122
	/**
123
	 * testQuery
124
	 *
125
	 * @since 4.0.0
126
	 *
127
	 * @param array $expectArray
128
	 *
129
	 * @dataProvider providerAutoloader
130
	 */
131
132
	public function testQuery(array $expectArray = []) : void
133
	{
134
		/* setup */
135
136
		$articleModel = new Model\Article();
137
138
		/* actual */
139
140
		$actualArray = $articleModel->query()->findFlatArray('id');
141
142
		/* compare */
143
144
		$this->assertEquals($expectArray, $actualArray);
145
	}
146
147
	/**
148
	 * testClearCache
149
	 *
150
	 * @since 4.0.0
151
	 */
152
153
	public function testClearCache() : void
154
	{
155
		/* setup */
156
157
		$articleModel = new Model\Article();
158
159
		/* actual */
160
161
		$actual = $articleModel->clearCache();
162
163
		/* compare */
164
165
		$this->assertNull($actual);
166
	}
167
}
168