1<?php
2
3namespace Rubix\ML\Tests\Persisters;
4
5use Rubix\ML\Persistable;
6use Rubix\ML\Persisters\Persister;
7use Rubix\ML\Persisters\Filesystem;
8use Rubix\ML\Classifiers\DummyClassifier;
9use Rubix\ML\Persisters\Serializers\RBX;
10use PHPUnit\Framework\TestCase;
11
12/**
13 * @group Persisters
14 * @covers \Rubix\ML\Persisters\Filesystem
15 */
16class FilesystemTest extends TestCase
17{
18    /**
19     * @var \Rubix\ML\Persistable
20     */
21    protected $persistable;
22
23    /**
24     * @var \Rubix\ML\Persisters\Filesystem
25     */
26    protected $persister;
27
28    /**
29     * @var string
30     */
31    protected $path;
32
33    /**
34     * @before
35     */
36    protected function setUp() : void
37    {
38        $this->path = __DIR__ . '/test.model';
39
40        $this->persistable = new DummyClassifier();
41
42        $this->persister = new Filesystem($this->path, true, new RBX());
43    }
44
45    /**
46     * @after
47     */
48    protected function tearDown() : void
49    {
50        if (file_exists($this->path)) {
51            unlink($this->path);
52        }
53
54        foreach (glob("$this->path.*.old") ?: [] as $filename) {
55            unlink($filename);
56        }
57    }
58
59    /**
60     * @test
61     */
62    public function build() : void
63    {
64        $this->assertInstanceOf(Filesystem::class, $this->persister);
65        $this->assertInstanceOf(Persister::class, $this->persister);
66    }
67
68    /**
69     * @test
70     */
71    public function saveLoad() : void
72    {
73        $this->persister->save($this->persistable);
74
75        $this->assertFileExists($this->path);
76
77        $model = $this->persister->load();
78
79        $this->assertInstanceOf(DummyClassifier::class, $model);
80        $this->assertInstanceOf(Persistable::class, $model);
81    }
82
83    protected function assertPreConditions() : void
84    {
85        $this->assertFileNotExists($this->path);
86    }
87}
88